1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
True [87]
3 years ago
15

One of your professors has asked you to write a program to grade her midterm exams, which consist of only 20 multiple-choice que

stions. Each question has one of four possible answers: A, B, C, or D. Create a file "CorrectAnswers.txt" and write the correct answers for each question on a separate line so that the first line contains the answer to the first question, the second line contains the answer to the second question, and so forth. Similarly, create a file "StudentAnswers.txt" and write the student’s answers for each question on a separate line. Write a program that reads the contents of the CorrectAnswers.txt file into a char array, and then reads the contents of file StudentAnswers.txt into a second char array. The program should determine the number of questions that the student answered correctly and then display the following: • The total number of questions answered correctly. • The percentage of questions answered correctly. • If the percentage of correctly answered questions is 75% or greater, the program should indicate that the student passed the exam. Otherwise, it should indicate that the student failed the exam.

Computers and Technology
1 answer:
Lorico [155]3 years ago
8 0

Answer:  

Here is the program that determines the number of questions that the student answered correctly.

#include <iostream>  //to use input output functions

#include <fstream>  // to handle files

#define size 20  //size of questions is fixed to 20

using namespace std;   //to identify objects cin cout

int correctAnswers(char answers[], char correct[]);  //to count correct answers given by student

   int main(){    //start of main function

       char correct[size];         // array stores correct answers

       char answers[size];       //stores answers of students

       int result;                     //stores the correct answers result of student

       double percentage;   //stores the percentage of correct answers

       int counter;        //counts number of correct answers given by student  

       ifstream reader("CorrectAnswers.txt");  //reads the contents of CorrectAnswers.txt file

       if(!reader){  //if file cannot be read/opened

           cout<<"Cannot open the file"<<endl; //displays error message

           return 0;        }          

       counter = 0;    //sets counter to 0 initially

       while( (reader>>correct[counter++]) && counter < size  );    //reads each answer from CorrectAnswers.txt  and stores it in char array correct[] at each iteration of while loop

       reader.close();  //closes the file

       reader.open("StudentAnswers.txt");    //opens StudentAnswers.txt file  

       if(!reader){   //if file could not be opened

           cout<<"Cannot open file"<<endl;   //displays this error message

           return 0;          }          

       counter = 0;    //sets counter to 0

       while( (reader>>answers[counter++]) && counter < size );   //reads each answer from StudentAnswers.txt  and stores it in char array answers[] at each iteration of while loop    

       result = correctAnswers(answers,correct);  //calls correctAnswers method to compute the correct answers given by student and stores the result of correct answers in result variable

       percentage = 100.0*result/size;   //computes percentage of correct answers of student    

       cout<<"The total number of questions answered correctly: "<<result<<endl;     //displays total no of qs answered correctly

       cout<<"The percentage of questions answered correctly: "<<percentage<<" %"<<endl;  //displays computed percentage

       if( percentage >= 75 ){  //if percentage of correctly answered questions is 75% or greater

           cout<<"Student has passed the exam!"<<endl;   } //displays that the student passed the exam

      else { //if percentage of correctly answered questions is below 75%

           cout<<"Student has failed the exam!"<<endl;         }  //displays that the student failed the exam

       return 0;     }  

   int correctAnswers(char answers[], char correct[]){   //method to compute the number of correct answers  

       int countCorrect = 0;   //stores the count of correct answers

       for(int i=0; i<size; i++){  //iterates 20 times to match answers from both the files of 20 questions

           if( answers[i] == correct[i] ){  // if the student answer in StudentAnswers.txt file matches with the correct answer given in CorrectAnswers.txt file

               countCorrect++;              }         }     //adds 1 to the countCorrect every time the student answer matches with the correct answer

       return countCorrect;     }  //returns the number of questions that the student answered correctly

Explanation:

The program is well explained in the comments mentioned with each line of code.

Here are the two files that can be used to check the working of the above program. You can create your own text files too.

     CorrectAnswers.txt :

A

A

B

D

C

C

A

C

D

D

A

B

A

B

B

C

A

C

C

B

StudentAnswers.txt

A

C

B

D

C

C

B

C

D

C

D

B

A

B

B

C

A

C

C

B      

Also if the number of wrong answers are to be displayed then add this method to the above code:

