In: Computer Science
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.
SOLUTION:
Dear Student, I am pasting the code with comments of above query and also attaching it's snapshots .
PROGRAM:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//declaring variables
ifstream file("marks.txt"); // open the marks.txt file
double marks[26];
int i, st_num=0;
int scr0_24=0, scr25_49=0, scr50_74=0, // initializing variables to
0
scr75_99=0, scr100_124=0, scr125_149=0,
scr150_174=0, scr175_200=0;
//initializing marks in array
for(i=0; i<26; i++)
{
marks[i] = 0;
}
//taking marks from the marks.txt file
for(i=0; i<26; i++)
{
file >> marks[i];
st_num++;
}
file.close(); // now close the file
//finding number of people for each score range
for(i=0; i<26; i++)
{
if(marks[i] <= 24)
scr0_24++;
if(marks[i] >= 25 && marks[i] <= 49)
scr25_49++;
if(marks[i] >= 50 && marks[i] <= 74)
scr50_74++;
if(marks[i] >= 75 && marks[i] <= 99)
scr75_99++;
if(marks[i] >= 100 && marks[i] <= 124)
scr100_124++;
if(marks[i] >= 125 && marks[i] <= 149)
scr125_149++;
if(marks[i] >= 150 && marks[i] <= 174)
scr150_174++;
if(marks[i] >= 175 && marks[i] <= 200)
scr175_200++;
}
// Now print the result in particular range
cout << "Number of students: " << st_num <<
endl;
cout << "0-24: " << scr0_24 << endl;
cout << "25-49: " << scr25_49 << endl;
cout << "50-74: " << scr50_74 << endl;
cout << "75-99: " << scr75_99 << endl;
cout << "100-124: " << scr100_124 << endl;
cout << "125-149: " << scr125_149 << endl;
cout << "150-174: " << scr150_174 << endl;
cout << "175-200: " << scr175_200 << endl;
return 0;
}
SNAPSHOTS:
TEXT FILE "marks.txt":
OUTPUT: