Array help

Hi everyone, so for my programming class we just learned arrays and now are tasked with a lab that gave you a array declaration. I had to find sum,avg,first last num and display them. I also had to add 10 to each test score and display it. I am currently stuck on finding the max test score and displaying it
Any help would be appreciated.

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
 #include <iostream>
using namespace std;
int main()
{
	int tests[6]; // array declaration
	int sum = 0;
	float avg;
	//input test scores
	cout << " Enter " << 6 << " test scores: " << endl;
	for (int i = 0; i < 6; i++)
	{
		cout << "Enter Test " << i + 1 << ": ";
		cin >> tests[i];
	}

	cout << "first test score->" << tests[0] << endl;
	cout << "last test score->" << tests[5] << endl;
	//display all scores
	for (int i = 0; i < 6; i++)
	{
		cout << "All test scores" << endl;
		cout << tests[i] << endl;
	}
	//sum
	sum = tests[0] + tests[1] + tests[2] + tests[3] + tests[4] + tests[5];
	cout << "The sum of all test scores is ->" << sum << endl;

	//avg
	avg = sum / 6;
	cout << "The avg of all test scores is->" << avg << endl;
	//add 10 to each score, print each score (add to all elements)
	for (int i = 0; i < 6; i++)
	{
		tests[i] = tests[i] + 10;
		cout << "Add 10 to each score -->" << tests[i] << endl;
	}
	//find highest score and display score(

	return 0;
}
Last edited on
Initialize a variable to a number less than any that will be in the array. You will then use a for loop like you have to move through the array. Inside the loop, you will need an if statement which will test if the current number in the array is greater than the previous highest. If the current number is higher then it sets that number equal to highest and moves to the next element. When you are done you will have the highest number. It should look similar to:

int highest = 0;

1
2
3
4
5
6
7
8
	for (int i = 0; i < 6; i++)
	{
		if (tests[i] > highest)
		{
			highest = tests[i];
		}
	}
	cout << "The highest score is " << highest << endl;
Last edited on
Topic archived. No new replies allowed.