This program should do the following: --Write a program that read a file consist
ID: 3631997 • Letter: T
Question
This program should do the following:
--Write a program that read a file consisting of students test scores in the range of 0-200.
--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,16 7,200,175,150,87,99,129,149,176,200,87,35,157,189.
My comments:
I have the coding done, but cannot make the program output any information.
Can you correct the program so that it gives an output indicated above?
Thank you.
#include
#include
#include
using namespace std;
const int maxCategory = 7;
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);
int main()
{
int category;
int score;
int student[maxCategory];
ifstream infile;
infile.open("score.txt");
if(!infile)
{
cout << "Cannot open input file." << endl;
system("pause");
return 0 ;
}
initialize(student);
infile>>score;
while (!infile.eof())
{
category = getCategory(score);
getStudent(category, student);
infile>>score;
}
infile.close();
printArray(category, student);
system("pause");
return 0;
}
void initialize(int student[])
{
int j;
for(j=0; j<=maxCategory; j++)
{
student[j]=0;
}
}
void getStudent(int c, int s[])
{
if (c<=maxCategory)
{
s[c]++;
}
}
void printArray(int category, int student[])
{
string scoreRange;
cout << "Score Range" << " ";
cout << "Number of Students" << endl;
for (category=0; category<=maxCategory; category++)
{
switch(category)
{
case 0:
scoreRange="0-24";
break;
case 1:
scoreRange="25-49";
break;
case 2:
scoreRange="50-74";
break;
case 3:
scoreRange="75-99";
break;
case 4:
scoreRange="100-124";
break;
case 5:
scoreRange="125-149";
break;
case 6:
scoreRange="150-174";
break;
case 7:
scoreRange="175-200";
break;
}
cout << scoreRange <<" ";
cout << student[category]<< endl;
}
}
int getCategory(int score)
{
int scoreRange;
if (score>=0 && score<25)
scoreRange=0;
else if (score>=25 && score<50)
scoreRange=1;
else if (score>=50 && score<75)
scoreRange=2;
else if (score>=75 && score<100)
scoreRange=3;
else if (score>=100 && score<125)
scoreRange=4;
else if (score>=125 && score<150)
scoreRange=5;
else if (score>=150 && score<175)
scoreRange=6;
else if (score>=175 && score<=200)
scoreRange=7;
else
cout <
return scoreRange;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int maxCategory = 8; //You have 8 categories (0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200) so why did you put 7?
int getCategory(int);
void initialize(int student[]);
void getStudent(int category, int student[]);
void printArray(int category, int student[]);
int main()
{
int category;
int score;
int student[maxCategory]; //should be array of 8 ints instead of 7 as you defined maxCategory...
ifstream infile;
infile.open("score.txt");
if(!infile)
{
cout << "Cannot open input file." << endl;
system("pause");
return 0;
}
initialize(student);
infile>>score;
while (!infile.eof())
{
category = getCategory(score);
getStudent(category, student);
infile>>score;
}
infile.close();
printArray(category, student);
system("pause");
return 0;
}
void initialize(int student[])
{
int j;
for(j = 0; j < maxCategory; j++) //for array index range from 0 to (elements - 1), so j should be run from 0 to 7 and does not have to equal maxCategory, just put j < maxCategory
{
student[j] = 0;
}
}
void getStudent(int c, int s[])
{
if (c < maxCategory) //same here does not need c <=
{
s[c]++;
}
}
void printArray(int category, int student[])
{
string scoreRange;
cout << "Score Range" << " ";
cout << "Number of Students" << endl;
for (category = 0; category < maxCategory; category++) //here too
{
switch(category)
{
case 0:
scoreRange="0-24";
break;
case 1:
scoreRange="25-49";
break;
case 2:
scoreRange="50-74";
break;
case 3:
scoreRange="75-99";
break;
case 4:
scoreRange="100-124";
break;
case 5:
scoreRange="125-149";
break;
case 6:
scoreRange="150-174";
break;
case 7:
scoreRange="175-200";
break;
}
cout << scoreRange << " ";
cout << student[category] << endl;
}
}
int getCategory(int score)
{
int scoreRange;
if (score>=0 && score<25)
scoreRange=0;
else if (score>=25 && score<50)
scoreRange=1;
else if (score>=50 && score<75)
scoreRange=2;
else if (score>=75 && score<100)
scoreRange=3;
else if (score>=100 && score<125)
scoreRange=4;
else if (score>=125 && score<150)
scoreRange=5;
else if (score>=150 && score<175)
scoreRange=6;
else if (score>=175 && score<=200)
scoreRange=7;
else
cout << "Out of range 0-200 "; //it should print some error message, i guess you missed it when copied the code
return scoreRange;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.