Question

This needs to be linux compatible in C++ code. Write a program that reads a file...

This needs to be linux compatible in C++ code.

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

Please note that the suggested data must be read from a file. Please call this file "inFile.txt".  Substitute this file by any  input file of the same format and your program should work correctly. 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable Code:

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
//ifstream object.
ifstream file;
//opens file scores.txt
file.open("inFile.txt");
//range array consists count of scores in the range
int range[8]={0};
char ch;
string st="";
//reading file character by character.
while(file >> ch){
//if character ch is ',' then we change string st to int and reset st="";
if(ch==','){
//converting string st to int.
stringstream change(st);
int x =0;
change >> x;
if(x >=0 && x<=24){ //increment range 0-24
range[0]++;
}
else if(x >=25 && x<=49){
range[1]++; //increment range 25-49
}
else if(x >=50 && x<=74){
range[2]++; //increment range 50-74
}
else if(x >=75 && x<=99){
range[3]++; //increment range 75-99
}
else if(x >=100 && x<=124){
range[4]++; //increment range 100-124
}
else if(x >=125 && x<=149){
range[5]++; //increment range 125-149
}
else if(x >=150 && x<=174){
range[6]++; //increment range 150-174
}
else if(x >=175 && x<=200){
range[7]++; //increment range 175-200
}
st="";//reseting string st to empty string.
}
else
st+=ch;
}
//converting last string st to int using stringstream
stringstream change(st);
int x =0;
change >> x;
file.close();
//checking for last score.
if(x >=0 && x<=24){ //increment range 0-24
range[0]++;
}
else if(x >=25 && x<=49){
range[1]++; //increment range 25-49
}
else if(x >=50 && x<=74){
range[2]++; //increment range 50-74
}
else if(x >=75 && x<=99){
range[3]++; //increment range 75-99
}
else if(x >=100 && x<=124){
range[4]++; //increment range 100-124
}
else if(x >=125 && x<=149){
range[5]++; //increment range 125-149
}
else if(x >=150 && x<=174){
range[6]++; //increment range 150-174
}
else if(x >=175 && x<=200){
range[7]++; //increment range 175-200
}

//printing range and with their count
int range1=0,range2=24;
for(int i=0;i<7;i++){
cout << range1 << "-" << range2 << " = " << range[i] << endl;
range1=range2+1;
range2=range1+24;
}
//printing last range 175-200 with count.
cout << range1 << "-" << range2+1 << " = " << range[7] << endl;
return 0;
}

Sample Output:

inFile.txt

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


Please don't hesitate to contact me if you have any queries. :)



Add a comment
Know the answer?
Add Answer to:
This needs to be linux compatible in C++ code. Write a program that reads a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • written in c++ Write a program that reads a file consisting of students’ test scores in...

    written in 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,...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. 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. Finally, the program outputs the total number of students, each score range...

  • Could someone help me out here assignment due tonight. Thank you. Develop and run problem 03...

    Could someone help me out here assignment due tonight. Thank you. Develop and run problem 03 page 231. Make a function to output your results. Read the following data into your program from a text file that you make. (Use 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, and 189.) Hint: to make a file use notepad++ input and...

  • This is a revision of a previous exercise, here is the question: Perform exercise 4 at...

    This is a revision of a previous exercise, here is the question: Perform exercise 4 at the end chapter 8 of your textbook using dynamic arrays. (This time the program must use dynamic arrays). I have regular array version posted below. 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,...

  • C++: Create a grade book program that includes a class of up to 20 students each...

    C++: Create a grade book program that includes a class of up to 20 students each with 5 test grades (4 tests plus a Final). The sample gradebook input file (CSCI1306.txt) is attached. The students’ grades should be kept in an array. Once all students and their test scores are read in, calculate each student’s average (4 tests plus Final counts double) and letter grade for the class. The output of this program is a tabular grade report that is...

  • Consider the compressive strength data 6-2. What propotion of thespecimens exhibit compressive strength of at least...

    Consider the compressive strength data 6-2. What propotion of thespecimens exhibit compressive strength of at least 1379 KN/m^2? ■ TABLE 62 Compressive Strength (in psi) of 80 Aluminum-Lithium Alloy Specimens 105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 121 120 181 160 194 184 165 145 160 150 181 168 158 208...

  • Please help me with this problem urgently. Please provide the solution both mathematically and in R...

    Please help me with this problem urgently. Please provide the solution both mathematically and in R code 3.21 The data in Table 3.8 consist of head measurements on first and second sons (Frets 1921). Define yi and y2 as the measurements on the first son and xi and 2 for the second son. (a) Find the mean vector for all four variables and partition it into ( as in (3.41). (b) Find the covariance matrix for all four variables and...

  • C++ program that reads students' names followed by their test scores (5 scores) from the given...

    C++ program that reads students' names followed by their test scores (5 scores) from the given input file, students.txt. The program should output to a file, output.txt, each student's first name, last name, followed by the test scores and the relevant grade. All data should be separated by a single space. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, an array of testScores of type int...

  • Write a C++ program that will read in image data from the file "image.txt" and illustrate...

    Write a C++ program that will read in image data from the file "image.txt" and illustrate that image as ASCII art. The Image file will contain several lines, representing horizontal rows in the image, and each line will have several integers in the range 0-255. representing povels, separated by spaces. You should read this image data into a vector vector in Once you've read in the phel data, go through the image and print out each piel in the image...

  • Maintaining a healthy weight is important for women’s health. Some physicians recommend being back to your...

    Maintaining a healthy weight is important for women’s health. Some physicians recommend being back to your pre-pregnancy weight six months after giving birth. Public health officials are interested in whether this is a realistic goal. Do the women in North Carolina weigh more than their pre-pregnancy weight six months postpartum? In other words, is the six months post-pregnancy weight greater than their pre-pregnancy weight? Use a paired samples t-test to compare pst6wght (variable 1) and prewght (variable 2). This uses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT