Finding Max Number

#include <iostream>
using namespace std;
int max(int num1, int num2, int num3);
int main ()
{
int num1, num2, num3, large;
cout << "Please enter three numbers"<< endl;
cin >> num1 >> num2 >> num3;
large = max(num1, num2, num3);
cout << "The largest number is " << large << endl;
return 0;
}
int max (int num1, int num2, int num3)
{
int large = num3;
if (num1 > num3)
num1 = large;
if (num2 > num3)
num2 = large;

return large;
}



For some reason it keeps num3 as large, any help at all?
first off code tags

secondly

you set num3 to large at the start and you never change it. you should figure out which one is largest first and then set large.

and also you are giving one of the nums larges value not setting large to its value. so you should swap it around

//large = num1 or num2 or num3
I need the program to decide what number is the largest. You enter in the three numbers and then the computer decides which is the largest.
step through your code line by line (just this bit):

1
2
3
4
5
int large = num3; 
if (num1 > num3)
num1 = large;
if (num2 > num3)
num2 = large;


lets assume some starting variables
1
2
3
num1 = 8
num2 = 5
num3 = 4


first large is set to num3 so
1
2
3
4
large = 4
num1 = 8
num2 = 5
num3 = 4


the comparison num1 > num3 returns true (because 8 > 4)
so num1 is set to the value of large
1
2
3
4
large = 4
num1 = 4
num2 = 5
num3 = 4


the comparison num2 > num3 returns true (because 5 > 4)
so num2 is set to the value of large
1
2
3
4
large = 4
num1 = 4
num2 = 4
num3 = 4


then large (4) is returned
Last edited on
1
2
3
4
5
if (num1 > num3 && num1 > num2)
large = num1
blah
blah
blah
Thank you all. I got it.
Topic archived. No new replies allowed.