0

I am not sure where my mistake is

Trying to find maximum of four numbers using function in c++ #include <iostream> #include <cstdio> using namespace std; int max_of_four(int a, int b, int c, int d) { if (a>b) { if(a>c) { if(a>d) { max_of_four(a, b, c, d) == a; } } } else if (b>a) { if(b>c) { if(b>d) { max_of_four(a, b, c, d) == b; } } } else if (d>b) { if(d>c) { if(d>a) { max_of_four(a, b, c, d) == d; } } } else if (c>b) { if(c>a) { if(a>d) { max_of_four(a, b, c, d) == c; } } } return max_of_four(a, b, c, d); } int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); int ans = max_of_four(a, b, c, d); ("%d", ans); return 0; }

1st Feb 2019, 2:53 PM
Duna000
Duna000 - avatar
2 Réponses
+ 6
Your function max_of_four() keeps calling itself, which will lead to an infinite loop. Instead of return max_of_four(a, b, c, d), store the result in a variable and return its value. Also, max_of_four(a, b, c, d) == d won't work. == is the comparison operator, not assignment. And you can't assign a value to a function call.
1st Feb 2019, 3:04 PM
Anna
Anna - avatar
0
Trying hard
1st Feb 2019, 5:26 PM
Duna000
Duna000 - avatar