C++ call stack Problem

(This is the Question)
Implement the following function:
int max(int a, int b, int c);
Your program receives 3 numbers and prints out the maximum number.


(and this is what I have tried) ( which is not even running lol, Pleasehelp)
#include <iostream>
using namespace std;
int max(int a, int b, int c);
{
int result;

if (int a > b || int a > c)
else ( int a)
return result;
}

int main() {
int a,b,c;
int m = max(a,b,c);
cout <<"the maximum is";
return 0;
}

also Please explain as I Want to learn , thanks.
Last edited on
I guess you belong to the group of people who have a dislike for books and studying.

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

int max(int a, int b, int c)
{
    int result = a;
    
    if (b > result)
        result = b;
        
    if (c > result)
        result = c;
    
    return result;
}

int main() 
{
    std::cout <<"the maximum is: " << max(4,3,7);;
}


OR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <algorithm>
#include <initializer_list>

int max(int a, int b, int c)
{
    std::initializer_list<int> il{a,b,c};
    return *std::max_element(std::begin(il), std::end(il));
}

int main() 
{
    std::cout <<"the maximum is: " << max(4,3,7);;
}
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

int max(int a, int b, int c)
{
	return std::max(std::max(a, b), c);
}

int main() {
	std::cout << "the maximum is: " << max(4, 7, 9) << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>
#include <initializer_list>

int max( int a, int b, int c )
{
    return std::max( { a, b, c } );
}

int main() 
{
    std::cout << "The maximum is: " << max( 4, 3, 7 );
}
Topic archived. No new replies allowed.