Thread: Comparing Arrays question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    21

    Comparing Arrays question

    Code:
    I need help with an assignment, he never went over arrays in our class and we have to do this assignment. We are only allowed to use do, for, while, if, else. Here is the question. I'm using the dev compiler.
    
    The local driver's License Office has asked you to write a program that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers: 
    1. B 
    2. D
    3. A
    4. A
    5. C
    6. A
    7. B
    8. A
    9. C
    10. D
    11. B
    12. C
    13. D
    14. A
    15. D
    16. C
    17. C
    18. B
    19. D
    20. A
    
    Your program should store the answers shown above in an array. It should then ask the user to enter the student's answers for each of the 20 questions, which should be stored in another array. After the student's answers have been entered, the program should display a message indicating whether the student passed or failed the exam. (A student should answer 15 of the 20 questions correctly to pass the exam.) It should then display the total number of correctly answered questions, total number of incorrectly answered questions, and a list showing the question numbers of te incorrectly answered questions.
    
     Input validation: only accept the letter a,b,c or d as answers
    
    
    
    
    
    
    this is my program so far, i need help:
    
    
    #include <iostream>
    #include <stdlib.h>
    #include<cmath>
    #include<iomanip>
    using namespace std;
    
    int main()
    {       
          
    int count, correct;          
    const int size = 21;
    char your_answers[size];
    char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a', '\0'};
    cout<< " Please enter the students answers. \n" ;
    for (count = 0; count<size; count++)
    {
        cin>> your_answers[count];
        
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're going to need another loop that loops through the both the your_answers array and the answers array and compares the current element in each. You'll then have to do some work about remembering which answers are correct or incorrect.

    Do you have an idea of how that loop might look?

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    One way to do it would be to create a score variable.
    Then create another 'for' loop that goes through until the end of the "answers" array.
    In each iteration of the for loop ( for(int index=0; index<=size-2; index++) ), create an if statement that checks to see if the 'your_answers[index]' == 'answers[index]', if so then add 1 to the score variable.

    After that 'for' loop, check to see if the score is above or equal to 15. If so, the user passed the test, if not, they failed..... I'm itching to write the code for you, but I know I shouldn't.

    EDIT: Never mind about the 'score' variable- I now see that you have a variable called 'correct'. Use that instead of the 'score' variable.
    Last edited by Flaug; 04-13-2009 at 09:26 PM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    flaug i have to use only do, for, if, else and while, cant use dex or index
    and no i have no clue where im goin from here i suck with loops and i keep tryin different things for validation such as 
    
    if (your_answers == answers)
    {
          correct+= 1;
    }
    
    
    
    
    but nothing seems to work, please help

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    write some code for me man
    you know you want to

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    Will this work for you?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {       
          
    	int count, correct = 0;          
    	const int size = 21;
    	char your_answers[size];
    	char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a', '\0'};
    	cout<< " Please enter the students answers. \n" ;
    	for (count = 0; count<size; count++)
    	{
    		cin>> your_answers[count];
    	    
    	}
    
    	for (count=0; count<=size-2;count++)
    	{
    		if (answers[count] == your_answers[count])
    				   correct += 1; //If they got this one right, then add one to 'correct'.
    	}
    
    	if(correct >= 15) //They got 15 right
    		 cout << "You passed the test. Congratulations." << endl;
    	else
    		 cout << "You're a failure." << endl;
    
    return 0;
    }
    EDIT: There were some errors... I updated the code.
    Last edited by Flaug; 04-13-2009 at 10:06 PM.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    it has a problem at the 
    if (answers[count] = your_answers[count])
    {
    correct +=1;
    }

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    In some cases, the compiler will exit out of the console as soon as the program ends. You can prevent this by making a blank string variable and asking the user to input data into the variable at the end.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {       
        string end = " ";
    
    	int count, correct = 0;          
    	const int size = 20;
    	char your_answers[size];
    	char answers[size] = {'b','d','a','a','c','a','b','a','c','d','b','c','d','a','d','c','c','b','d','a'};
    	cout<< " Please enter the students answers. \n" ;
    	for (count = 0; count<=size-1; count++)
    	{
    		cin>> your_answers[count];
    	    
    	}
    
    	for (count=0; count<=size-1;count++)
    	{
    		if (answers[count] == your_answers[count])
    				   correct += 1; //If they got this one right, then add one to 'correct'.
    	}
    
    	if(correct >= 15) //They got 15 right
    		 cout << "You passed the test. Congratulations. (" << correct << " out of 20 correct)" << endl;
    	else
    		 cout << "You're a failure." << endl;
    
    	cin >> end;
    
    return 0;
    }
    The 'size' variable should only be 20.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    What is the error message?

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    Code:
    also i need some way of showing which ones they got wrong
    and i need to put in answer vallidation 
    like so you can only put up an a,b,c, or d answer
    
    i tried
    
    while (your_answers != a,b,c,d)
    {
        cout<< "Incorrect answer, please enter again. \n";
       cin>> your_answers;
    }
    
    
    but it didnt work

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    C:\Users\TimmyD\Desktop\programs\stuffski.cpp In function `int main()':
    23 C:\Users\TimmyD\Desktop\programs\stuffski.cpp `your' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    23 C:\Users\TimmyD\Desktop\programs\stuffski.cpp expected `)' before "answers"

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    the updated version works, now i jus need help on the a, b, c, d validation and displaying all incorrect answers

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    this is annoyingly complicated i hate nested loops

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    I'm working on it.

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    21
    also, i cant use strings, we werent taught that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question about arrays
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 11-08-2006, 03:09 AM
  3. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM