fork download
  1. //Ryan Robateau CSC5 Chapter 7, P. 446, #9
  2. //
  3. /*******************************************************************************
  4.  * Corrected DL Exam
  5.  * _____________________________________________________________________________
  6.  * This program compares right answers and wrong answers for 20 questions in
  7.  * an exam.
  8.  *
  9.  * For the purposes of this program, a student must answer 15
  10.  * out of 20 correctly to pass.
  11.  * _____________________________________________________________________________
  12.  ******************************************************************************/
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. int main()
  17. {
  18.  
  19. const int NUM_Q = 20;
  20. char student[NUM_Q]; // INPUT - Student Answers
  21. char correct[NUM_Q] = {'B', 'D', 'A', 'A', 'C',
  22. 'A', 'B', 'A', 'C', 'D',
  23. 'B', 'C', 'D', 'A', 'D',
  24. 'C', 'C', 'B', 'D', 'A'};
  25. int numWrong = 0; // OUTPUT - Nmber of wrong answers
  26. int numCorrect = 0; // OUTPUT - Number of correct answers
  27. int count;
  28. int incorrect[NUM_Q];
  29.  
  30. for (count = 0; count < NUM_Q; count++)
  31. {
  32. cout << "Enter your answer for #" << count + 1 << ": " << endl;
  33. cin >> student[count];
  34.  
  35.  
  36. while (student[count] != 'A' && student[count] != 'B' && student[count]
  37. != 'C' && student[count] != 'D')
  38. {
  39. cout << endl;
  40. cout << "<ERROR: Please input a valid answer A, B, C, or D>";
  41. cout << endl << endl;
  42. cout << "Enter your answer for #" << count + 1 << ": " << endl;
  43. cin >> student[count];
  44. }
  45. }
  46.  
  47. // Correct the student's test
  48. for (count = 0; count < NUM_Q; count++)
  49. {
  50. if (student[count] == correct[count])
  51. numCorrect++;
  52. else
  53. {
  54. numWrong++;
  55. incorrect[count - numCorrect] = count + 1;
  56. }
  57. }
  58.  
  59. // Determine passing or failing
  60. if (numCorrect >= 15)
  61. cout << "\nYou've passed the test!" << endl;
  62. else
  63. cout << "\nYou failed the test." << endl;
  64.  
  65. // Display score details
  66. cout << "Score: " << numCorrect << "/" << NUM_Q << endl;
  67. cout << "Number of incorrect answers: " << numWrong << endl;
  68. cout << "Incorrectly answered questions: ";
  69. for (count = 0; count < numWrong; count++)
  70. {
  71. cout << "#" << incorrect[count] << " ";
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5300KB
stdin
B
D
A
A
C
A
B
A
C
D
B
C
G
G
D
A
D
C
C
B
D
D
stdout
Enter your answer for #1: 
Enter your answer for #2: 
Enter your answer for #3: 
Enter your answer for #4: 
Enter your answer for #5: 
Enter your answer for #6: 
Enter your answer for #7: 
Enter your answer for #8: 
Enter your answer for #9: 
Enter your answer for #10: 
Enter your answer for #11: 
Enter your answer for #12: 
Enter your answer for #13: 

<ERROR: Please input a valid answer A, B, C, or D>

Enter your answer for #13: 

<ERROR: Please input a valid answer A, B, C, or D>

Enter your answer for #13: 
Enter your answer for #14: 
Enter your answer for #15: 
Enter your answer for #16: 
Enter your answer for #17: 
Enter your answer for #18: 
Enter your answer for #19: 
Enter your answer for #20: 

You've passed the test!
Score: 19/20
Number of incorrect answers: 1
Incorrectly answered questions: #20