Reputation: 391
I'm working on a random exercise from my C++ book because I'm "re-learning" C++, but I'm getting some odd output from a program I wrote. I'm fairly certain there are no errors in the logic of the program, but where the sum of the elements in the "scoreCount" array should be 26, the same as the length of the scores array, it is only 20. I can't figure out what happened to the other 6 elements. The description of the exercise is in the code below. Can anyone spot what I might have done wrong?
/* Exercise 09 - 04
Write a program that reads a file consisting of students' test scores
in the range 0-200. It should then determine the number of students having
scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99,
100-124, 125-149, 150-174, and 175-200. Output the score ranges and the
number of students. (Run your program with the following input data:
76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175,
150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.) */
#include <cstdio>
int main(int argc, char ** argv) {
int scores[] = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189,
167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
35, 157, 189};
int size = sizeof(scores) / sizeof(scores[0]);
int scoreCount[] = {0, 0, 0, 0, 0, 0, 0, 0};
printf("Number of Scores: %d\n\n", size);
for(int i = 0; i < size; i++) {
scoreCount[((int)(scores[i]/25))] += 1;
printf("%d - scoreCount Index: %d\n", i, ((int)(scores[i]/25)));
}
printf("\n");
int low = 0;
int high = 24;
size = sizeof(scoreCount) / sizeof(scoreCount[0]);
for(int i = 0; i < size; i++) {
printf("Range %d-%d: %d\n", low, high, scoreCount[i]);
low += 25;
high += 25;
if(high == 199) high = 200;
}
int sum = 0;
for(int i = 0; i < size; i++) {
sum += scoreCount[i];
}
if(sum < 26) printf("\n%d: Wrong number of scores counted.\n", sum);
else printf("\nAll students accounted for.\n");
return 0;
}
Thanks for any help!
Upvotes: 1
Views: 316
Reputation: 6695
scoreCount[((int)(scores[i]/25))] += 1;
This would produce 8 if score[i] is 200. And currently your code does not handle this.
Upvotes: 1
Reputation: 9380
The number of elements should be 9 rather than 8 in the scoreCount array.
As 200/25 will be evaluated to 8 and it results in index out of bound
EDIT:
As suggested by Andrew_CS, the element having value as 200 shoud be added to the last group itself.
scoreCount[(scores[i]/25)==8?7:(scores[i]/25)] += 1;
Upvotes: 1
Reputation: 2562
200/25 = 8 is out of bounds for scoreCount - only has indexes 0 - 7.
I personally wouldn't increase the number of elements in scoreCount since there are only 8 groups of valid scores. I would instead check for this one case that produces 8 as an index.
for(int i = 0; i < size; i++){
int theIndex = (scores[i]/25);
if(theIndex == 8)
scoreCount[7] += 1;
else
scoreCount[theIndex] += 1;
printf("%d - scoreCount Index: %d\n", i, theIndex;
}
Upvotes: 0
Reputation: 393547
Here's proper C++ style. I know the question has already been answered, but here's a bonus:
See it live on http://ideone.com/rBTi42
#include <vector>
#include <map>
#include <iostream>
int main(int argc, char ** argv)
{
const std::vector<int> scores = {76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189,
167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87,
35, 157, 189
};
std::map<int, int> scoreCount;
std::cout << "Number of Scores: " << scores.size() << "\n";
for(auto score : scores)
{
scoreCount[score/25] ++;
std::cout << score << " - scoreCount Index: " << score/25 << "\n";
}
for(auto const& slot : scoreCount)
{
auto low = slot.first*25;
auto high = low+24;
std::cout << "Range " << low << "-" << high << ": " << slot.second << "\n";
}
int sum = 0;
for(auto const& slot : scoreCount)
sum += slot.second;
if(sum < 26)
printf("\n%d: Wrong number of scores counted.\n", sum);
else
printf("\nAll students accounted for.\n");
}
Upvotes: 7
Reputation: 158559
You are generating an index out of bounds for scoreCount
here:
scoreCount[((int)(scores[i]/25))] += 1;
The valid indexes for scoreCount
are from 0 to 7
but with your current scores
you will generate indexes up to 8
. A solution would be to extend scoreCount
by one element or to check if the index is 8
and map it to index 7
since it seems like the problem limits your ranges.
Upvotes: 1
Reputation: 2101
You need to increase the number of elements in the scoreCount
array because 200/25
gives 8
and scoreCount[8]
does not exists .
Upvotes: 0
Reputation: 115
I ran the code on my machine. I got 23 out of 26.
The reason for this is that there are 3 values of 200. When the value is 200 the it will be under 8.
Upvotes: 0