C++ call stack Problem 2

Implement the following function:

int max(int a, int b, int c);

Your program receives 3 numbers and prints out the maximum number // (Question)

( What I have tried , also please explain thanks)
#include <iostream>
using namespace std;


int max(int a, int b, int c)
{
int result;
cin >> a >> b >> c;

if (a > b || a > c)
result = a;
else
result = b;
if (c>a||c>b)
result = c;

return result;
}

int main()
{
int i;
int j;
int k;
int u = max(i,j,k);

cout << "The maximum between " << i <<
" and " << j << " is " << k << endl;

return 0;
}
1. Use code tags when posting code ->https://www.cplusplus.com/articles/jEywvCM9/
2. Move the input to main.
3. It's much easier to calculate the max of two numbers.

So
 
int u = max( max(i,j), k);
This has been asked before. std::max() accepts an initialiser_list, so:

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

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

int main()
{
	const int i {10}, j {34}, k {21};
	const int u {max(i, j, k)};

	std::cout << "The maximum between " << i <<
		" and " << j << " and " << k << " is " << u << '\n';
}



> Implement the following function:
> int max(int a, int b, int c);
It's for the OP to implement the function, not use whatever is in the toolbox.
Topic archived. No new replies allowed.