C++: Write a program that reads a file consisting of students test scores in the
ID: 3913050 • Letter: C
Question
C++: 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.)]
needs to be compiled
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int x;
ifstream inf;
inf.open("test.txt");
int count[8] = {0};
// incrementing the count by 1 each time
char y;
while (inf >> x) {
if(x/24 == 8)
count[7]++;
else
count[x/24]++;
inf >> y;
}
inf.close();
// printing output
int first = 0;
for(int i=0; i<7; i++)
{
cout << first << "-" << first+24 << " = " << count[i] << endl;
first += 25;
}
cout << first << "-" << 200 << " = " << count[7] << endl;
first += 25;
return 0;
}
/*SAMPLE OUTPUT
0-24 = 1
25-49 = 2
50-74 = 0
75-99 = 5
100-124 = 2
125-149 = 2
150-174 = 6
175-200 = 8
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.