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

Rewrite Homework 7a, assuming that you have no idea what the size of the Correct

ID: 3602538 • Letter: R

Question

Rewrite Homework 7a, assuming that you have no idea what the size of the CorrectAnswer file is. Since you don’t know how big the file is (i.e. how many multiple choice questions there were to grade), then a better choice of sequence containers might be the vector instead of the array. Use the same files, CorrectAnswers.txt, and StudentAnswers.txt, as before but this time determine the number of questions to grade from the size of the Vector that you make when you load all CorrectAnswers.txt into the CorrectAnswer Vector. You will then want to check to be sure that the StudentAnswers.txt file contains the same number of answers as the CorrectAnswers file, when you load those into a second Vector holding StudentAnswers. Below are the requirements for this program.

• The output should look pretty much the same as that in Homework 7A.

• Name your program: YourName-HwrkVII.cpp .

• A list of the questions (by number) missed by the Student, showing the correct answer, and the incorrect answer provided by the student for each missed question.

• The total number of questions missed.

• The percentage of questions answered correctly.

• Make sure your name is displayed along with your data.

• Also, always be sure that your name appears as a comment at the top of your program.

• Your program should test to be sure that there are the same number of answers given in the StudentAnswers.txt file as there are Questions in the CorrectAnswers.txt file. o If there are not, then the program should stop, displaying on the screen that the StudentAnswers.txt file does not have the same number of answers as there are questions. You can use a cin.get(); to hold the error display on the screen.

• Be sure to use Vectors, not Arrays, to obtain your result. • Do not make the assumption that the CorrectAnswers.txt file and the StudentAnswers.txt file have only 20 questions. They could have any number.

Below is a re-statement of Homework07A. One of your professors has asked you to write a program to grade the final , which consist of only multiple-choice questions. Each question has one of four possible answers: A, B, C, or D. The file CorrectAnswers.txt contains the correct answers for all of the questions, with each answer written on a separate line. The first line contains the answer to the first question, the second line contains the answer to the second question, and so forth.

Write a program that reads the contents of the CorrectAnswers.txt file into a char array, and then reads the contents of another file, containing a student’s answers, called StudentAnswers.txt, into a second char array. The program should determine the number of questions that the student missed and then display the following:

• A list of the questions (by number) missed by the Student, showing the correct answer, and the incorrect answer provided by the student for each missed question.

• The total number of questions missed.

• The percentage of questions answered correctly.

This is the original program:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char answer_array[20];
int i=0;
char student_answer_array[20];
ifstream infile("CorrectAnswers.txt");
ifstream infile1("StudentAnswers.txt");
if(!infile)
{
cout <<"Unable to open CorrectAnswers.txt. so Exiting from program " << endl;
return 0;
}
if(!infile1)
{
cout <<"Unable to open StudentAnswers.txt. so Exiting from program " << endl;
return 0;
}
while(!infile.eof())
{
infile >> answer_array[i++];
}
infile.close();
i=0;
while(!infile1.eof())
{
infile1 >> student_answer_array[i++];
}
infile1.close();
int missed_questions=0;
for(i=0; i<20; i++)
{
if(answer_array[i]!=student_answer_array[i])
{
cout <<"You got wrong answer for question " << (i+1) << endl;
cout << "Correct answer is " << answer_array[i] << endl;
cout << "Your answer is " << student_answer_array[i] << endl;
missed_questions++;
}
}
cout <<"The total number of questions missed is " << missed_questions << endl;
double percentage = static_cast<double> (20-missed_questions)*100/20;
cout <<"The percentage of questions answered correctly " << percentage << endl;
if(percentage>70)
cout <<"Student passed the final " << endl;
else
cout <<"Student failed the final " << endl;
return 0;
}

Explanation / Answer

--

#include<iostream>
#include<fstream>
#include <vector>
#include <iterator>
using namespace std;
int main() {

   int i = 0;
   ifstream infile("CorrectAnswers.txt");
   ifstream infile1("StudentAnswers.txt");
   if (!infile) {
       cout << "Unable to open CorrectAnswers.txt. so Exiting from program "
               << endl;
       return 0;
   }
   if (!infile1) {
       cout << "Unable to open StudentAnswers.txt. so Exiting from program "
               << endl;
       return 0;
   }

   istream_iterator<char> start(infile), end;
   vector<char> answer_vector(start, end);

   istream_iterator<char> start1(infile1), end1;
   vector<char> student_answer_vector(start1, end1);

   if (answer_vector.size() != student_answer_vector.size()) {
       cout << "Answers in both files are not same" << endl;
   }

   int missed_questions = 0;
   vector<char>::iterator it1 = student_answer_vector.begin();
   vector<char>::iterator it2 = answer_vector.begin();
   for (; it1 != student_answer_vector.end(); ++it1, ++it2) {

       if (*it1 != *it2) {
           cout << "You got wrong answer for question " << (i + 1) << endl;
           cout << "Correct answer is " << *it1 << endl;
           cout << "Your answer is " << *it2 << endl;
           missed_questions++;
       }
   }
   infile.close();
   infile1.close();
   cout << "The total number of questions missed is " << missed_questions
           << endl;
   double percentage = static_cast<double>(20 - missed_questions) * 100 / 20;
   cout << "The percentage of questions answered correctly " << percentage
           << endl;
   if (percentage > 70)
       cout << "Student passed the final " << endl;
   else
       cout << "Student failed the final " << endl;
   return 0;
}

==

CorrectAnswers.txt

==

A
B
C
D

==

StudentAnswers.txt

=

A
C
C
D

===

OUTPUT:-

==

You got wrong answer for question 1
Correct answer is C
Your answer is B
The total number of questions missed is 1
The percentage of questions answered correctly 95
Student passed the final


==

Thanks

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