void wrongAnswers(char answers[], char correct[]){  //method takes two arrays one with correct answers and the other with student answers

       for(int i=0; i<size; i++){ //iterates 20 times

           if( answers[i] != correct[i] ){ //if the answer by student does not match with correct answer

               cout<<"Question Number: "<<i+1<<endl; //the question that is answered incorrectly

               cout<<"Correct answer is: "<<correct[i]<<endl;  //displays the correct answer

               cout<<"Student answer is: "<<answers[i]<<endl;    }    }    } //displays the answer that student gave

Now call this method in main() as:

if(result < size){  //if result is less than size i.e. 20

           cout<<"The questions answered incorrectly"<<endl;  //number of questions that are answered incorrectly

           wrongAnswers(answers,correct);  //calls method to compute the wrongly answered questions

       }else{  //if result is not less than size

           cout<<"No incorrect answers."<<endl;       }    //no questions incorrectly answered      

       cout<<"Number of missed questions: "<<size-result<<endl; //displays the number of missed questions

The screenshot of the output is attached.

You might be interested in
How do you think computers have helped improve documentation, support and services within the healthcare industry
SVETLANKA909090 [29]
It's summer stop working
5 0
3 years ago
The arrangement of keys on a keyboard, QWERTY reflects the keyboard layout by ________. Group of answer choices using the letter
Firdavs [7]

The setup of keys on a keyboard, QWERTY shows the keyboard layout by taking the letters of the first six alphabetic characters found on the top row of keys.

<h3>What is keyboard?</h3>

A keyboard is known to be a kind of peripheral device that helps a user to be able to input text into any computer or any any kind of electronic device.

Note that The setup of keys on a keyboard, QWERTY shows the keyboard layout by taking the letters of the first six alphabetic characters found on the top row of keys.

Learn more about keyboard from

brainly.com/question/13380788

#SPJ1

8 0
2 years ago
What considerations have to be kept in mind with JPEG
topjm [15]
JPEG is a file format for an image that will be compressed. Since it compressed the file size will be much smaller. That makes JPEG is one of the most used file types for images and was a default format for many application. But the drawback of compressing an image is that a portion of the data will lose and will cause the image quality dropped or produce a noise.
8 0
3 years ago
The cathode ray tube eventually was eventually used for what entertainment device?
Oliga [24]

\huge\underline{\underline{\boxed{\bm {Answer:}}}}

\large\bm{Television}

\huge\underline{\underline{\boxed{\bm {Explanation:}}}}

The cathode ray tube can be said to be a vacuum tube which contains one or more electric guns. This tube produces images when an electron beam strikes a phosphorescent surface. This tube produces pictures as a form of video signals. The entertainment device, which the cathode ray tube was eventually used for since it produces pictures, is a television set.

5 0
1 year ago
The overall cost of wiring in addressable fire alarm system is significantly cheaper than the conventional one. Why is it so?
stellarik [79]

Answer:

Yes, The overall cost of wiring in addressable fire alarm system is significantly cheaper than the conventional one.But the addressale fire alram system is more reliable than a conventional system.

Explanation:

Conventional systems are hardware-based and use analog technology. where addressable fire alarm use digital technology. addressable system is generally more reliable than a conventional system mainly due to how the different systems are wired. With a conventional system, if a device’s wire is damaged or severed, its signal and the signal of other devices down the line cannot be transmitted to the control panel.

With an addressable system, both ends of the wire connect to the control panel. Therefore, if one end becomes damaged or severed, signals can still reach the control panel through the other end of the loop. In addressable systems, a device can be removed or disabled and it will not affect the other devices in the loop.

7 0
3 years ago
Other questions:
  • Plz answer these questions...those who give answer get the brainliest......hurry up..!!!​
    14·1 answer
  • George and Miguel want to know more about their local and online competitors and about the retail industry. What is the best way
    9·1 answer
  • A local cinema allows customer to make a movie ticket reservation. A customer can be
    6·1 answer
  • Defination of formula work sheet​
    15·1 answer
  • 3.5 code practice question 1
    5·2 answers
  • Tips for staying safe using social media. Using in your own words.
    6·1 answer
  • Briefly explain how Riboflavin deficiency lead to disease state.​
    15·1 answer
  • Please define processor​
    14·1 answer
  • A regional transportation and logistics company recently hired its first ChiefInformation Security Officer (CISO). The CISO’s fi
    7·1 answer
  • Greg is concerned about the use of ddos attack tools against his organization, so he purchased a mitigation service from his isp
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!