Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The file \"grades.txt\" contains the following data, separated by spaces, in thi

ID: 3681826 • Letter: T

Question

The file "grades.txt" contains the following data, separated by spaces, in this order: first name, last name, point percentage. For each person in the file, compute his or her letter grade using the grade breakdown shown below. You should then display the distribution of letter grades to the terminal. An example is shown below Grade Minimum % | 93 | 90 | 87 | 83 | 80 | 77 | 73 | 70 | 67 | 63 | 60 | 0 Additionally, create a file called "statistics.txt" to show how many people got each letter grade In this file, sum all grades with the same letter and report them together. The format of the statistics file should be: letter grade [space] number of people who received this letter grade. For example, if you count 1 B+, 3 Bs, and 1 B-, report 5 Bs in the statistics file (see example below). You will use the "statistics.txt" file for problem B Example terminal output (user input is underlined) 2 A- 1 B+ 3 B 1 C+ 1 C 0 D+ 0 D- 1 F Example statistics.txt: A 3 B 5 C 2 D 1

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;

int main(){
  
   // declaring and opening input stream
   ifstream input;
   input.open("grades.txt");
  
   //declaring and opening output file
   ofstream output;
   output.open("statistics.txt");
  
   string firstname, lastname;
   double grade;
  
   // declaring variables to count different grades
   int grades[12] = {0}; // grades[0] = A, grades[1] = A- .... grades[11] = F, initializing with 0
   int A=0, B=0, C=0, D=0, F=0;
  
   // reading each line and based on grade value incrementing grade counter
   while(input>>firstname>>lastname>>grade){
      
       if(grade>=93){
           grades[0]++;
           A++;
       }
       else if(grade>=90){
           grades[1]++;
           A++;
       }
       else if(grade>=87){
           grades[2]++;
           B++;
       }
       else if(grade>=83){
           grades[3]++;
           B++;
       }
       else if(grade>=80){
           grades[4]++;
           B++;
       }
       else if(grade>=77){
           grades[5]++;
           C++;
       }
       else if(grade>=73){
           grades[6]++;
           C++;
       }
       else if(grade>=70){
           grades[7]++;
           C++;
       }
       else if(grade>=67){
           grades[8]++;
           D++;
       }
       else if(grade>=63){
           grades[9]++;
           D++;
       }
       else if(grade>=60){
           grades[10]++;
           D++;
       }
       else{
           grades[11]++;
           F++;
       }
      
   }
   input.close();
  
   // printing each grade count
   cout<<grades[0]<<"   A"<<endl;
   cout<<grades[1]<<"   A-"<<endl;
   cout<<grades[2]<<"   B+"<<endl;
   cout<<grades[3]<<"   B"<<endl;
   cout<<grades[4]<<"   B-"<<endl;
   cout<<grades[5]<<"   C+"<<endl;
   cout<<grades[6]<<"   C"<<endl;
   cout<<grades[7]<<"   C-"<<endl;
   cout<<grades[8]<<"   D+"<<endl;
   cout<<grades[9]<<"   D"<<endl;
   cout<<grades[10]<<"   D-"<<endl;
   cout<<grades[11]<<"   F"<<endl;
  
   //writing to file
  
   output<<"A   "<<A<<endl;
   output<<"B   "<<B<<endl;
   output<<"C   "<<C<<endl;
   output<<"D   "<<D<<endl;
   output<<"F   "<<F<<endl;
  
   output.close();
   return 0;
}


/*

grades.txt:

John Watson 43
Sherlock Holmes 92
Violet Crawley 83
Greg Lestrade 74
Missus Hudson 85
Jim Moriarty 99.999999999
Molly Hooper 65
Irene Adler 91
ycroft Holmes 82
Tom Branson 83
John Bates 89
Mister Carson 79

Console Output:

1   A
2   A-
1   B+
3   B
1   B-
1   C+
1   C
0   C-
0   D+
1   D
0   D-
1   F


statistics.txt :

A   3
B   5
C   2
D   1
F   1

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote