Arrays and Vectors

Hello everyone, I was having a little bit of trouble with my program. The purpose of the program is to have a user enter up to 50 test scores from 0-100. If the user enters a negative number or a number greater than 100, it skips it. That's the part I seem to be having trouble with. If I enter the following input:
14 21 46 31 -1 1006 18 Q
The program doesn't properly output as it should. The program should skip the out of bounds numbers and count the 18.
Code:
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <cstdlib>

int readArray(int, int[]);
int stat(int, const int[], int&, int&, int&);
int histogram(int, const int[], int[]);

int main()
{
 
  const int MAX_SCORE = 50;
  int score[MAX_SCORE];
  int nscores = readArray(MAX_SCORE, score);

  int minscore, maxscore, avgscore;
  if (stat(nscores, score, minscore, maxscore, avgscore) == 0)
  {
    cout << "Min=" << minscore << endl;
    cout << "Max=" << maxscore << endl;
    cout << "Average=" << avgscore << endl;
    int grade[5] = {};
    histogram(nscores, score, grade);
    cout << "A's: " << grade[0] << endl;
    cout << "B's: " << grade[1] << endl;
    cout << "C's: " << grade[2] << endl;
    cout << "D's: " << grade[3] << endl;
    cout << "F's: " << grade[4] << endl;
  }
  else
  {
    cout << "no data" << endl;
  }
}

int readArray(int MAX_SCORE, int score[])
{
  int nscores = 0;
  char buf[100];

  cout << "Enter up to 50 scores, separated by spaces\n";
  cout << "Enter a Q after the last score is entered\n";

    for(int i = 0; i < MAX_SCORE; i++)
    {
      cin >> buf;
      score[i] = atoi(buf);

      if (score[i] <= 0 || score[i] >= 100)
      break;
      if (score[i] == 'Q' || score[i] == 'q')
      {
          continue;
      }
      else
        nscores++;
    }
  return nscores;
}

int stat(int nscores, const int score[], int& minscore, int& maxscore, int& avgscore)
{
  minscore = score[0];
  maxscore = score[0];
  avgscore = 0;
  if(nscores != 0)
  {
  for (int J = 0; J < nscores; J++)
    avgscore +=  score[J];
    avgscore /= nscores;
    for (int i = 0; i < nscores; i++ )
    {
      if (minscore > score[i])
      {
      minscore = score[i];
      }
      if (maxscore < score[i])
      {
      maxscore = score[i];
      }
    }
  return 0;
  }
  else return 1;
}

int histogram(int nscores, const int score[], int grade[])
{
 for (int i = 0; i < nscores; i++)
 {
     if (score[i] >= 90)
     {
        grade[0] += 1;
     }
     if (score[i] > 80 && score[i] < 90)
     {
         grade[1] += 1;
     }
     if (score[i] > 70 && score[i] < 80)
     {
         grade[2] += 1;
     }
     if (score[i] > 60 && score[i] < 70)
     {
         grade[3] += 1;
     }
     if (score[i] > 0 && score[i] < 60)
     {
         grade[4] += 1;
     }
 }
 return 0;
}
Last edited on
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
int readArray(int MAX_SCORE, int score[])
{
  int nscores = 0;
  char buf[100];

  cout << "Enter up to 50 scores, separated by spaces\n";
  cout << "Enter a Q after the last score is entered\n";

    for(int i = 0; i < MAX_SCORE;) //note <- no i  here
    {
      cin >> buf;
      score[i] = atoi(buf);

      if (score[i] < 0 || score[i] > 100) { //note <- I removed the "=" because its possible to get 100 & 0
      {
          std::cout<<"#: "<<i<<" was skipped.\n";
          continue;
      }
      else i++;

      if (buf[i] == 'Q' || buf[i] == 'q') //note <- changed to buf
      {
          continue;
      }
      else
        nscores++;
    }
  return nscores;
}
Last edited on
That didn't seem to work. The purpose of this program is to average numbers between 0-100, give their average, their max, their min, and the number of As, Bs, Cs, Ds, Fs. The problem with my program is that it it calculates maximum minimum average and the number of As, Bs, Cs, Ds, Fs correctly, however any valid entry after an out of bounds number that is inputted is not being counted. For example if I run the program with my original code, and input the following: 11 22 33 44 44444 -4444444 55 66 77 88 99 x q Q .
The output is :

Enter up to 50 scores, separated by spaces
Enter a Q after the last score is entered
11 22 33 44 44444 -4444444 55 66 77 88 99 x q Q
Min=11
Max=44
Average=27
A's: 0
B's: 0
C's: 0
D's: 0
F's: 4

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

It is outputting the first four numbers correctly, then it skips the out of bounds numbers.
The program is supposed to count the 55 66 77 88 and 99 but it doesn't for some reason.
Here is your code:

for(int i = 0; i < MAX_SCORE; i++)
{
cin >> buf;
score[i] = atoi(buf);

if (score[i] <= 0 || score[i] >= 100)
break;
if (score[i] == 'Q' || score[i] == 'q')
{
continue;
}
else
nscores++;
}
return nscores;


The problem is the break after your first if statement. The break will exit out the entire for loop instead of just skipping the number. You could say

if(score[i]<0 || score[i]>100);

By just putting a semicolon after the if statement you actually make it so that it does nothing if it encounters an invalid entry, whereas a break will make the function break out of the for loop.
I found a few errors:
1. try to execute following code:
1
2
3

  cout << "Q is " << atoi("Q") << endl;

2. As has noted manutd636, there is a break after your first if statement

My opinion:
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

int readArray(int MAX_SCORE, int score[])
{
  int nscores = 0;
  char buf[100];
  cout << "Enter up to 50 scores, separated by spaces\n";
  cout << "Enter a Q after the last score is entered\n";

    for(int i = 0; i < MAX_SCORE; i++)
    {
      cin >> buf;
      
      // remember ...
      int temp = atoi(buf);
      

      if (temp > 0 && temp < 100)
      {
		// save ...
                score[nscores] = temp;
		nscores++;
      }
     // exit condition 
     if (temp == 0)
        break;
      
        
    }
  return nscores;
}




Enter up to 50 scores, separated by spaces
Enter a Q after the last score is entered
14 21 46 31 -1 1006 18 0
Min=14
Max=46
Average=26
A's: 0
B's: 0
C's: 0
D's: 0
F's: 5
Last edited on
Thanks for the help everyone!

I tried @alptp ' s code, and the program worked, but it counted any character as breaking the statement. My instructor wants it so that the program breaks on the user entering Q or q only.
How would I make it so that I can have the user enter Q or q to break as well as including scores within the range, in the same direction @alptp wrote it in. So it would be:
14 21 46 31 -1 1006 18 0
and a Q or q at the end so it would break only with these two letters.
try to do it

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
int readArray(int MAX_SCORE, int score[])
{
  int nscores = 0;
  char buf[100];
  cout << "Enter up to 50 scores, separated by spaces\n";
  cout << "Enter a Q after the last score is entered\n";

    
    
    for(int i = 0; i < MAX_SCORE; i++)
    {
      cin >> buf;
      
     // exit condition 
     if (char(*buf) == 'Q' || char(*buf) == 'q' ) break;
       
      // remember ...
      int temp = atoi(buf);
      

      if (temp > 0 && temp < 100)
      {
		// save ...
                score[nscores] = temp;
		nscores++;
      }
  
        
    }
  return nscores;
}
Topic archived. No new replies allowed.