Using C++ to find the minimum number of something

  • Comp Sci
  • Thread starter emmadun
  • Start date
  • Tags
    C++ Minimum
In summary: In this particular case, the insertion sort is one of these sorting algorithms.In summary, the question is asking for a sorting algorithm that is compatible with the "left shake" and "right shake" methods. The insertion sort is one of the sorting algorithms that is compatible with these methods.
  • #1
emmadun
8
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
  • #2
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.)
 
  • #3
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.
 
  • #4
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.
 

FAQ: Using C++ to find the minimum number of something

How can I use C++ to find the minimum number of a set of values?

To find the minimum number of a set of values using C++, you can use the min_element function from the algorithm library. This function takes in two parameters - the beginning and ending iterators of the set of values - and returns an iterator pointing to the minimum element.

Can I use C++ to find the minimum number of a specific data type?

Yes, you can use C++ to find the minimum number of any data type as long as it is comparable. This means that the data type must have the < operator defined for it. You can then use the min_element function to find the minimum value.

How does C++ determine the minimum number in a set of values?

C++ determines the minimum number in a set of values by comparing each element in the set to the current minimum value. If an element is smaller than the current minimum, it becomes the new minimum. This process continues until all elements have been compared and the minimum is found.

Can I use C++ to find the minimum number of a dynamically allocated array?

Yes, you can use C++ to find the minimum number of a dynamically allocated array. You can use the min_element function by passing in the beginning and ending iterators of the array as parameters. Alternatively, you can also use a loop to iterate through the array and keep track of the minimum value.

Are there any alternative methods to find the minimum number in C++?

Yes, there are alternative methods to find the minimum number in C++. One method is to use the sort function from the algorithm library to sort the set of values in ascending order, and then access the first element, which will be the minimum. Another method is to use a loop to iterate through the set of values and keep track of the minimum value. However, using the min_element function is the most efficient and recommended method.

Similar threads

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