By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

Need help getting started with arrays C++

P: 1
I need help with arrays, as in how to set them up for this program I am working on. This is program I am doing.

The State Department of Motor Vehicles (DMV) has asked you to write a program that grades the written portion of the driver’s license exam, which has 20 multiple choice questions. Here are the correct answers:

1.B 5.C 9.C 13.D 17.C
2.D 6.A 10.D 14.A 18.B
3.A 7.B 11.B 15.D 19.D
4.A 8.A 12.C 16.C 20.A

To do this you should create a TestGrader class. The class will have a correct_answers array of 20 characters, which holds the correct test answers. It will have two public member functions that
enable user programs to interact with the class. These are:
setCorrect_answers. The setCorrect_answers function receives a 20-character char array holding the correct answers and copies this information into its correct_answers array.
grade. The grade function receives a 20-character array holding the test taker’s answers and compares each of their answers to the answers held in the correct_answers array.An applicant must correctly answer 15 or more of the 20 questions to pass the exam.

The grade function should print out the following information:
User answers and the correct answer for each question
An "X" across the questions that the user answered incorrectly
a message indicating whether the applicant passed or failed the exam
the number of right answers and the number of wrong answers

The client program that creates and uses a TestGrader object should first make a single
call to setCorrect_answers, passing it a char array containing the 20 correct answers. Once this is done it should allow a test taker’s 20 answers to be entered in a 20-character array. Then it should call the grade function to grade the exam to display the results of the grading process.

I have some code written down, I am just at a loss on how to set up the arrays to finish the program. Thank you in advance, any help would be great!

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <string>
  5. using namespace std;
  7. class TestGrader
  8. {
  9. private:
  10.     char correct_answers[20];
  12. public:
  13.     void grade(const char[], userAnswers);
  14.     void setCorrect_answers(const char[], _corr_answers);
  16. };

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5. #include "TestGrader.h"
  7. // ***************** Function Prototypes ***************
  8. void printUserAnswers(string, const char[], const int);
  10. // ***************** Class tester ***************
  12. int main()
  13.     const int NUM_QUESTIONS = 20;
  14.     // Create a TestGrader object & set its key with correct answers
  15.     char correct_ans[] = {'B', 'D', 'A', 'A', 'C',
  16.                   'A', 'B', 'A', 'C', 'D',
  17.                   'B', 'C', 'D', 'A', 'D',
  18.                      'C', 'C', 'B', 'D', 'A'};
  20.     // Array to hold user 1 and user 2's test answers
  21.     char user1_ans[20] = {'A', 'D', 'A', 'A', 'C', 
  22.                 'A', 'B', 'A', 'C', 'D', 
  23.                 'A', 'C', 'D', 'A', 'D', 
  24.                 'A', 'C', 'B', 'D', 'A' };  
  26.     char user2_ans[20] = { 'A', 'D', 'D', 'A', 'C',
  27.                 'A', 'B', 'D', 'C', 'D',
  28.                 'A', 'C', 'D', 'C', 'C',
  29.                 'A', 'A', 'B', 'D', 'A' };
  31.     TestGrader DMVexam;          
  33.     DMVexam.setCorrect_answers(correct_ans);
  35.     //------------- Print and grade grade User 1's answers ---------
  36.     printUserAnswers("User #1", user1_ans, NUM_QUESTIONS);
  37.     DMVexam.grade(user1_ans);
  39.     //------------- Print and grade grade User 2's answers ---------
  40.     printUserAnswers("User #2", user2_ans, NUM_QUESTIONS);
  41.     DMVexam.grade(user2_ans);
  43.     system("pause");
  44.     return 0;
  45. }
  46. void printUserAnswers(string _username, const char array[], const int size)
  47. {
  48.     cout << "---------------" << _username<< " answers ---------------\n\n";
  51.        cout << "\n-------------------------\n";
  52. }
Nov 4 '15 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 2,396
Take a look at the Arrays Revealed article.
Nov 4 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.