Static Arrays... program issues

Pages: 12
Well, could you show the code for your latest attempt please. Sorry if I appear a bit unhelpful, it's just that sometimes we all go through this same process, and it's part of learning programming. For example, we may get stumped by something, then perhaps after sleeping on it, wake up with a new idea to try. Not that it should require a whole night's sleep for every line of code we write ;) of course.
ok! I am getting closer here is my code however I can not get it exactly right

1
2
3
4
5
6
7
8
9
10
11
12
for (int count = 0; count != SIZE; count++)
    {
        if (exam_scores[count] % 5)
        {
            cout << exam_scores[count] << " ";
        }
        else
        {
            cout << endl;
        }
        count++;
    }


And no you are being a great help! I am leaning a lot just needed to think about how I would do this for a bit.
You're making an out of bounds reference to exam_scores.

Let's assume SIZE is 20. Your loop will iterate 21 times and attempt to reference exam_scores[20], which is out of bounds. Your valid scores are [0]-[19].

The correct for loop is:
1
2
 
for (int count = 0; count < SIZE; count++)


Note: That is an underline to highligt the difference. Not a <= condition.
Last edited on
Also, if SIZE is odd, it would give an infinite loop, since the count != SIZE condition would always be true.
ok so I must still be missing something them because the code I have is not doing it. Its close but not quite. So I think that my If/ else is wrong some were. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 for (int count = 0; count < SIZE; count++)
    {
        if (exam_scores[count] % 5)
        {
            cout << exam_scores[count] << " ";
        }
        else
        {
            cout << endl;
        }
        count++;
    }
            
   cout << endl << endl;


also you typed < with _ under it how do you do that is that my problem? I did not know you could do that....
Last edited on
So I think that my If/ else is wrong some were

A couple of points (there are actually more than two things wrong)
1. because of the way the if statement is written, not all elements of exam_scores will be output.
2. because count is incremented at both line 1 and line 11, it goes up in steps of 2, thus giving a second reason why some elements will not be output.

did you want a third point?
3. the if condition is dependent upon the contents of the array, thus the printing of the output could change depending upon how the students performed in the exam.
OK! I got the 1st four numbers on a line now just got to find out how to do that for all the numbers



1
2
3
4
5
6
7
8
9
10
11
12
for (int count = 0; count < SIZE; count++)
    {
        if (count < 4)
        {
            cout << exam_scores[count] << " ";
        }
        else
        {
            cout << endl;
        }
        
    }
There's still the same problem as in earlier attempts, the if statement will:
either
print an array element
or
print a newline

However, the array element should always be printed, otherwise some content will be omitted.

The if statement should control only the output (or non-output ) of the newline.
yea I am stumped. I understand what your saying and have tried all kinds of things just not working....
It's getting late now in my part of the world, so just a brief answer before I go. Maybe see this previous post:
http://www.cplusplus.com/forum/beginner/137937/#msg731602
don't just copy+paste as it obviously doesn't do the right thing. But there's a very big hint there.

EDIT Since I'm not sure where you currently stand on this, here's another hint, in fact its very nearly a complete solution:
either of these could work, though the behaviour is subtly different:
1
2
3
4
5
6
7
8
9
    for (int count = 0; count < SIZE; count++)
    {

        if (condition)
            cout << endl;

        cout << exam_scores[count] << " ";
        
    }


Or:
1
2
3
4
5
6
7
8
9
    for (int count = 0; count < SIZE; count++)
    {

        cout << exam_scores[count] << " ";

        if (condition)
            cout << endl;        

    }


All that needs to be done is to replace condition with suitable code - this can be as simple or as complicated as you like.
Last edited on
ok so... I am moving in the right direction and you might think that I am a an a complete noon at this which I am (this is my 5th week programing ;)) but here is what I have. It works, but it is hard coded so to speak. I have to find a why to get it to repeat.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (count = 0; count < SIZE; count++)
    {
        if (count == 4)
            cout << endl;
        if (count == 8)
            cout << endl;
        if (count == 12)
            cout << endl;
        if (count == 16)
            cout << endl;
        cout << exam_scores[count] << " ";
        
    }


here is the out put just FYI:

