Checking for greatest value with multiple ints

For my intro to C++ class we were recommended to create a program that checked for a maximum value and and average value. My question is, is there a more efficient want to check for a maximum value without creating more and more if statements? If I had 50 inputs for instance, would I have to make 49 if statements?

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
  #include <iostream>
using namespace std;
int main()
{
  const float INPUT_AMOUNT = 3.0;
  short int1;
  short int2;
  short int3;
  short max;
  float avg;

  cout << "Please enter the value of integer 1: " << endl;
  cin >> int1;

  cout << "Please enter the value of integer 2: " <<  endl;
  cin >> int2;

  cout << "Please enter the value of int 3: " << endl;
  cin >> int3;

  if (int1 > int2)
    max = int1;
  else
    max = int2;

  if (int3 > max)
    max = int3;


  avg = (static_cast<float>((int1 + int2 + int3) / INPUT_AMOUNT));
  cout << avg << endl;
  cout << max << endl;

  return 0;
}
If I had 50 inputs for instance, would I have to make 49 if statements?

You have to use an array, a for-loop and that's it.
Can you give me the basic idea behind an array?
You can actually get it done without an array :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
	int i;
	int max = -999888777;
	int avg, total = 0, val;
	for(i = 0; i < 50; i++)
	{
		cout << "Enter value " << i + 1 << " : "; cin >> val;
		if(max < val) max = val; total += val;
	}

	avg = total / 50;
	cout << avg << endl;
	cout << max << endl;

	return 0;
}
Topic archived. No new replies allowed.