-
Need a little help
I am brand new at this C++ stuff and I am starting to get the hang of it, but one of the exercises in my book says to write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. I know how to do everything except for the part that says determine the largest and smallest number. I have looked over the chapter many times but I can't find how to do it. Can someone just give me a pointer on how I could do it. Thanks alot! :D
-
Are you sure this isnt homework? Anyway if you have the numbers stored as n1. n2, n3, n4, n5. I can think of a way to do it but it seems a bit clumsy to me, might be missing something obvious, anywya here goes:
if (n1> n5 && n1 > n4 && n1 > n3 && n1 > n2)
//n1 is biggest
if (n2> n5 && n2 > n4 && n2 > n3 && n2 > n1)
//n2 is biggest
if (n3 > n5 && n3 > n4 && n3 > n2 && n3 > n1)
//n3 is biggest
if (n4 > n5 && n4 > n3 && n4 > n2 && n4 > n1)
//n4 is biggest
if (n5 > n4 && n5 > n3 && n5 > n2 && n5 > n1)
//n5 is biggest
then a similar thing for smallest
if (n1< n5 && n1 < n4 && n1 < n3 && n1 < n2)
//n1 is smallest
if (n2< n5 && n2 < n4 && n2 < n3 && n2 < n1)
//n2 is smallest
if (n3 < n5 && n3 < n4 && n3 < n2 && n3 < n1)
//n3 is smallest
if (n4 < n5 && n4 < n3 && n4 < n2 && n4 < n1)
//n4 is smallest
if (n5 < n4 && n5 < n3 && n5 < n2 && n5 < n1)
//n5 is smallest
I'm sure there must be a cleaner way but as far as I can think that method works.
-
tip
set a value called smallest and a value called largest,
after each is value is entered evaluate them and copy them in to one of these variables if necessary
-
Thanks
Thanks alot guys I didn't mean for you to right out all of the code I just wanted a pointer on it but thanks for helping!:D
-
Code:
int GetLow (const int &num[]) {
int i, LOW = 0;
for (i = 0; i < 5; ++i) {
if (num[i] < num[LOW])
LOW = i;
}
return LOW;
}
// main
int num[5] = {3, 5, 6, 1, 2 };
int *temp = new int[5];
for (i = 0; i < 5; ++i) {
GetLow(num);
temp[i] = num[low];
num[low] += (16 << 4);
}
for (i = 0; i < 5; ++i)
num[i] = temp[i];
delete[] temp;