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
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming problem related to finding the minimum number of ingredients for a shake, with a focus on sorting these ingredients. Participants explore the implementation of sorting algorithms and the specifics of ingredient storage and manipulation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a C++ code snippet for loading ingredients but notes uncertainty about the sorting approach.
  • Another participant points out that the provided code does not include any sorting functionality and suggests researching sorting algorithms compatible with the "left shake" and "right shake" methods.
  • Some participants express that the original question lacks clarity regarding how ingredients are stored and whether additional storage is available for sorting.
  • There is a discussion about the implications of only being able to swap adjacent ingredients, which limits the choice of sorting algorithms to a specific family that allows for such operations.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specifics of the problem, as there are multiple competing views regarding the storage of ingredients and the appropriate sorting algorithms to use. The discussion remains unresolved with respect to the best approach to take.

Contextual Notes

Participants note limitations in the original question, including missing details about ingredient storage and the constraints on swapping ingredients. There is also uncertainty about the efficiency of various sorting algorithms in relation to the specified methods.

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:
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 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K