C++ From Control Structures Through Objects Chapter 7 Review
The "Chapter 7 – #11: Exam Grader – Tony Gaddis – Starting Out With C++" programming challenge comes from Tony Gaddis' book, "Starting Out with C++ from Control Structures to Objects (8th Edition)"
Problem
One of your professors has asked yous to write a program to grade her final exams, which consist of merely 20 multiple-choice questions. Each question has i of four pos- sible answers: A, B, C, or D. The file CorrectAnswers.txt contains the correct answers for all of the questions, with each answer written on a separate line. The kickoff line contains the answer to the first question, the second line contains the answer to the second question, and and so forth. (Download the volume'southward source code from the companion Spider web site at www.pearsonhighered.com/gaddis. You will find the file in the Chapter 07 binder.)
Write a programme that reads the contents of the CorrectAnswers.txt file into a char assortment, and and so reads the contents of another file, containing a student'southward answers, into a second char array. (Yous tin can use the file StudentAnswers.txt for testing purposes. This file is also in the Affiliate 07 source code folder, available on the book's companion Web site.) The plan should determine the number of questions that the educatee missed and and then display the following:
• A list of the questions missed by the student, showing the right reply and the incorrect reply provided past the student for each missed question
• The total number of questions missed
• The pct of questions answered correctly. This can be calculated as
Correctly Answered Questions ÷ Total Number of Questions
• If the pct of correctly answered questions is 70% or greater, the plan should indicate that the student passed the exam. Otherwise, it should indicate that the student failed the exam.
Solution
#include <iostream> #include <fstream> #include <vector> using namespace std; const int NUMBER_OF_QUESTIONS = 20; void readFileContentsIntoArray(char[], string, const int); void displayArray(const char[], const int); void displayAnswers(const vector<int>, const char[]); void checkAnswers(const char[], const char[], int &, vector<int> &, vector<int> &, int &, int &, const int); void displayAllAnswers(const char[], const char[], const int); double calculatePercentage(const int, const int); void displayFinalInfo(const vector<int>, const vector<int>, const char[], const char[], int, int, double); int main() { char correct_answers[NUMBER_OF_QUESTIONS], student_answers[NUMBER_OF_QUESTIONS]; int number_of_incorrect_questions, total_correct_questions = 0, total_missed_questions = 0; double percentage_correctly_answered; vector<int> incorrect_questions; vector<int> correct_questions; readFileContentsIntoArray(correct_answers, "CorrectAnswers.txt", NUMBER_OF_QUESTIONS); readFileContentsIntoArray(student_answers, "StudentAnswers.txt", NUMBER_OF_QUESTIONS); displayAllAnswers(correct_answers, student_answers, NUMBER_OF_QUESTIONS); checkAnswers(student_answers, correct_answers, number_of_incorrect_questions, correct_questions, incorrect_questions, total_missed_questions, total_correct_questions, NUMBER_OF_QUESTIONS); percentage_correctly_answered = calculatePercentage(total_correct_questions, NUMBER_OF_QUESTIONS); displayFinalInfo(correct_questions, incorrect_questions, correct_answers, student_answers, total_missed_questions, total_correct_questions, percentage_correctly_answered); return 0; } // END int principal() void readFileContentsIntoArray(char assortment[], string fileName, const int ARRAY_SIZE) { ifstream inputFile; inputFile.open up(fileName); if (inputFile.fail()) cout << "Error opening file." << endl; else { for (int i = 0; i < ARRAY_SIZE; i++) inputFile >> assortment[i]; inputFile.close(); } } void displayAllAnswers(const char correct_answers[], const char student_answers[], const int NUMBER_OF_QUESTIONS) { cout << "Correct Answers: " << endl; displayArray(correct_answers, NUMBER_OF_QUESTIONS); cout << endl; cout << "Student Answers: " << endl; displayArray(student_answers, NUMBER_OF_QUESTIONS); cout << endl; } void displayArray(const char array[], const int ARRAY_SIZE) { for (int i = 0; i < ARRAY_SIZE; i++) { if(i == (ARRAY_SIZE - 1)) cout << array[i]; else cout << assortment[i] << ", "; } cout << endl; } void checkAnswers(const char student_answers[], const char correct_answers[], int &number_of_incorrect_questions, vector<int> &correct_questions, vector<int> &incorrect_questions, int &total_missed_questions, int &total_correct_questions, const int NUMBER_OF_QUESTIONS) { for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) { if (student_answers[i] != correct_answers[i]) { number_of_incorrect_questions++; incorrect_questions.push_back(i); total_missed_questions++; } else { correct_questions.push_back(i); total_correct_questions++; } } } double calculatePercentage(const int total_correct_questions, const int NUMBER_OF_QUESTIONS) { return (total_correct_questions / static_cast<double>(NUMBER_OF_QUESTIONS)) * 100; } void displayFinalInfo(const vector<int> correct_questions, const vector<int> incorrect_questions, const char correct_answers[], const char student_answers[], int total_missed_questions, int total_correct_questions, double percentage_correctly_answered) { cout << "Questions answered correctly: " << endl; displayAnswers(correct_questions, correct_answers); cout << endl; cout << "Questions answered incorrectly: " << endl; displayAnswers(incorrect_questions, student_answers); cout << endl; cout << "Total missed questions : " << total_missed_questions << endl; cout << "Total correct questions: " << total_correct_questions << endl; cout << endl; cout << setprecision(1) << fixed; cout << "Pct of questions answered: %" << percentage_correctly_answered << endl; cout << "Y'all " << (percentage_correctly_answered >= 70 ? "passed" : "failed") << " the exam." << endl; } void displayAnswers(const vector<int> vector, const char array[]) { for (int i = 0; i < vector.size(); i++) { cout << (vector[i] + i) << ". " << array[vector[i]] << endl; } // Range-based for loop in C++11 // for (auto value : vector) // cout << (value + 1) << ". " << array[value] << ", "; }
Video Walk Through
This video includes a step by step process of how to solve "Chapter 7 – #eleven: Exam Grader – Tony Gaddis – Starting Out With C++".
Watch video on YouTube.
Chapter 7 Playlist on YouTube
That's information technology for Chapter 7 – #11: Exam Grader
Thank you for taking an interest in what I do! I hope information technology was helpful for you as much as it helped me forth my journey in learning to lawmaking!
C++ Chapter 7 Solutions Here:
Starting Out With C++ | Affiliate 7 | Programming Claiming Solutions
Chapter vii References:
Arrays:
http://world wide web.cplusplus.com/md/tutorial/arrays/
Parallel Arrays:
https://world wide web.tutorialspoint.com/cplusplus-program-to-implement-parallel-assortment
Notice Smallest Array Value:
http://www.cplusplus.com/forum/beginner/66021/
Get Total of Array Values:
https://www.tutorialspoint.com/array-sum-in-cplusplus-stl
Passing an Array to a Function:
https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Multidimensional Arrays:
https://www.programiz.com/cpp-programming/multidimensional-arrays
Passing Array to a Role:
https://www.programiz.com/cpp-programming/passing-arrays-part
Would y'all consider making a donation?
If so, hither's how yous can help support my page:
Source: https://jesushilarioh.com/chapter-7-11-exam-grader-tony-gaddis-starting-out-with-c-plus-plus/
0 Response to "C++ From Control Structures Through Objects Chapter 7 Review"
Post a Comment