fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 7 P.446 #
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compute Exam Scores
  6.  * ____________________________________________________________________________
  7.  * This program will ask for the 20 correct answers, then ask for the student's
  8.  * 20 answers. It will then display the questions missed, correct vs. student
  9.  * answers, total missed, percentage score, and pass/fail status.
  10.  *
  11.  * Formulas used:
  12.  * missed = number of questions - correctly answered
  13.  * percent = (correctly answered / number of questions) * 100.0
  14.  * ____________________________________________________________________________
  15.  * Input
  16.  * num_questions : Constant representing the total number of questions
  17.  * correct : The answer key for the test
  18.  * student : The answers the student had put for each question
  19.  * Output
  20.  * correctCount : The number of questions answered correctly by the student
  21.  * missed : Number of questions the student missed
  22.  * percent : Perecntage of correctly answered questions
  23.  *****************************************************************************/
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27. //Global Variable
  28. const int num_questions = 20;
  29.  
  30. int main() {
  31. //Data Dictionary
  32. char correct[num_questions]; //INPUT - The answer key for the test
  33. char student[num_questions]; //INPUT - The answers the student had put for each question
  34. int correctCount = 0; //OUTPUT - The number of questions answered correctly by the student
  35. int missed; //OUTPUT - Number of questions the student missed
  36. float percent; //OUTPUT - Perecntage of correctly answered questions
  37.  
  38. //Prompt User
  39. cout << "Enter the correct answers for " << num_questions << " questions: " << endl;
  40. for (int i =0; i < num_questions; i++) {
  41. cout << "Question " << i + 1 << ": " << endl;
  42. cin >> correct[i];
  43. if (correct[i] != 'A' && correct[i] != 'B' && correct[i] != 'C' && correct[i] != 'D'){
  44. cout << "Invalid Letter please enter A, B, C, or D!" << endl;
  45. cin >> correct [i];
  46. }
  47. }
  48. cout << "Enter the student's answers for " << num_questions << " questions: " << endl;
  49. for (int i = 0; i <num_questions; i++) {
  50. cout << "Question " << i + 1 << ": " << endl;
  51. cin >> student[i];
  52. if (student[i] != 'A' && student[i] != 'B' && student[i] != 'C' && student[i] != 'D'){
  53. cout << "Invalid Letter please enter A, B, C, or D!" << endl;
  54. cin >> student[i];
  55. }
  56. }
  57. //Display Answers
  58. cout << "Questions Missed: " << endl;
  59. cout << "-------------------------------------" << endl;
  60. cout << left << setw(10) << "Question" << setw(15) << "Correct Ans";
  61. cout << setw(15) << "Student Ans" << endl;
  62. cout << "--------------------------------------" << endl;
  63.  
  64. for (int i = 0; i < num_questions; i++) {
  65. if (student [i] == correct[i]) {
  66. correctCount++;
  67. }
  68. else {
  69. cout << left << setw(10) << (i + 1) << setw(15) << correct[i];
  70. cout << setw(15) << student [i] << endl;
  71. }
  72. }
  73.  
  74. //Calculations
  75. missed = num_questions - correctCount;
  76. percent = ((correctCount * 1.0) / num_questions) * 100.0;
  77.  
  78. //Display Results
  79. cout << "-----------------------------------" << endl;
  80. cout << "Total Questions Missed: " << missed << endl;
  81. cout << fixed << setprecision(2);
  82. cout << "Percentage Correct: " << percent << "%" << endl;
  83.  
  84. if (percent >= 70.0)
  85. cout << "Result: PASS" << endl;
  86. else
  87. cout << "Result: FAIL" << endl;
  88. return 0;
  89. }
Success #stdin #stdout 0.01s 5284KB
stdin
B
D
A
A
C
A
B
A
C
D
B
C
D
A
D
C
C
B
D
A
B
D
A
A
B
A
B
A
C
D
A
C
D
A
D
B
B
B
D
A
stdout
Enter the correct answers for 20 questions: 
Question 1: 
Question 2: 
Question 3: 
Question 4: 
Question 5: 
Question 6: 
Question 7: 
Question 8: 
Question 9: 
Question 10: 
Question 11: 
Question 12: 
Question 13: 
Question 14: 
Question 15: 
Question 16: 
Question 17: 
Question 18: 
Question 19: 
Question 20: 
Enter the student's answers for 20 questions: 
Question 1: 
Question 2: 
Question 3: 
Question 4: 
Question 5: 
Question 6: 
Question 7: 
Question 8: 
Question 9: 
Question 10: 
Question 11: 
Question 12: 
Question 13: 
Question 14: 
Question 15: 
Question 16: 
Question 17: 
Question 18: 
Question 19: 
Question 20: 
Questions Missed: 
-------------------------------------
Question  Correct Ans    Student Ans    
--------------------------------------
5         C              B              
11        B              A              
16        C              B              
17        C              B              
-----------------------------------
Total Questions Missed: 4
Percentage Correct: 80.00%
Result: PASS