Static Arrays... program issues

Pages: 12
Hello, I am having a problem with a program I am tying to wright. It tells me that I have to:

Create a 1-D Array to hold a set of exam scores:
55 74 93 84 64 72 69 78 87 84 72 33 83 68 62 51 88 90

Write a program to do the following tasks:

1. Display scores in rows of four(4) scores.

2. Calculate average score and display.

3. Find Lowest score and display.

4. Find Highest score and display.

1. How would I go about doing this would I use the "string" function or is there an easer why you can think of?

2. I thought I did every thing right for the average but it comes out to "3" witch obviously is not right (it should be 66.94)

3. I found the lowest score just fine :)

4. But some how I can not get the highest number to be right.

any help, pointers or suggestions etc. would be great! Thanks!


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
 //  main.cpp
//  Program 6
//  Created by William Blake Harp on 7/10/14.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int exam_scores[] = {55, 74, 93, 84, 64, 72, 69, 78, 87, 84, 72, 33, 83, 68, 62, 51, 88, 90};
    int const SIZE = sizeof(exam_scores)/sizeof(exam_scores[0]);
    int sum_of_exam_scores;
    double average;
    int count;
    int highest_exam_score;
    int lowest_exam_store;
    
    for (sum_of_exam_scores = 0; sum_of_exam_scores != SIZE; sum_of_exam_scores++)
    {
        sum_of_exam_scores += exam_scores[0];
        average = sum_of_exam_scores / SIZE;
        
        cout << "The average of the exam scores are: " << average << endl;
    
        break;
    }
    
    highest_exam_score = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] > highest_exam_score)
        {
            highest_exam_score = exam_scores[count];
            
            cout << "The highest exam score is: " << highest_exam_score << endl;
            break;
        }
    }
    
    lowest_exam_store = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] < lowest_exam_store)
        {
            lowest_exam_store = exam_scores[count];
            
            cout << "The lowest exam score is: " << lowest_exam_store << endl;
            break;
        }
        
    }
Well, you cannot calculate the average inside the for loop. That's because first of all the program needs to accumulate the total of all of the scores added together. The loop simply needs to iterate through all of the elements and add each to the total. The total needs to be set to zero before the loop begins.

None of the loops should have a break statement. Again, the loop needs to iterate through all of the items.
Last edited on
ok here is what I have .....

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
//  main.cpp
//  Program 6
//  Created by William Blake Harp on 7/10/14.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int exam_scores[] = {55, 74, 93, 84, 64, 72, 69, 78, 87, 84, 72, 33, 83, 68, 62, 51, 88, 90};
    int const SIZE = sizeof(exam_scores)/sizeof(exam_scores[0]);
    int sum_of_exam_scores;
    double average;
    int count;
    int highest_exam_score;
    int lowest_exam_store;
    
    for (sum_of_exam_scores = 0; sum_of_exam_scores != SIZE; sum_of_exam_scores++)
    {
    }
    
    sum_of_exam_scores += exam_scores[0];
    average = sum_of_exam_scores / SIZE;
    
    cout << "The average of the exam scores are: " << average << endl;
    
    highest_exam_score = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] > highest_exam_score)
        {
            highest_exam_score = exam_scores[count];
            
            cout << "The highest exam score is: " << highest_exam_score << endl;
        }
    }
    
    lowest_exam_store = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] < lowest_exam_store)
        {
            lowest_exam_store = exam_scores[count];
            
            cout << "The lowest exam score is: " << lowest_exam_store << endl;
        }
        
    }
    
    
    return 0;
}//End Code! 




but I am getting this when it displays. Why it it giving me one wrong and one right answer for highest exam score? and my average is now 4 :) wow I am doing something why wrong! any ideas?

The average of the exam scores are: 4
The highest exam score is: 74
The highest exam score is: 93
The lowest exam score is: 33
This loop is wrong for several reasons:
1
2
3
    for (sum_of_exam_scores = 0; sum_of_exam_scores != SIZE; sum_of_exam_scores++)
    {
    }

First, it is using the name sum_of_exam_scores where I would expect to see a simple count or i. Second, it doesn't access any of the array elements (it's supposed to add each of them to the total).

The cout statements for highest and lowest should be placed after the end of the corresponding loop, not inside the loop.
ok I fixed the highest and the lowest score as you can see below but I am sorry I still do not get how I would access the array elements. could you show me or explain I am hoping to learn ;)

The average of the exam scores are: 4
The highest exam score is: 93
The lowest exam score is: 33


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
//  main.cpp
//  Program 6
//  Created by William Blake Harp on 7/10/14.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int exam_scores[] = {55, 74, 93, 84, 64, 72, 69, 78, 87, 84, 72, 33, 83, 68, 62, 51, 88, 90};
    int const SIZE = sizeof(exam_scores)/sizeof(exam_scores[0]);
    int sum_of_exam_scores;
    double average;
    int count;
    int highest_exam_score;
    int lowest_exam_store;
    
    for (count = 0; count != SIZE; count++)
    {
    }
    
    sum_of_exam_scores += exam_scores[0];
    average = sum_of_exam_scores / SIZE;
    
    cout << "The average of the exam scores are: " << average << endl;
    
    highest_exam_score = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] > highest_exam_score)
        {
            highest_exam_score = exam_scores[count];
        }
    }
    
    cout << "The highest exam score is: " << highest_exam_score << endl;
    
    lowest_exam_store = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] < lowest_exam_store)
        {
            lowest_exam_store = exam_scores[count];
        }
    }
    
    cout << "The lowest exam score is: " << lowest_exam_store << endl;
    
    
    return 0;
}//End Code! 
Last edited on
I still do not get how I would access the array elements

