fork download
  1. // Andres Guzman CSC5 Chapter 7, P. 446, #9
  2. /******************************************************************************
  3.  *
  4.  * COMPARE STUDENT ANSWERS
  5.  * ____________________________________________________________________________
  6.  * This program will determine whether a students answers match the correct
  7.  * answer sheet.
  8.  * ____________________________________________________________________________
  9.  *
  10.  * INPUT
  11.  *
  12.  * [SIZE] = array size
  13.  * coranswers[SIZE] = correct answers list
  14.  * studanswers[SIZE] = student answers
  15.  * OUTPUT
  16.  *
  17.  * correct = subscripts that match correct list
  18.  * incorrect = subscripts that dont match correct list
  19.  *
  20.  ******************************************************************************/
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int main()
  25. {
  26. const int SIZE = 20; //Array Size
  27. int i; //counter to cycle subscripts
  28. //Initialization list of correct answers
  29. char coranswers[SIZE] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B',
  30. 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
  31. char studanswers[SIZE]; //Subscripts for characters
  32. int correct = 0; //Accumulator for correct answers
  33. int incorrect = 0; //Accumulator for incorrect answers
  34. //
  35. //Initializing For Loop
  36. for(i = 0; i < SIZE; i++)
  37. {
  38. cout << "Question " << (i + 1) << " Student Answer: ";
  39. cin >> studanswers[i];
  40. while(studanswers[i] != 'A' && studanswers[i] != 'B' &&
  41. studanswers[i] != 'C' && studanswers[i] != 'D') //Input Validation
  42. {
  43. cout << "Invalid Letter Grade." << endl;
  44. cout << "Question " << (i + 1) << " Student Answer: ";
  45. cin >> studanswers[i];
  46. }
  47. if(studanswers[i] == coranswers[i]) //Determines if answers match
  48. {
  49. correct++; //Increases amount of correct answers
  50. }
  51. else
  52. {
  53. incorrect++; //Increases amount of incorrect answers
  54. }
  55. cout << endl;
  56. }
  57. if(correct >= 15) //Determines if there are 15 or more correct answers
  58. {
  59. cout << "\nPASS!\n" << "Correct Questions: " << correct << endl;
  60. cout << "Incorrect Questions: " << incorrect;
  61. }
  62. else //When there are less than 15 correct answers
  63. {
  64. cout << "\nFail\n" << "Correct Questions: " << correct << endl;
  65. cout << "Incorrect Questions: " << incorrect;
  66. }
  67.  
  68. cout << "\n\nIncorrect Questions" << endl;
  69. //
  70. //Initializing For Loop for display all the incorrect answers
  71. for(i = 0; i < SIZE; i++)
  72. {
  73. if(studanswers[i] != coranswers[i]) //When the answers do not match
  74. cout << "Question " << (i + 1) << endl;
  75. }
  76. return 0;
  77. }
Success #stdin #stdout 0s 5320KB
stdin
A A A A A A A A A A A A A A A A A A A A 
stdout
Question 1 Student Answer: 
Question 2 Student Answer: 
Question 3 Student Answer: 
Question 4 Student Answer: 
Question 5 Student Answer: 
Question 6 Student Answer: 
Question 7 Student Answer: 
Question 8 Student Answer: 
Question 9 Student Answer: 
Question 10 Student Answer: 
Question 11 Student Answer: 
Question 12 Student Answer: 
Question 13 Student Answer: 
Question 14 Student Answer: 
Question 15 Student Answer: 
Question 16 Student Answer: 
Question 17 Student Answer: 
Question 18 Student Answer: 
Question 19 Student Answer: 
Question 20 Student Answer: 

Fail
Correct Questions: 6
Incorrect Questions: 14

Incorrect Questions
Question 1
Question 2
Question 5
Question 7
Question 9
Question 10
Question 11
Question 12
Question 13
Question 15
Question 16
Question 17
Question 18
Question 19