55 74 93 84
64 72 69 78
87 84 72 33
83 68 62 51
88 90
Last edited on
Yes, that is a sort of solution.

Now imagine that your array, instead of containing 18 elements, contained a million, or a billion elements.

How could you modify your solution so that it would still work - without writing millions of separate if statements.
ok! am I right in saying that in order for this to work I must?

1. Use %
2. Use multiple statements in my if condition

also you typed < with _ under it how do you do that is that my problem?

I underlined the < to highlight what i had changed. I didn't think about it appearing as a <= symbol in a single character.
1. Use %
2. Use multiple statements in my if condition

1. yes.
2. perhaps, but it may depend on what you are meaning by "multiple statements".

if you mean something like this: if ((cond1) && (cond2) || (cond3)) then definitely not, it's very much simpler than that.
Last edited on
ok I will use %. and yes that is what I meant. ok much simpler then that... wow I must be blind.
@ Chervil && @ AbstractionAnon

Thank you so much I Finally got it to work! You two were great help! I have learned more this week then any other! I can not think you two enough! Here is my finished code just to show you that every thing worked:

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//  main.cpp
//  Program 6
//  Created by William Blake Harp on 7/10/14.

#include <iostream>
#include <iomanip>
#include <cmath>

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;
    double standard_deviation;
    int deviation = 0;
    
    cout << "The exam scores are listed below." << endl
         << "---------------------------------" << endl;

    // Printing out the exam scores in rows of four.
    for (count = 0; count < SIZE; count++)
    {
        if ((count % 4) == 0)
            cout << endl;
        
        cout << exam_scores[count] << ", ";
    }
    cout << endl << endl;
    
    // Finding the average.
    for (count = 0; count != SIZE; count++)
    {
        sum_of_exam_scores += exam_scores[count];
    }
    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];
    
    // Finding the highest exam score.
    for (count = 0; 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];
    
    // Finding the lowest exam score.
    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;
    
    // finding the deviation of each score.
    cout << "The deviation of each score is listed below:" << endl << endl;
    cout << "Score:  -----  Deviation:";
    for (count = 0; count < SIZE; count++)
    {
        if (count != SIZE)
            deviation = exam_scores[count] - average;
            
            cout << endl;
        
            cout << setw(3) << fixed;
            cout << exam_scores[count] << "       |       " << deviation << endl;
            cout <<                       " --------------------";
    }
    cout << endl << endl;
    
    // Finding the standard deviation.
    standard_deviation = sqrt(pow(exam_scores[count] - average, 2.0) / SIZE);
    cout << "The standard deviation is: " << standard_deviation << endl << endl;
    
    // Finding how meany scores are with in one standard deviation: 55.5 OR 89.72
    cout << "The exam scores that are with in one standard devation are:" << endl << endl;
    
    for (count = 0; count < SIZE; count++)
    {
        if (exam_scores[count] >= 55.5 && exam_scores[count] <= 89.72)
        {
            cout << exam_scores[count] << ", ";
        }
    }
    
    
    return 0;
}//End Code! 


and here is the out put:

The exam scores are listed below.
---------------------------------

55, 74, 93, 84,
64, 72, 69, 78,
87, 84, 72, 33,
83, 68, 62, 51,
88, 90,

The average of the exam scores are: 72.61
The highest exam score is: 93
The lowest exam score is: 33
The deviation of each score is listed below:

Score: ----- Deviation:
55 | -17
--------------------
74 | 1
--------------------
93 | 20
--------------------
84 | 11
--------------------
64 | -8
--------------------
72 | 0
--------------------
69 | -3
--------------------
78 | 5
--------------------
87 | 14
--------------------
84 | 11
--------------------
72 | 0
--------------------
33 | -39
--------------------
83 | 10
--------------------
68 | -4
--------------------
62 | -10
--------------------
51 | -21
--------------------
88 | 15
--------------------
90 | 17
--------------------

The standard deviation is: 17.11

The exam scores that are with in one standard devation are:

74, 84, 64, 72, 69, 78, 87, 84, 72, 83, 68, 62, 88,
Very pleased to hear that!
Topic archived. No new replies allowed.
Pages: 12