fork download
  1. //Nathan Dominguez CSC5 Chapter 7, P. 446, #9
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Display Driver Exam Results
  6.  * _____________________________________________________________________________
  7.  * The program will compute the number of correct and incorrect answers of the
  8.  * exam, and display the number of questions answered incorrectly, correctly, if
  9.  * the user pass or failed the exam, and which questions were answered
  10.  * incorrectly.
  11.  * _____________________________________________________________________________
  12.  * INPUT
  13.  * student[grade] : student's choice
  14.  * size : holds size for arrays
  15.  * key[i] : key to compare for correct answers
  16.  *
  17.  * OUTPUT
  18.  * correct : number of correct answers
  19.  * incorrect : number of incorrect answers
  20.  * wrongNumber[questions] : holds questions with incorrect answers
  21.  *
  22.  ******************************************************************************/
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. int main()
  28. {
  29. const int size = 20; //array size
  30. char key[size] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B',
  31. 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; //array list
  32. char choice[size]; //answer chosen
  33. int correct = 0; //OUTPUT - number of answers chosen correctly
  34. int incorrect = 0; //OUTPUT - number of answers chosen incorrectly
  35. int incorrectQ[size]; //number of incorrect questions stored
  36. //
  37. //loop program 20 times
  38. for (int i = 0; i < size; i++)
  39. {
  40. cout << "what is your choice for question " << (i+1) << "?: ";
  41. cin >> choice[i];
  42. while (choice[i] != 'A' && choice[i] != 'B'
  43. && choice[i] != 'C' && choice[i] != 'D')
  44. {
  45. //display error message
  46. cout << endl << "Invalid entry, please try again." << endl;
  47. cin >> choice[i];
  48. }
  49. cout << choice[i] << endl;
  50. //compute which answers and correct and incorrect
  51. if (choice[i] == key[i])
  52. correct++;
  53. else
  54. {
  55. incorrect++;
  56. incorrectQ[incorrect-1] = (i+1);
  57. }
  58. }
  59. //ouput passed for failed
  60. if (correct >= 15)
  61. cout << "EXAM PASSED" << endl;
  62. else
  63. cout << "EXAM FAILED" << endl;
  64. //ouput final scores
  65. cout << "amount correct: " << correct << endl;
  66. cout << "amount incorrect: " << incorrect << endl;
  67. cout << "incorrect questions: ";
  68. for (int i = 0; i < incorrect; i++)
  69. {
  70. cout << incorrectQ[i] << ", ";
  71. }
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0.01s 5308KB
stdin
B D A D C A B D C A B C A C D C C B D A
stdout
what is your choice for question 1?: B
what is your choice for question 2?: D
what is your choice for question 3?: A
what is your choice for question 4?: D
what is your choice for question 5?: C
what is your choice for question 6?: A
what is your choice for question 7?: B
what is your choice for question 8?: D
what is your choice for question 9?: C
what is your choice for question 10?: A
what is your choice for question 11?: B
what is your choice for question 12?: C
what is your choice for question 13?: A
what is your choice for question 14?: C
what is your choice for question 15?: D
what is your choice for question 16?: C
what is your choice for question 17?: C
what is your choice for question 18?: B
what is your choice for question 19?: D
what is your choice for question 20?: A
EXAM PASSED
amount correct: 15
amount incorrect: 5
incorrect questions: 4, 8, 10, 13, 14,