Question

In: Computer Science

Write a C++ program that reads a file consisting of students’ test scores in the range...

Write a C++ 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.)

Solutions

Expert Solution

Thanks for the question, here is the code in C++

Note: Please refer the screenshot for the input file having the scores data. Also, when you run the program make sure you update the filename correctly in the below line -

char * filename ="F:\\scores.txt";

Have included comments so that you can follow the code. Let me know in case you have any questions.

============================================================================

#include<iostream>
#include<fstream>
using namespace std;

// utlility function that accepts a number and returns

// the index in the array where it should be counted

int get_index(int n){
  
   if(n>=200)return 7;
   return n/25;
  
}

// accepts an int array and prints the frequency in the range

void displayHistogram(int range[], int size){
  
   for(int i=0;i<size;i++){
       if(i!=size-1)
       cout<<25*i<<"-"<<i*25+24<<" : "<<range[i]<<endl;
       else
       cout<<25*i<<"-"<<i*25+25<<" : "<<range[i]<<endl;
      
      
   }
}

int main(){

// update the filename of the input file in the below line.
   char * filename ="F:\\scores.txt";
   ifstream infile(filename);
   int range[8]{0};
   if(infile.is_open()){
       int score;
       while(infile>>score){
           range[get_index(score)]+=1;
          
       }
       infile.close();
       displayHistogram(range,8);
      
   }else{
       cout<<"Unable to read data from file: "<<filename<<endl;
   }
}


Related Solutions

Write a program that reads a file consisting of students’ test scores in the range 0–200....
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,...
Write a program that reads students’ names followed by their test scores. The program should output...
Write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testScore of type int (testScore is between 0 and...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. Replace "sh" with ph This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would not do that Paragraph 2 We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT