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

Re: 2-D array

  #11  
Jul 20th, 2005
Actually, it's more efficient to check each answer as it goes, so only one loop is needed. Then again, that depends on the nature of grading one wants. I mean, you could do neat things like automatically fail them when they get 6 wrong, or auto-pass at 15 right.
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

  #12  
Jul 20th, 2005
OK, I gave it one more try, And this is my last solution, I have no idea how else to do this

//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

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


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0, total1, total2;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
//SHOW RESULT;
	cout << "THIS IS YOUR RESULT.\n";
	for(i = 0; i < 20; i++)
	{
		if(answer[i] == key[i])
		{
			cout << "this answer is correct.\n";
			correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER
		}
		else
		{
			cout << answer [i] << " is an incorrect choice.\n";
				incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER
		}
	}

	total1 += correct++; //TOTAL UP THE CORRECT ANSWERS
	total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS

	cout << "you have answered " << total1 << " questions correctly.\n";
	cout << "You have answered " << total2 << " questions incorrectly.\n";

	if (total1 >= 15)
	{
		cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
	}
	else 
	{
		cout << "\nSorry. You have failed the Driver's License Exam.\n";
		cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n";
	}

	return 0;
}


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;
}

I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?

OMG. I think I'm fainting!
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

  #13  
Jul 20th, 2005
Originally Posted by karen_CSE

//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

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


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0; //, total1, total2;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
	{
		answer[i] = StudentAns(i);		
	}
	
//SHOW RESULT;
	cout << "THIS IS YOUR RESULT.\n";
	for(i = 0; i < 20; i++)
        {
            cout<<"Question "<<(i+1)<<":"<<endl;
		if(answer[i] == key[i])
		{
			cout << "was answered is correct.\n";
			correct++; //INTITIALIZE COUNTER FOR CORRECT ANSWER
		}
		else
		{
		cout << answer [i] << " is an incorrect choice.\n";
		incorrect++; //INNITIALIZE COUNTER FOR INCORRECT ANSWER
		}
	}

//	total1 += correct++; //TOTAL UP THE CORRECT ANSWERS
//	total2 += incorrect++; //TOTAL UP THE INCORRECT ANSWERS

	cout << "you have answered " << correct << " questions correctly.\n";
	cout << "You have answered " << incorrect << " questions incorrectly.\n";

	if (correct >= 15)
	{
		cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
	}
	else 
	{
		cout << "\nSorry. You have failed the Driver's License Exam.\n";
		cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n";
	}

	return 0;
}


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;
                choice = toupper(choice);
	}

	return choice;
}

I tried to compile this and it worked out pretty well except for the totaling up the correct answers part. Can you guys please check it?

OMG. I think I'm fainting!

Try that edit. Looks pretty good to me, but I didn't run it myself.

One problem was that on lower-case answers that weren't valid (for example: e), you didn't convert the case to make a lower-case a upper case, which made your code inconsistent.
But mainly: You had too many variables. If you're incrementing correct and incorrect, why do you need total1 and total2?
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

  #14  
Jul 20th, 2005
HEY GUYS I FIGURED IT ALL OUT!!!!!!! YAY FOR ME!!! :mrgreen:

I'm posting this online anyway, for any beginner, like myself, that have programs that are similar to mine. you guys can use this as a guide. I find an example a lot easier to understand than explanation :lol:

[code]
//Driver's License Exam;
//This program first enter the answer key with 20 answers
//then it will ask the student to enter his/her answer
//The program then totals up the number of questions answered correctly and incorrectly
//The student must answer at least 15 questions right in order to pass the exam.

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


using namespace std;

char StudentAns(int );

int main()
{
int i, correct = 0, incorrect =0;

char *key = "BDAACABACDBCDADCBBDA"; //THE ANSWER KEY

char answer[20]; //DECLARE THE STUDENT ANSWER ARRAY

//STUDENT ENTER ANSWERS.
for(i = 0; i < 20; i++)
{
answer[i] = StudentAns(i);
}

//SHOW RESULT;
cout << "THIS IS YOUR RESULT.\n";
for(i = 0; i < 20; i++)
{
if(answer[i] == key[i])
{
cout << "this answer is correct.\n";
correct++; //update COUNTER FOR CORRECT ANSWER
}
else
{
cout << answer [i] << " is an incorrect choice.\n";
incorrect++; //update COUNTER FOR INCORRECT ANSWER
}
}

cout << "You have answered " << correct << " questions correctly.\n";
cout << "You have answered " << incorrect << " questions incorrectly.\n";

if (correct >= 15)
{
cout << "\nCONGRATUATIONS! YOU HAVE PASSED THE DRIVER'S LICENSE TEST.\n";
}
else
{
cout << "\nSorry. You have failed the Driver's License Exam.\n";
cout << "\nYou must answer at least 15 questions right in order to pass the exam.\n\n";
}

return 0;
}


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;
}


Thank you all who have been really patient with me throughout this HW question. Your advices were an immense help. I'm so grateful I discovered this forums. you guys are THE BEST!!!!! :mrgreen:

THANKS AGAIN!
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

  #15  
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

  #16  
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:42 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