RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 3547 | Replies: 15
Reply
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

2-D array

  #1  
Jul 19th, 2005
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');
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: 2-D array

  #2  
Jul 19th, 2005
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:
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;
    }
}
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
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: 2-D array

  #3  
Jul 19th, 2005
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;
}
}
Reply With Quote  
Join Date: Jun 2005
Posts: 60
Reputation: zyruz is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
zyruz zyruz is offline Offline
Junior Poster in Training

Re: 2-D array

  #4  
Jul 19th, 2005
make the key 1d?
like:
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:
if (choise == answer[quest])
{
// do things that happends when he have the right answer
} 

and last change StudentAns(answer); to StudentAns(key);
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: 2-D array

  #5  
Jul 19th, 2005
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?
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: 2-D array

  #6  
Jul 20th, 2005
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?
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?

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:
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
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: 2-D array

  #7  
Jul 20th, 2005
*signs* I'm near tears here :cry:

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

//Driver's License Exam


#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;

//prototypes
char StudentAns(int );

//global variable
char choice;

int main()
{
	char key[20] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D', 'B', 'C',
		'D', 'A', 'D', 'C', 'B', 'B', 'D', 'A'};

	int i; 
	char answer [20]; 

	for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
	return 0;

	int pos = 0;    // initialize pos to point to the 0th index.
	if (!strcmp(choice, key[pos]))
			cout << "This  question is answered incorrectly.\n";
	else 
	{ choice =0;  // initialize counter
	while (choice <=20)
	{
		cout << "you have answered " << choice++ << "correctly.\n";
	}
	}

	if (choice++ >= 15)
	{
		cout << "you have passed the Driver's License Exam.\n";
		cout << "Congratuations!\n";
	}
	else
	{
		cout << "You have failed the Driver's License Exam.\n";
		cout << "Please try again next time.\n";
	}

}

char StudentAns(int i)
{
	
	char choice;
		
	cout << "Please enter your choice for Question " << i+1 << endl;
	cin >> choice;
	choice = toupper(choice);
		
	while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
	{
		cout << "You must enter A, B, C, or D.\n";
		cin >> choice;
	}

	return choice;
}
<< 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
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: 2-D array

  #8  
Jul 20th, 2005
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.

//Driver's License Exam


#include <iostream>
#include <stdio.h>
#include <string.h>


using namespace std;

//prototypes
char StudentAns(int );

//global variable
char choice;

int main()
{
	char key[20] = { 'B','D', 'A','A', 'C','A', 'B', 'A', 'C', 'D', 'B', 'C',
		'D', 'A', 'D', 'C', 'B', 'B', 'D', 'A'};

	int i; 
	char answer [20]; 

	for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
	//return 0; //This exits main before anything else below it is called.         //You're losing a lot of your code. 


	int pos = 0;    // initialize pos to point to the 0th index.
	if (!strcmp(choice, key[pos])) // You still need to fix this by making choice a pointer.
			cout << "This  question is answered incorrectly."<<endl;
	else 
	{ 
             choice =0;  // initialize counter

	     while (choice <=20)
	      {
		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.
	      }
	}

	if (choice++ >= 15)
	{
		cout << "you have passed the Driver's License Exam.\n";
		cout << "Congratuations!\n";
	}
	else
	{
		cout << "You have failed the Driver's License Exam.\n";
		cout << "Please try again next time.\n";
	}

}

char StudentAns(int i)
{
	
	char choice;
		
/*	cout << "Please enter your choice for Question " << i+1 << endl;
	cin >> choice;
	choice = toupper(choice); */ //this can be condensed in your loop.
		
	while(!(choice =='A'||choice=='B'||choice=='C'||choice=='D'))
	{
		cout << "For Question: "<<i+1<<"\n Answer with A, B, C, or D.\n";
		cin >> choice;
                choice = toupper(choice);
	}
 //This should work, saves a bit of code, and makes sure that you don't forget the question you are on.

	return choice;
}

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.
Reply With Quote  
Join Date: Jul 2005
Posts: 47
Reputation: karen_CSE is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
karen_CSE karen_CSE is offline Offline
Light Poster

Re: 2-D array

  #9  
Jul 20th, 2005
HEY, DROWZEE
that was the exact thing I did! wow, without help, too! I figured it out right before i check this website for, like, the hundredth time! Man, don't I sound arrogant! i'm just so darn happy right now. I've been on that project for like 4 days.
anyway, thanks for all your help Drowzee, you were great!

Karen
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: 2-D array

  #10  
Jul 20th, 2005
No problem.
Hah. Just got another 'reminder' that I need to become much better at C/C++ from one of the employees here.
No matter how much you learn, there's always someone better than you to draw on...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:29 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC