A 20 question multiple choice test was given to a group of students. The possibl
ID: 3833890 • Letter: A
Question
A 20 question multiple choice test was given to a group of students. The possible responses are a, b, c, or d.
The maximum size of the class is 35. However, less than 35 students may have taken the test.
An input file has been created that contains the following:
1st line: The answer key consists of 20 lower case letters that represent the correct answers to the exam. There will be no blank spaces between the letters. Example: abcdabcdabcdabcdabcd
2nd line - last line: Each line will start with an ID# (int), followed by the first and last names of a student (names will be separated by at least one blank space). The names may be in a mixture of upper and lower case letters. The names will be followed by at least 1 blank space and then that student's responses to the test questions will be provided. There will not be any blank spaces between the responses, but they may be in a mixture of upper and lower case letters. An upper case letter should be considered correct if it matches the lower case equivalent in the key.
Example: 63 biLL jOnEs aBcdAbCDabDcabDDABCD
NOTE: The key will consist of exactly 20 characters. However, the student responses may consist of more or less than 20 characters. Assume that the first 20 characters in a student response correspond to the 20 test questions. In other words, if a student only provides 15 answers, then the last 5 questions are wrong. If a student provides 25 answers, the last 5 responses should be ignored.
A second input file contains several integers that are supposed to be the ID#s for some of the students in the class.
Design a C++ program that will
interactively prompt for and read the name of the first input file (with key and student data) and use a filestream variable to represent the file
read the data from the input file and store the student information (ID#s, names, test answers) into an array of structs, counting the students as the data is read
reformat the names into a more conventional form
use the key to grade the exams and store each student's score in the array
interactively prompt for and read the name of an output file
write the following to the specified output file (separate each section with a blank line to improve readability)
-a report that displays the names of the students (alphabetically by last name) along with their ID#s and test scores (see formatting specifications and samples below)
-a report that displays the class standings with ID#s and test scores (in descending order) (see formatting specifications and samples below)
-the standings report should include
-the number of students who took the test with a label (maximum class size is 35) the class average (2 digits to right of decimal)
-the median (middle value or average of middle values if there are an even number of students, 2 digits to right of decimal)
interactively prompt for and read the name of the second input file
-write an appropriate message to indicate the start of the second input file processing
-write the test key to the output file (with label)
-read each integer, if it matches an ID# in the student array, write the student's name, test score, and test answers (you may reformat the test answers if you wish) with labels to the output file
-if the integer does not match an ID# in the array, write an error message that includes the invalid ID# to the output file Grading a Test : Each of the questions on the test is worth 5 points. The test score is the # of correct responses multiplied by the point value of a question.
Assumptions
-The first input file will not be empty. Each line in the file will be terminated by a linefeed (' '). It will be formatted as described above. -
The second input file will not be empty. Each integer will be separated by whitespace. The last line in the file will be terminated by a linefeed (' ').
-ID#s will be between 1 and 99.
-The length of a name (first or last) will be 10 characters or less, for formatting purposes.
Requirements :
The program MUST make use of functions (at least 5 meaningful functions in addition to main).
The program MUST CREATE A STRUCT DATA TYPE TO STORE STUDENT DATA AND THEN DECLARE AN ARRAY OF STRUCTS.
The program MUST PASS PARAMETERS to communicate values. No global variables are allowed.
No goto statements may be used.
Program must make use of filestream variables to represent the input and output files. Each input file may only be read one time.
When prompting for file names, the required order is: 1st input file, output file, 2nd input file.
Failure to adhere to the 6 previous requirements will result in up to a 60% deduction of assignment's point value.
Average and median (if even number of students) should be displayed with 2 digits to right of decimal.
For first report, right justify ID#s and test scores. Left justify names. Display names: last,first.
For second report (class standings), right justify ID#s and scores.
Program must include preprocessor directives for all header files used.
Program must use a static array (do not use a variable for the size of the array).
Test your program adequately!
Sample terminal session:
[rrrrr@bobby keys]$ more first4five
abcdabcdabcdabcdabcd
23 boNNie jOnEs ABCDabcdABCDabcdABCD
71 amy sMITH dcbaABCDabcdABCdabCaBBBB
10 mAttHeW ADams AAAAAAAAAAAA
31 BarBarA mARTINSon ABCDABCDABCDABCDABdb 1
9 jENNIFER schWarTz aabbcccdabcdabcdabcd
56 lArrY blaCk ABCDABCDABCDABCDaBcbbbbbbbb
[rrrrr@bobby keys]$ more second4five
56 13 10
19
-5
[rrrrr@bobby keys]$ g++ assign05.cpp
[rrrrr@bobby keys]$ ./a.out
Enter name of first input file
first4five
Enter the name of the output file
fiveout
Enter name of 2nd input file
second4five
[rrrrr@bobby keys]$ more fiveout
NAME ID# SCORE
Adams,Matthew 10 15
Black,Larry 56 95
Jones,Bonnie 23 100
Martinson,Barbara 31 90
Schwartz,Jennifer 19 75
Smith,Amy 71 75
CLASS ID# STANDING SCORE
23 100
56 95
31 90
19 75
71 75
10 15
# of Students: 6
Class Average: 75.00
Median Score: 82.50
BEGIN STUDENT SEARCH FILE PROCESSING
Test key: abcdabcdabcdabcdabcd
ID#: 56
Name: Larry Black
Test score: 95
Test responses: abcdabcdabcdabcdabcbbbbbbbb
13 is not a valid student ID#
ID#: 10
Name: Matthew Adams
Test score: 15
Test responses: aaaaaaaaaaaa
ID#: 19
Name: Jennifer Schwartz
Test score: 75
Test responses: aabbcccdabcdabcdabcd
-5 is not a valid student ID#
Explanation / Answer
Here is the code for the question. Output is shown. Please dont forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
typedef struct
{
int id;
string fname;
string lname;
string ans;
int score;
}student;
//converts a name so that the output string is having 1st letter capital and rest all in lower case
string convert_name(string name)
{
name[0]=toupper(name[0]);
for(int i=1; i<name.length(); i++)
name[i] = tolower(name[i]);
return name;
}
//load student data from file into array of student struct. returns the number of records loaded
int load_file(istream &infile, student stud[])
{
int numOfStud = 0;
while(infile >> stud[numOfStud].id)
{
infile >> stud[numOfStud].fname;
stud[numOfStud].fname = convert_name(stud[numOfStud].fname);
infile >> stud[numOfStud].lname;
stud[numOfStud].lname = convert_name(stud[numOfStud].lname);
infile >> stud[numOfStud].ans;
numOfStud ++;
}
return numOfStud;
}
//calculate the score for the ans
int get_score(string keys, string ans)
{
int size = ans.length();
int score = 0;
if(size > 20) // if more than 20 characters are present , ignore them
size = 20;
for(int i=0; i<size; i++)
{
if(tolower(ans[i]) == keys[i])
{
score += 5; //5 points for correct answer
}
}
return score;
}
//calcuate scores for all students
void calculate_score(string keys, student stud[], int count)
{
for(int i=0; i<count; i++)
{
stud[i].score = get_score(keys, stud[i].ans);
}
}
//display student details in output file
void display(ostream &out, student stud[], int count)
{
for(int i=0; i<count; i++)
{
out<<setw(10)<<left<<stud[i].lname<<", "<<setw(10)<<stud[i].fname<<setw(5)<<right<<stud[i].id<<setw(5)<<stud[i].score<<endl;
}
out<<endl;
}
//sorts the arry by student lname
void sort_by_lname(student stud[], int count)
{
student temp;
int minIdx;
for(int i=0; i<count; i++)
{
minIdx = i;
for(int j=i+1; j<count; j++)
{
if(stud[j].lname < stud[minIdx].lname)
minIdx = j;
}
if(minIdx != i)
{
temp = stud[i];
stud[i] = stud[minIdx];
stud[minIdx] = temp;
}
}
}
//sorts the array in descending order by student score
void sort_by_score(student stud[], int count)
{
student temp;
int maxIdx;
for(int i=0; i<count; i++)
{
maxIdx = i;
for(int j=i+1; j<count; j++)
{
if(stud[j].score > stud[maxIdx].score)
maxIdx = j;
}
if(maxIdx != i)
{
temp = stud[i];
stud[i] = stud[maxIdx];
stud[maxIdx] = temp;
}
}
}
//prints report1
void report1(ostream &outfile, student stud[], int numOfStud)
{
sort_by_lname(stud, numOfStud);
outfile<<"NAME ID# SCORE"<<endl;
display(outfile,stud, numOfStud);
}
//prints report 2
void report2(ostream &outfile, student stud[], int numOfStud)
{
sort_by_score(stud, numOfStud);
outfile<<"CLASS ID# STANDING SCORE"<<endl;
for(int i=0;i<numOfStud;i++)
outfile<<setw(5)<<right<<stud[i].id<<" "<<setw(5)<<stud[i].score<<endl;
outfile<<endl;
}
//print statistic average, median
void print_stats(ostream &out, student stud[], int numOfStud)
{
out<<"# of Students: "<<numOfStud<<endl;
double avg = 0;
for(int i=0; i<numOfStud; i++)
{
avg += stud[i].score;
}
avg /= numOfStud;
out<<"Class Average: "<<fixed<<setprecision(2)<<avg<<endl;
//calculate the median
double median;
int mid = numOfStud/2;
if(numOfStud % 2 == 0) //even number of students
{
median = (stud[mid-1].score + stud[mid].score)/2.0;
}
else
{
median = stud[mid].score;
}
out<<"Median Score: "<<fixed<<setprecision(2)<<median<<endl;
out<<endl;
}
//returns the index of the student id in the array, -1 if not found
int find_student(int id, student stud[], int count)
{
for(int i=0; i<count; i++)
{
if(stud[i].id == id)
return i;
}
return -1;
}
//searches for ids from input file and output matching student details in output file
void search_and_print(ifstream &infile, ostream &outfile, student stud[], int count)
{
int id;
int idx;
while(infile>>id)
{
idx = find_student(id, stud, count);
if(idx != -1)
{
outfile<<"ID#: "<<stud[idx].id<<endl;
outfile<<"Name: "<<stud[idx].fname<<" "<<stud[idx].lname<<endl;
outfile<<"Test score: "<<stud[idx].score<<endl;
outfile<<"Test responses: "<<stud[idx].ans<<endl<<endl;
}
else
{
outfile<<id<<" is not a valid student ID#"<<endl<<endl;
}
}
}
int main()
{
ifstream infile1, infile2;
ofstream outfile;
string infilename1, infilename2, outfilename;
string keys;
student stud[35];
int numOfStud=0;
cout<<"Enter the 1st input file: ";
cin>>infilename1;
cout<<"Enter the output file: ";
cin>>outfilename;
infile1.open(infilename1.c_str());
if(!infile1.is_open())
{
cout<<"Input file1 "<<infilename1 <<" could not be opened !"<<endl;
return 1;
}
infile1>>keys;
numOfStud = load_file(infile1, stud);
infile1.close();
outfile.open(outfilename.c_str());
if(!outfile.is_open())
{
cout<<"Error opening output file "<<outfilename<<endl;
return 1;
}
calculate_score(keys, stud, numOfStud);
report1(outfile, stud, numOfStud);
report2(outfile, stud, numOfStud);
print_stats(outfile, stud, numOfStud);
cout<<"Enter the 2nd input file: ";
cin>>infilename2;
infile2.open(infilename2.c_str());
if(!infile2.is_open())
{
cout<<"Input file2 "<<infilename1 <<" could not be opened !"<<endl;
return 1;
}
outfile<<"BEGIN STUDENT SEARCH FILE PROCESSING"<<endl;
outfile<<"Test key: "<<keys<<endl<<endl;;
search_and_print(infile2, outfile, stud, numOfStud);
infile2.close();
outfile.close();
}
sample run:
Enter the 1st input file: input1.txt
Enter the output file: output.txt
Enter the 2nd input file: input2.txt
input1.txt (input file)
abcdabcdabcdabcdabcd
23 boNNie jOnEs ABCDabcdABCDabcdABCD
71 amy sMITH dcbaABCDabcdABCdabCaBBBB
10 mAttHeW ADams AAAAAAAAAAAA
31 BarBarA mARTINSon ABCDABCDABCDABCDABdb
19 jENNIFER schWarTz aabbcccdabcdabcdabcd
56 lArrY blaCk ABCDABCDABCDABCDaBcbbbbbbbb
input2.txt (input file)
56 13 10
19
-5
output.txt (output file)
NAME ID# SCORE
Adams , Matthew 10 15
Black , Larry 56 95
Jones , Bonnie 23 100
Martinson , Barbara 31 90
Schwartz , Jennifer 19 75
Smith , Amy 71 75
CLASS ID# STANDING SCORE
23 100
56 95
31 90
19 75
71 75
10 15
# of Students: 6
Class Average: 75.00
Median Score: 82.50
BEGIN STUDENT SEARCH FILE PROCESSING
Test key: abcdabcdabcdabcdabcd
ID#: 56
Name: Larry Black
Test score: 95
Test responses: ABCDABCDABCDABCDaBcbbbbbbbb
13 is not a valid student ID#
ID#: 10
Name: Matthew Adams
Test score: 15
Test responses: AAAAAAAAAAAA
ID#: 19
Name: Jennifer Schwartz
Test score: 75
Test responses: aabbcccdabcdabcdabcd
-5 is not a valid student ID#
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.