Using C++ to find the minimum number of something

  • Context: Comp Sci 
  • Thread starter Thread starter emmadun
  • Start date Start date
  • Tags Tags
    C++ Minimum
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
emmadun
Messages
8
Reaction score
0
Homework Statement
In a bar, drinks consist of n-ingredients, conveniently numbered from 1 to n, added to a shaker in some random order. You have to mix them properly to have them ordered increasingly, that is sorted. You can perfromr operations of left and right shakes.

The task is: perform a minimum number of shakes to prepare the drink. Be quick!
Relevant Equations
Right shake is equivalent to:
For place:=1 to N-1 do:
If(ingredient(place)>ingredient(place+1)) swapIngredients(place, place+1);

A left shake is very similar and equivalent to:
For place:=N-1 downto 1 do:
If (ingredient(place)>ingredient(place+1)) swapIngredients (place, place+1));
I attempted going with c++ and this is what i got:
C:
#include <iostream>
Using namespace std;

int i, j, keeper, n;

int Ingredients[100], Blender[100],

count<<”How many ingredients does the shake need?”<<endl;

cin>>n;

count<<”Type down the ingredients:”<<endl;

for(i=1; i<=n; i++)

{

count<<”ingredients[“<<i<<”]=”;

Cin>>Ingredients[i];

}
from here, i wanted to order the ingredients increasingly using an array but i don't think my approach to the problem is correct.
Any ideas anyone might have?[/i]
 
Last edited by a moderator:
on Phys.org
Your program allows you to load in the ingredients, but it doesn't actually do any sorting.

You might want to google "sorting algorithms" and see if you can find one that is compatible with your "left shake" and "right shake" methods.

You can expect that some sorting algorithms, albeit more efficient, are not compatible with the "left shake" and "right shake" methods. I'm guessing you are supposed to implement a sorting algorithm that is compatible with these methods. (Fortunately, there is a sorting algorithm that is compatible with the "left shake" and/or "right shake" methods and is fairly easy to code.)

(Also, you might want to load in your ingredients from a file or something. Typing them into the console every time could be a bear to debug.)
 
The question seems to be missing information. It mentions swapping ingredients, but doesn't specify how the ingredients are stored, which I assume is separate from the left and right shakes, and if there is additional empty storage that could be used to store ingredients.
 
rcgldr said:
The question seems to be missing information. It mentions swapping ingredients, but doesn't specify how the ingredients are stored, which I assume is separate from the left and right shakes, and if there is additional empty storage that could be used to store ingredients.
If you look carefully at the pseudo code, only adjacent ingredients can be swapped. I assume that a simple, temporary variable is suitable (and available) for this process.

The fact that only adjacent ingredients can be swapped narrows down the possibilities to a particular family of sorting algorithms. There may be other sorting algorithms out there (and some more efficient), but only a particular family of them involve swapping adjacent elements.