You are to write a C program that grades a true-false quiz. The quiz data will b
ID: 3677791 • Letter: Y
Question
You are to write a C program that grades a true-false quiz. The quiz data will be available in a text file;
here is an example:
Quiz #8 (09/22/14) (corrected version)
TTFTTFTFTF
0080 TTFTTFTFTF
0340 TTFTFFTFTF
1522 TTFFFFTFTF
9001 TFFTTTTTTF
0012 FFTFTFTFTF
9013 TTFTTTFTFT
0913 TTTFFTFTTF
6155 FFTTTTTTFT
3144 FFTTFTTTFT
4400 FFTFFTFTFF
0
The first line in the data file is a comment that you may assume can be up to 80 characters long; it cannot be blank. The second line contains the answer key for the 10-question true-false quiz. The following lines in the data file contain a student’s id number in column 1 followed by their answers for the quiz in column 2. A 0 (zero) on a line by itself indicates that there are no more students to process.
Write a program that first reads in (from standard input; more on this in a moment) the comment line and answer key as strings (i.e. into a char array; see the example code below) followed by each student’s data. Store the student’s id numbers in an array. You are to ‘grade’ each student’s quiz using the key provided followed by recording their scores (in a second array) in order to be able to print them out later. You do not need to use an array of strings or characters in your program to store these ids and scores. Write your program allowing for up to 100 students, and you may assume that at least 1 student will have taken the quiz.
You should create test data text files and provide the data to your program using redirection from the command line (see the sample run below). Your program should output the number of students who took the quiz, the average score, the best score, and a table showing each student’s id, score, and grade. The formatting, spacing, and labels in your output should 1 match the sample run below exactly.
Your program should determine each student’s grade as follows: if the score is equal to the best score b or b 1, give an A. If it is b 2, award a B. Give C’s for any score that is equal to b 3 or b 4, a D for b 5 and an F for all others.
The sample run below uses the data file presented in the example above.
Quiz #8 (09/22/14) (corrected version)
Key: TTFTTFTFTF
# of students: 10
average score: 5.50
best score: 10
id score grade
---- ----- -----
0080 10 A
0340 9 A
1522 8 B
9001 7 C
0012 6 C
9013 5 D
0913 4 F
6155 3 F
3144 2 F
4400 1 F
The code needs to be in C++
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <stdio.h>
using namespace std;
double average (vector<int> a , int n) // function to calculate average
{
double sum = 0;
for (int j = 0; j < n; j++)
{
sum = sum + a[j];
}
sum = sum /n;
return sum;
}
int main()
{
vector<string> id;
ifstream file_("grades.txt"); // opening the file
vector<string> v;
vector<int> score;
int i = 0;
string number;
string tf;
if(file_ >> tf) v.push_back(tf);
while(file_ >> number >> tf)
{
id.push_back(number); // reading id and the respose of the student
v.push_back(tf);
i++;
}
file_.close(); // closingthe file
string s = v[0];
for (i = 1; i < v.size(); ++i)
{
string r = v[i];
int count = 0;
for (int j = 0; j < s.size(); ++j)
{
if(s[j] == r[j]) count++;
}
score.push_back(count);
}
int max = score[0];
for ( i = 0; i < score.size(); ++i)
{
if(score[i] > max) max = score[i];
}
double average_of_scores = average ( score, score.size()); // calling the function
vector<char> grade;
for ( i = 0; i < score.size(); ++i)
{
if(score[i] == max || score[i] == max-1) grade.push_back('A');
else if(score[i] == max-2) grade.push_back('B');
else if( score[i] == max-3 || score[i] == max-4) grade.push_back('C');
else if(score[i] == max-5) grade.push_back('D');
else grade.push_back('F');
}
cout << "Quiz #8 (09/22/14) (corrected version) ";
cout << "Key: "<< v[0] << endl;
cout << "# of students: "<< score.size()<< endl;
cout << "average score: "<<average_of_scores<<endl;
cout << "best score: " << max << endl;
cout << "id score grade"<<endl;
cout << "---- ----- -----"<<endl;
for ( i = 0; i < score.size(); ++i)
{
cout<< id[i] <<" "<<score[i]<<" "<<grade[i]<< endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.