Function with IF statements - Error?

I am new to c++ and been self learning, still a newbie. I have been doing exercises to practice. I have got a error with return value for my function.

Can someone please help? - I am trying to write a function which will return the highest number out of 4 numbers which is going to be inputted.

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

int highest(int a, int b, int c, int d)
{
if(a>b)
{
if(a>c)
{
if(a>d)
{
return a;
}
else return d; 
}
}
else if(b>c)
{
if(b>d)
{
return b;
}
else return d;
}
}




Last edited on
Is that ALL of your code? You really need to indent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int highest(int a, int b, int c, int d)
{
	if (a > b)
	{
		if (a > c)
		{
			if (a > d)
			{
				return a;
			}
			else return d;
		}
	}
	else if (b > c)
	{
		if (b > d)
		{
			return b;
		}
		else return d;
	}
}



I see no reason for errors in this function, you should put the rest of your code.
The else (and hence a return) for if(b>c) is missing. What if (a > b) and (b <= c)?
With the help of a helper function (say std::max) or just write your own version, you can reduce this to
1
2
3
int highest(int a, int b, int c, int d) {
  return max(a,(max(b,max(c,d))));
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
using namespace std;

template<typename T> T largest( T a, T b) 
{
   return a > b ? a : b;
}

template<typename T, typename... Tail> T largest( T a, Tail... b )
{
   return largest( a, largest( b... ) );    
}

int main()
{
   int a = 1, b = 3, c = 2, d = -2;
   cout << largest( a, b, c, d ) << '\n';
   cout << max( { a, b, c, d } ) << '\n';
}
what if the largest number is c?
This is my code to find the largest number, correct me if I'm wrong.
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
int getLargest(int a, int b, int c, int d)
{
	if (a > b)
	{
		if (a > c)
		{
			if (a > d)
			{
				return a;
			}
			else return d;

		}
		if (c > d)
			return d;

		else return c;
	}
	if (b > c)
	{
		if (b > d)
			return b;
		else return d;
	}
	if (c > d)
		return c;
	else return d;
}
Jc126 wrote:
correct me if I'm wrong.

You're wrong.

Try a=2, b=1, c=4, d=3

This bit's definitely wrong:
1
2
3
4
	if (c > d)
			return d;

		else return c;



I do suggest code with a little less ... branching.
Last edited on
1
2
3
4
5
6
7
8
9
10
int highest( int a, int b, int c, int d )
{
    int highest_so_far = a ;

    if( b > highest_so_far ) highest_so_far = b ; // higher of a and b
    if( c > highest_so_far ) highest_so_far = c ; // highest of a,b and c
    if( d > highest_so_far ) highest_so_far = d ; // highest of a,b,c and d

    return highest_so_far ;
}

Or
1
2
3
4
5
// using the library: #include <algorithm> (you may ignore this for now)
constexpr int highest( int a, int b, int c, int d )
{
    return std::max( { a, b, c, d } ) ;
}
Would this be correct now?

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

int highest(int a, int b, int c, int d)
{
	if (a > b)
	{
		if (a > c)
		{
			if (a > d)
			{
				return a;
			}
			else return d;
		}
	}
	else if (b > c)
	{
		if (b > d)
		{
			return b;
		}
		else return d;
	}
        else if (c > d)
        {
           return c;
         }
          else return d;
}




No. There are circumstances under which it would return nothing.

BTW, how many separate accounts do you have?

BTW2, please upload your WHOLE code; then we can test it in situ.
1
2
3
4
5
6
7
8
9
10
11
12
13
int highest(int a, int b, int c, int d)
{
  if (a > b)
  {
    // the only thing we know so far is that b is not the largest
    // we have to compute highest(a, c, d) here
  }
  else
  {
    // the only thing we know so far is that a is not the largest
    // we have to compute highest(b, c, d) here
  }
}

Writing all the nested conditions makes code long and complex.

Look at the solution by JLBorges. There are only 3 tests.


@salem c: In this specific case there are power-of-two operands. One could:
return std::max( std::max(a, b), std::max(c, d) );
Topic archived. No new replies allowed.