Need help with the first part of Homework.

Okay guys, sorry if this is a bad submission but my first part of the homework is to return the largest of a, b, c. Here is my code so far... I have two solutions but I don't know if I'm doing this correctly.

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
46
47
48
49
50
51
52
53
#include <string>
#include <cassert>
#include <iostream>

using namespace std;

// Returns the largest of a, b, c.
int maximum(int a, int b, int c)
{
	// EDIT HERE
	int a = 0;
	int b = 0;
	int c = 0;
	for (int x = 1; x < 100; x++)
	{
		cin >> a;
		if (a > b);
		{
			b = a;
			cout << "The largest number is: " << b << endl;
		}
		if (b > c)
		{
			c = b;
			cout << "The largest number is: " << c << endl;
		}
		else
			cout << "The largest number is: " << a << endl;
		
	}
	/*
	cout << "Input a number for a: ";
	cin >> a;
	cout << "Input a number for b: ";
	cin >> b;
	cout << "Input a number for c: ";
	cin >> c;

	if ((a > b && a > c))
	{
		cout << "The largest is: " << a << " A";
	}
	if ((b > a && b > c))
	{
		cout << "The largest is: " << b << " B"; 
	}
	if ((c > a && c > b))
	{
		cout << "The largest is: " << c << " C";
	}*/
	
	return 0;
}
Okay, I came up with something like this, but I don't know why it's giving me an error for int a, b, and c? It says "redefinition of formal parameter 'a' 'b' and 'c'.
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
#include <string>
#include <cassert>
#include <iostream>

using namespace std;

// Returns the largest of a, b, c.
int maximum(int a, int b, int c)
{
	// EDIT HERE
	int a;
	int b;
	int c;
	cout << "Input a three numbers: ";
	cin >> a >> b >> c;

	if (a >= b) {
		if (a >= c)
		{
			cout << "The largest is: " << a << endl;
		}
		else
		{
			cout << "The largest is: " << b << endl;
		}
	}
	else {
		if (b >= c)
		{
			cout << "The largest is: " << b << endl;
		}
		else
		{
			cout << "The largest is: " << c << " C";
		}
	}
	return 0;
}
Is this the entirety of the code? If not post it because it seems the problem would be somewhere else. Also at line 24 shouldn't you be outputting C?
You do redefine(re-declare) a,b,c in two position

int maximum(int a, int b, int c)

and

1
2
3
int a = 0;
int b = 0;
int c = 0;


And you may want to #include<algorithm> and just return std::max(a,b,c)

By the way, try to separate Input/Output and logic is a good practice (cout outside maximum function)
Last edited on
Topic archived. No new replies allowed.