Worlds worst programmer with a question on array functions

Hey, yup, the worlds worst programmer here for a question on a program gone wrong. Here is the assignment below.

Description

Write a program to find the average score in a classroom. The allowable maximum class size is 20. The program will ask the user to enter the class size. Then it will ask the user to enter a test score. It will repeatedly ask the user to enter a test score till all scores are entered. It will then display all the scores entered by the user and the average of the scores.

Above, if the user enters a class size that is greater than the maximum allowable class size, the program will display a message to that effect and exit.

Test Data

Run1

Enter class size <1-20>

10

Enter score

100

Enter score

90

Enter score

80

Enter score

70

Enter score

60

Enter score

50

Enter score

65

Enter score

75

Enter score

85

Enter score

95

Original Scores:

100 90 80 70 60 50 65 75 85 95

Average Score

77



Run2

Enter class size <1-20>

21

Class size is NOT within required range. The required range is 1 to 20.



Run3

Enter class size <1-20>

-1

Class size is NOT within required range. The required range is 1 to 20.


The code is below.

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

using namespace std;

int main()
{


    double scores[20];
    double sum,avg;
    int i, count;
    cout << "Enter score count<1-20>"<<endl;
    cin>>count;

    if((count<0) or(count>20))



    {
        cout<<"Invalid Count"<<endl;
        return 0;
    }




    for(i=0;i<count;i++)

    {
        cout<<"Enter score" <<endl;
        cin>>scores[i];
        sum=sum+scores[i];
    }



    avg=sum/count;



    cout<<"Original Scores: ";
    for(i=0;i<count;i++)

    {


    cout<<" " <<scores[i]<<endl;




    }

    cout<<"Average Score "<<avg<<endl;

    return 0;
}


output down below.

Enter score count<1-20>
5
Enter score
20
Enter score
30
Enter score
40
Enter score
50
Enter score
20
Original Scores:  20
 30
 40
 50
 20
Average Score 5.45495e+264

Process returned 0 (0x0)   execution time : 6.474 s
Press any key to continue.


For one thing, the original scores aren't lining up for some reason. And the other is that the average score is all jacked up. any pointers to help out the worlds worst Computer Science student?
closed account (oN3AqMoL)
For one you cant put the word OR in an if statement. The correct way to do that is put ||.

(By the way elementary school class sizes are MUCH larger than 20 now.)
Try initializing sum to zero.
double sum = 0.0, avg;

As far as your alignment being jacked up, take a look at the loop that prints all the original scores (superfluous whitespace removed):
1
2
3
4
5
cout<<"Original Scores: ";
for(i=0;i<count;i++)
{
    cout<<" " <<scores[i]<<endl;
}


You should probably print a new line after "Original Scores: ", and if you want all your scores in the same row, don't say endl after you print every score. Try this:
1
2
3
4
5
cout<<"Original Scores: " << endl;
for(i=0;i<count;i++)
{
    cout <<scores[i] << " ";
}
Last edited on
Topic archived. No new replies allowed.