# Very basic programming help please

• 08-13-2006
xrated
I am trying to make a program that will state the smallest number entered, with only 2 numbers it works.
Code:

```    if ( x > y )     cout << "Smallest: " << x << endl;         if ( y > x )     cout << "Smallest: " << y << endl;```
With 3 or more numbers it prints all the values as the smallest as if quoting the values entered.
Code:

```    if ( x > y , z )     cout << "Smallest: " << x << endl;         if ( y > x , z )     cout << "Smallest: " << y << endl;     if ( z > y , x )     cout << "Smallest: " << z << endl;```
I have only just started leaning c++ so if someone could please help it would be apprieciated. I have tried to work this out myself but must be missing something really simple.
• 08-13-2006
SlyMaelstrom
The easiest way to do this is to store your numbers in an array, that way you can cycle through them with only one identifier.
Code:

```int arry[10]; // Read in 10 numbers int lowest = arry[0];  // Just assign it to whatever value starts the array for(int i = 1; i < 10; i++) {  // You can ignore the 0 index since it's already considered lowest   if (arry[i] < lowest)       arry[i] = lowest; }```
...and there you go. By the way, the comma operator isn't used like that, and that's where you were probably having the most confusion. Other than that, you should consider using 'else if' and 'else'. There is no point in checking if y is greater than x if you already know x is greater than y.
• 08-13-2006
anon
This doesn't work even with 2 numbers: if x is smallest, then x > y shouldn't be true.
Besides that, the code doesn't handle cases where x == y.

As to three numbers, you should evaluate multiple conditions using the "and" - && operator: if (x < y && x < z). Again you should decide, how you are going to handle cases where the two (or three) smallest values are equal.

If you want to find the smallest value in a larger collection, it's time to look for arrays.
• 08-13-2006
xrated
Thanks for your help, exactly what I needed to know.
• 08-13-2006
Mario F.
You can also use a container instead of an array. The simplest on this case would be a std::set. You could know at any time which is the lowest input entered by simply reading the first element of the set.
• 08-13-2006
Wraithan
Mario,

Stuff like this looks like basic homework stuff, which means use of containers that they haven't learned will get you docked points depending on the teacher. Though if this is just a person playing with the language, nevermind me... :D
• 08-13-2006
Mario F.
Well, you are probably right :)
• 08-13-2006
xrated
I'm trying to learn through a book so it is homework in a way but not homework that gets checked by teachers. It's good to learn different ways to achive the same result. Thanks for the help :)
• 08-14-2006
jafet
There's a very simple way of finding the smallest value in a list of any size. First assume the first value is the smallest. If the next value is smaller, then use the next value, otherwise keep the current value. Try it with pencil-and-paper on small test arrays and convince yourself it works.