944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5907
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2005
0

2-D array

Expand Post »
hi, I know it's me again.
It's HW, and I'm supposed to write a program that grades an example of 20 questions. I'm supposed to store the correct answers in an array. And then ask the user to enter the student's answers for each of the 20 questions, whic should be stored in another array. Then if the student got more than 15 questions right, I have to display a message that says "PASS". Moreover, if the choices they enter is not A, B, C, or D, I have to tell them to enter it again.
So far, I only got to the part where student has to enter his answer, but I don't know how to do it, since it a two dimensational array.

can anyone help?

this is my code so far
//driver's license Exam

#include <iostream.h>

void StudentAns(char [20][2]);
char choice, A, B, C, D;

void main(void)
{
char key[20][2] = { {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, 'B'}, {18, 'B'}, {19, 'D'}, {20, 'A'} };

StudentAns(key);
}

void StudentAns(char answer[20][2])
{
for (int quest = 1; quest <=20; quest++)
{
cout << "Please enter your choice for Question " << quest << endl;
cin >> choice;
do
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 19th, 2005
0

Re: 2-D array

why do you need to have this as a 2d array?
if you just want the numbers 1-20, you can change the function StudentAns to:
C++ Syntax (Toggle Plain Text)
  1. void StudentAns(char answer[20])
  2. {
  3. for (int quest = 1; quest <=20; quest++)
  4. {
  5. cout << "Please enter your choice for Question " << quest << endl;
  6. cin >> choice;
  7. do
  8. {
  9. cout << "You must enter A, B, C, or D.\n";
  10. cin >> choice;
  11. }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
  12. answer[quest-1] = choice;
  13. }
  14. }
and remeber to make your char key[20][2] to char key[20], and do the nessasery changes in it.

and: main shuld return int, and use iostream instead for iostream.h
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 19th, 2005
0

Re: 2-D array

Hey
Ok I've thought of this before...you know to make it a 1-D array not 2-D array. the thing is my answer key, which is in the main function, is 2-D. How the heck am I supposed to compare 1-D array to a 2-D array. I have to grade the Student Answers!
and what do you mean "main should use int"?
This is my code now:
// Project #7 Driver's License Exam

#include <iostream.h>

void StudentAns(char [20]);
char choice, A, B, C, D;

void main(void)
{
char key[20][2] = { {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, 'B'}, {18, 'B'}, {19, 'D'}, {20, 'A'} };

StudentAns(answer);
}

void StudentAns(char answer[20])
{
for (int quest = 1; quest <=20; quest++)
{
cout << "Please enter your choice for Question " << quest << endl;
cin >> choice;
do
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
answer[quest-1] = choice;
}
}
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 19th, 2005
0

Re: 2-D array

make the key 1d?
like:
C++ Syntax (Toggle Plain Text)
  1. char key[20]= {'B', 'D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','B', 'B','D','A'};
remove : "change answer[quest-1] = choice;" and put:
C++ Syntax (Toggle Plain Text)
  1. if (choise == answer[quest])
  2. {
  3. // do things that happends when he have the right answer
  4. }

and last change StudentAns(answer); to StudentAns(key);
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 19th, 2005
0

Re: 2-D array

hey, thanks a bunch...your advice gives me an idea to the esolution.
one last question (hopefully), did I do something wrong with this part of my code?
void StudentAns(char answer[20])
{
for (int quest = 1; quest <=20; quest++)
{
cout << "Please enter your choice for Question " << quest << endl;
cin >> choice;
do
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
}

because when I try to execute it, it keeps on popping the sentence "you must enter A, B, C, or D." even if I did enter the right choice. in other words, it keeps iterates.

Do you know what the problem is?
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 20th, 2005
0

Re: 2-D array

Quote originally posted by karen_CSE ...
hey, thanks a bunch...your advice gives me an idea to the esolution.
one last question (hopefully), did I do something wrong with this part of my code?
C++ Syntax (Toggle Plain Text)
  1. void StudentAns(char answer[20])
  2. {
  3. for (int quest = 1; quest <=20; quest++)
  4. {
  5. cout << "Please enter your choice for Question " << quest << endl;
  6. cin >> choice;
  7. do
  8. {
  9. cout << "You must enter A, B, C, or D.\n";
  10. cin >> choice;
  11. }while (choice !='A'|| choice != 'B'|| choice != 'C' || choice != 'D');
  12. }
because when I try to execute it, it keeps on popping the sentence "you must enter A, B, C, or D." even if I did enter the right choice. in other words, it keeps iterates.

Do you know what the problem is?
Yes. You're using logical 'ors'. What you've got is a logic error in the do/while loop.
Whenever you do logical operations, read what you've written aloud.
What your code says is:
"Repeat while the choice is not A or B or C or D."
This is faulty logic for loop termination. Even if you pick A, B,C, or D, A!=B!=C!=D. Ergo, unless you enter something that's simultaneously equivalent to A,B,C, and D, one of your conditions will be positive, resulting in the infinite loop you're noticing.

There are two ways to resolve this.
Try switching from || to &&.
or...
use XOR (exclusive or), ^.
Then, your logical statement becomes:
"Repeat while the choice is not A and not B and not C and not D"
or
"Repeat while the choice is not one of the following answers: A, B, C or D"
respectively.


XOR is best thought of as meaning "One or the other, but not both(more than one, if there are multiple entries)." It's very handy for things like this, and is supposedly a major part of some of the most efficient algorithms known.

For now, you should probably just stick with the &&.

One last consideration is to use DeMorgan's Law (google it yourself)
In this, you could say:
C++ Syntax (Toggle Plain Text)
  1. while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
which is equivalent to your current code with && substituted in.
It reads, by the way, as
"While it is not the case that choice is A, B, C, or D."
Last edited by Drowzee; Jul 20th, 2005 at 3:02 pm. Reason: removing redundancy
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 20th, 2005
0

Re: 2-D array

*signs* I'm near tears here :cry:

I've edited the program and this is what I come up so far.

C++ Syntax (Toggle Plain Text)
  1. //Driver's License Exam
  2.  
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. //prototypes
  12. char StudentAns(int );
  13.  
  14. //global variable
  15. char choice;
  16.  
  17. int main()
  18. {
  19. char key[20] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D', 'B', 'C',
  20. 'D', 'A', 'D', 'C', 'B', 'B', 'D', 'A'};
  21.  
  22. int i;
  23. char answer [20];
  24.  
  25. for(i = 0; i < 20; i++)
  26. {
  27. answer[i] = StudentAns(i);
  28. }
  29.  
  30. return 0;
  31.  
  32. int pos = 0; // initialize pos to point to the 0th index.
  33. if (!strcmp(choice, key[pos]))
  34. cout << "This question is answered incorrectly.\n";
  35. else
  36. { choice =0; // initialize counter
  37. while (choice <=20)
  38. {
  39. cout << "you have answered " << choice++ << "correctly.\n";
  40. }
  41. }
  42.  
  43. if (choice++ >= 15)
  44. {
  45. cout << "you have passed the Driver's License Exam.\n";
  46. cout << "Congratuations!\n";
  47. }
  48. else
  49. {
  50. cout << "You have failed the Driver's License Exam.\n";
  51. cout << "Please try again next time.\n";
  52. }
  53.  
  54. }
  55.  
  56. char StudentAns(int i)
  57. {
  58.  
  59. char choice;
  60.  
  61. cout << "Please enter your choice for Question " << i+1 << endl;
  62. cin >> choice;
  63. choice = toupper(choice);
  64.  
  65. while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
  66. {
  67. cout << "You must enter A, B, C, or D.\n";
  68. cin >> choice;
  69. }
  70.  
  71. return choice;
  72. }
<< moderator edit: added [code][/code] tags -- learn to use them >>


my program, however, is just stubborn and won't compile! It said "strcmp cannot convert parameter 1 to 'const char*' ". What did I do wrong now? did I intialize the pos function correctly?
Can you guys check my counter code please?

thanks in advance,
Karen
Reputation Points: 10
Solved Threads: 0
Light Poster
karen_CSE is offline Offline
47 posts
since Jul 2005
Jul 20th, 2005
0

Re: 2-D array

Quote originally posted by karen_CSE ...
*signs* I'm near tears here :cry:

I've edited the program and this is what I come up so far.
[...]

my program, however, is just stubborn and won't compile! It said "strcmp cannot convert parameter 1 to 'const char*' ". What did I do wrong now? did I intialize the pos function correctly?
Can you guys check my counter code please?

thanks in advance,
Karen
Well, you need to check how to use strcmp correctly. You need to pass it a pointer to a char, a char array, or another string for it to work correcly.
Also, you've got some really weird calls.

C++ Syntax (Toggle Plain Text)
  1. //Driver's License Exam
  2.  
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. //prototypes
  12. char StudentAns(int );
  13.  
  14. //global variable
  15. char choice;
  16.  
  17. int main()
  18. {
  19. char key[20] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D', 'B', 'C',
  20. 'D', 'A', 'D', 'C', 'B', 'B', 'D', 'A'};
  21.  
  22. int i;
  23. char answer [20];
  24.  
  25. for(i = 0; i < 20; i++)
  26. {
  27. answer[i] = StudentAns(i);
  28. }
  29.  
  30. //return 0; //This exits main before anything else below it is called. //You're losing a lot of your code.
  31.  
  32.  
  33. int pos = 0; // initialize pos to point to the 0th index.
  34. if (!strcmp(choice, key[pos])) // You still need to fix this by making choice a pointer.
  35. cout << "This question is answered incorrectly."<<endl;
  36. else
  37. {
  38. choice =0; // initialize counter
  39.  
  40. while (choice <=20)
  41. {
  42. cout << "you have answered " << choice++ << "correctly.\n"; //no idea what you're trying to do here; this will always read 'You have answered 1 correctly', because choice never increments.
  43. }
  44. }
  45.  
  46. if (choice++ >= 15)
  47. {
  48. cout << "you have passed the Driver's License Exam.\n";
  49. cout << "Congratuations!\n";
  50. }
  51. else
  52. {
  53. cout << "You have failed the Driver's License Exam.\n";
  54. cout << "Please try again next time.\n";
  55. }
  56.  
  57. }
  58.  
  59. char StudentAns(int i)
  60. {
  61.  
  62. char choice;
  63.  
  64. /* cout << "Please enter your choice for Question " << i+1 << endl;
  65. cin >> choice;
  66. choice = toupper(choice); */ //this can be condensed in your loop.
  67.  
  68. while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
  69. {
  70. cout << "For Question: "<<i+1<<"\n Answer with A, B, C, or D.\n";
  71. cin >> choice;
  72. choice = toupper(choice);
  73. }
  74. //This should work, saves a bit of code, and makes sure that you don't forget the question you are on.
  75.  
  76. return choice;
  77. }

That's what I can figure out. But for some of your code, I don't know what you're trying to do.

Let's get to basics. Before you write code, you should be aware of what you want the code to do.
In simple sentences, describe your program first, like so:
*****
This program conducts and grades a multiple choice driver's exam.

It will need places to store:
An answer key with 20 answers.
The test-taker's answer(s)
The question number.

It will ask the student to enter answers until every question has a valid response, even if the answer is wrong.
It will then compare the student's answers to the answer key, and if the student has more than 15 of 20 answers correct, they pass. Otherwise, they fail.


*********

That should help you organize your thoughts.

If you're still confused, I'll show you how I'd do it once you give it another try.
I'd suggest you start over with your main function, and this time, have a character array for the student's answers of the same size as your answer key, grading the entire array at once with a simple while loop at the end of the program. That should save you a headache and tears.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 20th, 2005
0

Re: 2-D array

To see it simpler this way in terms of schematics, here we are:

There are two phases in your program. The test-taking phase, and the test-grading phase. Both can be done in a loop, as you have 20 questions to ask, and then 20 to grade, both of which can be done via repetitive action.

Using choice as a global variable is not necessary - it's only needed in asking the questions, which you can put in a method - you already have. You can assign the answers straight to the answers array, which you also already have.

Grading the test should not require strcmp. As both the answer and the key are characters, they should be able to function under the equivalence operator just fine. So here's a little skeleton to go off of in 'pseudo-pseudocode'.

C++ Syntax (Toggle Plain Text)
  1. char ask(int i) {
  2. declare holder for the input
  3. console output the prompt for them
  4. do {
  5. ask them for input, inserted into the holder
  6. } while the input within the holder isn't satisfactory
  7. return the input in form of the holder
  8. }
  9.  
  10. void main() {
  11. declare array, fill with your answer key
  12. declare empty array to store answers
  13. declare an int to hold the number of right answers
  14. for(int i = 0; i < 20; i++) {
  15. fill the empty array slot by slot according to i, using your asking method
  16. }
  17. for(int j = 0; j < 20; j++) {
  18. check each answer against its respective slot in the answer key
  19. if right, add 1 to right answers, otherwise add 0
  20. }
  21. check number of right answers
  22. if more than 15, they pass, else they fail
  23. return 0;
  24. }
  25.  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kazastankas is offline Offline
10 posts
since Jul 2005
Jul 20th, 2005
0

Re: 2-D array

More or less what I was thinking, but I'll still do a complete solution if you need it.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Why won't this compile
Next Thread in C++ Forum Timeline: cin.getline problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC