Comp Sci Using C++ to find the minimum number of something

  • Thread starter Thread starter emmadun
  • Start date Start date
  • Tags Tags
    C++ Minimum
AI Thread Summary
The discussion focuses on a C++ program intended to find the minimum number of ingredients for a shake but lacks a proper sorting implementation. Users suggest that the program should incorporate a sorting algorithm compatible with the specified "left shake" and "right shake" methods, as the current code does not sort the ingredients. There is also a recommendation to load ingredients from a file to simplify debugging. The conversation highlights the need for clarity on how ingredients are stored and the limitations of swapping only adjacent elements. Overall, implementing an appropriate sorting algorithm is crucial for the program's functionality.
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],

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

cin>>n;

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

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

{

Cout<<”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:
Physics news 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.
 

Similar threads

Replies
3
Views
1K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
8
Views
1K
Replies
3
Views
1K
Replies
9
Views
2K
Replies
13
Views
2K
Back
Top