### Returning the largest value

I need some help with this problem: write a program that computes the maximum of four integer values. The main function should read in four integer values and then call another function three times. The other function determines the larger of a pair of integers each time. It should accept a pair of values as parameters and return the larger value through a third parameter. This function will require a combination of value and reference parameters.

To be honest, after rereading the problem I'm pretty sure I'm way off the mark with what I have so far. If anyone could help clear up the problem I would appreciate it because I'm pretty confused as to how to advance with this problem.

Here's the code so far:

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; void largest(int num1, int num2, int num3, int num4){ int var = 0; if (num1 > var){ var = num1; } else if (num2 > num1){ var = num2; } else if (num3 > var){ var = num3; } else if (num4 > var) { var = num4; } cout << var; } int main() { int num1,num2, num3, num4, wle = 0; cout << "Enter four integers: "; cin >> num1 >> num2 >> num3 >> num4; while(wle < 3){ largest(num1, num2, num3, num4); wle++; } return 0; }``````
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; void largest(int num1, int num2, int &num3); int main() { int num1,num2, num3,num4, max = 0; cout << "Enter four integers: "; cin >> num1 >> num2 >> num3 >> num4; cin.ignore(); largest(num1, num2, max); largest(num2, num3, max); largest(num3, num4, max); cout << "The maximum number is " << max << endl; cin.ignore(); return 0; } // Finds the max number of two numbers checks that number // against max and stores the greater value in max void largest(int num1, int num2, int &max) { if(num1 > num2) { if(num1 > max) max = num1; } else if (num2 > num1) { if (num2 > max) max = num2; } }``````
You're doing great, keep studying.

some clues to make it work better:

1) why do you call the function four times (in the while loop)? Once is sufficient, the other three are just mere repetitions.

2) your function "largest" SHOWS ON THE SCREEN the largest value of four integers. You are supposed to RETURN a value from the function.

3) your function takes four arguments, you are supposed to make a function that takes only two (and then yes, use it three times with a for loop).

I'm not solving this for you, just a general scheme:

 ``1234567891011121314151617181920`` ``````includes namespace int largest_OF_THESE_TWO(int num1, int num2){ // recieve two numbers, compare, // return largest } int main(){ // cin four numbers // declare hold_the_largest_of_two = 0; for (int iii = 0; iii < 3; iii++){ // use the function four times - or three times, it depends } by the end, you should have it } ``````

good luck!
Topic archived. No new replies allowed.