Finding the largest number!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

/*
      max3.cpp -- program to print out the largest of three input values
     usage: ./max3
     input: three doubles
    output: the largest of the three input values
      note: no prompt for user, no explanation on output

    modified by: David Kim
    modified on: date of completion
 */

using namespace std;

double max3(double, double, double);    // tell compiler about this

int main()
{
        double  a,b,c;                  // input values
        double  largest;                // result

        cout << "Please enter three numbers.";
        cin >> a >> b >> c;             // read in three values
        largest = max3(a,b,c);          // compute the largest
        cout << largest << endl;        // display it on the screen
}

double max3(double n1, double n2, double n3) //where MY code begins
{

    if ( n1 > n2 && n1 > n3)
    {
        cout << n1 << endl;
    }
   else if ( n2 > n1 && n2 > n3)
    {
        cout << n2 << endl;
    }
   else if ( n3 > n1 && n3 > n1)
    {
        cout << n3 << endl;
    }
    return 0;
}


So basically what I'm trying to do is find the largest number and at the moment it's kind of working but when I input the three doubles and hit return I get the correct answer and on the line below a "0". Why is that?

Also, the first half my professor made can someone help me through with the purpose of that top half. Mind you this is my first course ever in computer science and It's only the third week in so I'm still kind of lost as the professor skipped over the more basics of syntax and whatnot.
largest = max3(a,b,c);

Here, you set largest to some value. What value? Whatever is returned from the function.

Let's see what that returned value is:

return 0;
The returned value is zero.

cout << largest << endl;
Here, you output the value largest. What is that value? We just discovered that it is zero.
Last edited on
When you call the function max3, it outputs the max value and at the end of the function returns 0. You'd be better off changing your cout statements in the if and else if statements to return n1 , n2, or n3, and not have the return 0 at the end of the function
i will not explain everything in detail, you can figure it out by yourself just b doing it.

cout prints data on the screen.
<< and >> are operators which is way to advanced to discuss right now.
cin lets you capture data enter via the keyboard.

so , or example, enter this when the program runs
1<enter>2<enter>3<enter>

this will result in:
a : 1
b : 2
c : 3

now you need to determine which is the largest, a b or c.


max3 is a function where a b and c are passed to.
n1 will get vallue of a, n2 will get b and n3 will get c.


in your code you are printing the result.
But you teacher wants you to return the largest value back to the main and there it will be stored in the varaible "largest".
and this variable is shown on the screen.

which, in your case, is always 0, because you return 0 at the end of your function.

you should return the max number and delete all the cout lines in your own code.

hope this helps.
Yeah wow Jikax thanks a lot! That really does clear up things. And so because I have "return 0;" in my code all that function is doing is kind of nothing because it'll always return "0" back to the main which will then spit out "0" as the largest value.

So in this case what does my return have to be?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if ( n1 > n2 && n1 > n3)
    {
         return n1;
        //cout << n1 << endl;
    }
   else if ( n2 > n1 && n2 > n3)
    {
        return n2;
        //cout << n2 << endl;
    }
   else if ( n3 > n1 && n3 > n1)
    {
       return n3; 
       //cout << n3 << endl;
    }

Do something like this, but there isn't any need for those cout statements at all, so you could either comment them out or just plain get rid of them
j b that's exactly what I tried but I got an error message saying "control reaches end of non-void function"

I'm guessing that's because I deleted the "return 0;" that I had at the end of all this code but bleh I'm confused.
That error "control reaches end of non-void function" means you do not have a return value if none of your control statements get activated. Even though you programmed it so that one of the three will always run, your compiler does not know that. Simply leave the code as jb has given you and just add a return -1; or some number to the end of the code so the compiler is satisfied.
Last edited on
if code doesn't matter specifically, it'd be much more efficient to do this

1
2
3
4
5
6
7
8
9
10
11
12
double max3(double n1, double n2, double n3) //where MY code begins
{
	double vals[]={n1,n2,n3};
	double maxval=vals[0];
	for(int i=1;i<3;i++)
	{
		if(vals[i]>maxval)
			maxval=vals[i];
	}
	return maxval;

}


And if you stick to your original program, it would be beneficial to change > signs to >=, so if two values are equal it still prints the max value
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double result = 0;
if ( n1 > n2 && n1 > n3)
    {
         result = n1;
        //cout << n1 << endl;
    }
   else if ( n2 > n1 && n2 > n3)
    {
        result =  n2;
        //cout << n2 << endl;
    }
   else if ( n3 > n1 && n3 > n1)
    {
       result =  n3; 
       //cout << n3 << endl;
    }
return result;


However, you should look at what happens when 2 numbers are the same... What do you want to happen?
And you got a little error in your last else if statement;)
Last edited on
Just because I like this sort of thing. It's like an addiction, really :(
1
2
3
4
double max3(double n1, double n2, double n3) 
{
  return (n3 > (n1 > n2 ? n1 : n2) ? n3 : (n1 > n2 ? n1 : n2) );
}


Still suffers if values are the same, though.
Last edited on
@moschops I think that is a bit to advanced ;)...
While we're on alternative solutions:

1
2
3
4
5
6
7
8
9
inline double max2(double n1, double n2)
{
     return n1 > n2 ? n1 : n2 ; 
}

double max3(double n1, double n2, double n3)
{
     return max2(max2(n1,n2), max2(n2,n3)) ;
}

Topic archived. No new replies allowed.