Array problems, need help!

I'm having troubles with calculating the new score and calculating and displaying the difference between each new score and the average of new scores.
we learned arrays yesterday and i feel completely lost.

I got some of the new score calculated but its coming out completely wrong and i dont even know how to start to calculate and display the difference between each new score and the average of new scores.

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
  #include <iostream>

using namespace std;

int main()
{
    //Write a program that uses an integer array to store 12 test scores that a user enters
    int a[12];
    int i=0;
    
    cout << "Please enter the scores: ";

        for(i=0;i<12;i++)
        {
            cin>>a[i];
        }
        
    //Calculates the highest score
    int max=a[0];
    for(i=0;i<12;i++)
        
    {
        if(a[i]>max)
            max=a[i];
    }
    
    cout << "The highest score is: ";
    cout << max << endl;
    
//Use following formula to calculate and display the new scores:
//original score/highest score * 100, only display the whole number part of the new score

    int newScore;
    
    newScore=(a[i])/(max)*100;
    
    cout << newScore << endl;

    int sum=0;
    for(i=0;i<12;i++)
        sum+=a[i];

    cout << "The average is: " << sum/30;

//Calculate and display the difference between each new score and the average of new scores.

    return 0;
}
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
54
55
56
#include <iostream>

using namespace std;

int main()
{
	//Write a program that uses an integer array to store 12 test scores that a user enters
	int AllScores[12];
	int i = 0;

	// Output message asking user to enter the scores
	cout << "Please enter the scores. \n";
	for (int i = 0; i < 12; ++i)
	{
		// User inputs a score.
		cout << "Input: ";
		cin >> AllScores[i];
		cout << endl;
	}

	//Calculates the highest score
	int max = 0;
	for (int i = 0; i < 12; ++i)

	{
		// If the value stored in array is larger than
		// current max, then change max to new max value.
		if (AllScores[i] > max)
			max = AllScores[i];
	}

	cout << "The highest score is: ";
	cout << max << endl;

	//Use following formula to calculate and display the new scores:
	//original score/highest score * 100, only display the whole number part of the new score

	float newScore = 0;
	for (int i = 0; i < 12; ++i)
	{
		// Note that dividing the same score will result in 1 and * 100 means 1*100 = 100
		newScore = ((AllScores[i]) / (max)) * 100;
		cout << "New Score [" << (i + 1) << "]: " << newScore << endl;
	}

	int sum = 0;
	for (int i = 0; i < 12; ++i)
		sum += AllScores[i];

	// Final Output
	cout << "The average is: " << (sum / 12) << endl;
	// Wait for user to press any of the character or return keys
	system("pause");
	//Calculate and display the difference between each new score and the average of new scores.
	return 0;
}


One Thing I noticed is that you weren't specifying the type for "i" in your for loops.
Try this out, I didn't try to compile it though, but most likely works.
Last edited on
Ehh i cant use some of those terms like system
We have not learned it. its basically the first lesson of Arrays.
You could take out the system("pause"); then, it's only keeping the console open until you press a key.

The information will still show up in the Output window though incase you do mind about keeping the Console window open.
Look at line 35:

 
    newScore=(a[i])/(max)*100;


What is the value of i going to be at this point?

Having worked that out, what is the value of a[i] going to be?
That would be the scores input into the array you created.

Since you're in a for() loop, and you defined int i = 0, the i would be whatever iteration of i is at that point and since you're only iterating by 1 each time it would be.
1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0; i < 12; ++i)
{
    newScore = (a[i] / max) * 100;
    // What i would look like in each loop run
    // i = 0, i = 1, i = 2, i = 3, i = 4 ... i = 11
    
    // Lets say that our first number is the max number "500"
    // newScore = (a[0] / max) * 100
    // newScore = (500 / 500) * 100;
    // newScore = 1 * 100;
    // newScore = 100;
}


Edit** I just read the name of who posted that question, lol... Sorry MilkeyBoy
Last edited on
Edit** I just read the name of who posted that question, lol... Sorry MilkeyBoy

No worries - and it was kind of a rhetorical question, trying to point the OP to where there's a problem, and what the nature of the problem is.

Topic archived. No new replies allowed.