perhaps it's just the words or terminology which you don't get. You're already accessing the array elements in two of your three loops.

This is how you'd typically iterate through all of the array elements:
1
2
3
4
    for (int i = 0; i <  SIZE; i++)
    {
        cout << exam_scores[i] << ' ';
    }

You just need to, instead of printing out the array element, add it to the total.

(I used i rather than count here,that doesn't matter, count would be ok, but you will very often see letters i, j, k used in this way, probably for historical reasons).
oh gosh I can really over look things like this sorry! I see now! thanks for all your help it worked great! Here is the out come and the code!

The average of the exam scores are: 58.06
The highest exam score is: 93
The lowest exam score is: 33

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
//  main.cpp
//  Program 6
//  Created by William Blake Harp on 7/10/14.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int exam_scores[] = {55, 74, 93, 84, 64, 72, 69, 78, 87, 84, 72, 33, 83, 68, 62, 51, 88, 90};
    int const SIZE = sizeof(exam_scores)/sizeof(exam_scores[0]);
    double sum_of_exam_scores;
    double average;
    int count;
    int highest_exam_score;
    int lowest_exam_store;
    
    for (count = 0; count != SIZE; count++)
    {
        sum_of_exam_scores += exam_scores[0];
    }
    
    sum_of_exam_scores += exam_scores[0];
    average = sum_of_exam_scores / SIZE;
    
    cout << setprecision(2) << fixed;
    cout << "The average of the exam scores are: " << average << endl;
    
    highest_exam_score = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] > highest_exam_score)
        {
            highest_exam_score = exam_scores[count];
        }
    }
    
    cout << "The highest exam score is: " << highest_exam_score << endl;
    
    lowest_exam_store = exam_scores[0];
    
    for (count = 1; count != SIZE; count++)
    {
        if (exam_scores[count] < lowest_exam_store)
        {
            lowest_exam_store = exam_scores[count];
        }
    }
    
    cout << "The lowest exam score is: " << lowest_exam_store <<  endl;
    
    return 0;
}//End Code! 
Why do you have exam_scores[0] at line 22? What do you think it should actually be?

Also, why is line 25 there, after the end of the loop?
 
sum_of_exam_scores += exam_scores[0];


By the way the average isn't 58.06 (nor is it 66.94).
Last edited on
is it ?sum_of_exam_scores += exam_scores[count];

which would make the average:

The average of the exam scores are: 75.67
Last edited on
I tried this several ways (including using Google as a calculator). I get the average to be 72.61
That is odd... I do not see how the code could be wrong I thought I fixed it all!?
Did you read my previous post:
Also, why is line 25 there, after the end of the loop?
 
sum_of_exam_scores += exam_scores[0];



You can calculate the average here:
http://www.calculatorsoup.com/calculators/statistics/average.php
Last edited on
that was my problem I for got to delete that line after I copied and pasted back into the loop now I get :

The average of the exam scores are: 72.61
The highest exam score is: 93
The lowest exam score is: 33

which it now correct! Thanks!

I almost forgot I have to:

1. Display scores in rows of four(4) scores.

1. How would I go about doing this would I use the "string" function or is there an easer why you can think of?
Last edited on
Display scores in rows of four(4) scores.


There's no need to bring "string" into this - I can't see how that would do anything other than make things more complicated.

Take another look at this code which I posted earlier:
1
2
3
4
    for (int i = 0; i <  SIZE; i++)
    {
        cout << exam_scores[i] << ' ';
    }

It prints all of the scores on one line. All you have to do is to print a newline after every 4th number. How would you do that? Well. it needs bit of thought, but start by thinking of the value of i % 4 (or count % 4 in your code), how would the result of that expression change on each pass through the loop.
ok so I think this why would work beast then

1
2
3
4
5
6
7
8
9
10
11
cout << "The exam scores are listed below." << endl
    << "---------------------------------" << endl;
    
    while (int i = 0 && i < SIZE && i++)
    {
            for (int i = 0; i < 4; i++)
            {
                cout << exam_scores[i] << ' ';
            }
    }


however when I added the while loop it stopped working what do you suggest. I different loop maybe?
I think with some modification, your code could be made to work. One problem is that you declare int i in two places. This is a local variable, it exists only within the loop. But the variable i in the innermost loop is a completely separate variable which "hides" the outer one. Give the variables different names if you want to follow this route. Or better, declare i only once and be careful how and when you modify it.

Still, I was thinking in terms of a single loop.
consider this code:
1
2
3
4
    for (int count=0; count<SIZE; count++)
    {
        cout << exam_scores[count] << "  " << count % 4 << endl;
    }

Output:
55  0
74  1
93  2
84  3
64  0
72  1
69  2
78  3
87  0
84  1
72  2
33  3
83  0
68  1
62  2
51  3
88  0
90  1


Do you see a pattern in the numbers on the right hand column?
Yes I like the one loop approach. But I was playing around with it. And I couldn't get the output to put four numbers on one line. Would you do multiple cout statements for this to work. Or is there something to add to the original cout statement to tell the for loop to endline every four entries.
Last edited on
The code I most recently posted was not the actual code required. Rather it was something to think about. Once you start playing with ideas, you can experiment and home in on a solution.

In this case I would suggest adding an if statement inside the loop. When a suitable condition is true, output the newline (either '\n' or endl).

And I couldn't get the output to put five numbers on one line
Oh, I thought you needed to put four numbers on one line.
Last edited on
ok I will work on that! And yes four per line. (I edited my post)
Last edited on
would it be useful or more work to make it a if / else statement or just an if?

Also I can not get the code right that tells the compiler, every four numbers "\n" I am missing the logic here. I know what I need to do just not how to tell the compiler in a why it understands...
Pages: 12