C/arrays
Expert: Zlatko - 11/16/2009
QuestionQUESTION: hello again, first i wanted to thank you for the last response..it helped out a lot.
now i have an assignment due for my programming class. The assignments was to write a program using 2 arrays, one to key in the students answers to a test, and one the held the correct answers to the test. the next step is to display how many questions the student got right and indicate if they passed or not. passing is 15 correct answers. this last part has to be done with a function. i am not sure how to write a function that will compare the answers in 2 arrays? could you please help ? any help would be greatly appreciated
thank you
chris
ANSWER: Hello Christopher.
If I understand correctly, you have 2 arrays. One array has the student's answers, and the other array has the correct answers and you have to go through the arrays and compare each element, and count the number of matching elements.
You didn't really describe how the answers are represented, but I assume they're characters, like in a multiple choice exam.
So you have 2 arrays
#define MAX_ANSWERS 128
char studentAnsers[MAX_ANSWERS];
char realAnswers[MAX_ANSWERS];
and you have a function that compares matches
void compareAnswers(char studentAnswers[], char realAnswers[], int numAnswers)
{
/*
here you have a loop that goes from 0 to numAnswers-1, and compares the individual elements of the arrays.
If the elements match, increment a counter. After the loop print out the statistics or return the counter to
the main program and let the main program print the statistics
*/
}
Show me what you have so far, and I'll help you more.
Best regards
Zlatko
---------- FOLLOW-UP ----------
QUESTION: thank you for your quik response...this is what i have so far...but i am stuck on that and do not know what direction to go
#include <iostream>
using namespace std;
int main()
{
//array with users answers
char studentsAnswers[20];
cout<<"Please enter the students answers"<<endl;
cin>> studentsAnswers[0];
cin>> studentsAnswers[1];
cin>> studentsAnswers[2];
cin>> studentsAnswers[3];
cin>> studentsAnswers[4];
cin>> studentsAnswers[5];
cin>> studentsAnswers[6];
cin>> studentsAnswers[7];
cin>> studentsAnswers[8];
cin>> studentsAnswers[9];
cin>> studentsAnswers[10];
cin>> studentsAnswers[11];
cin>> studentsAnswers[12];
cin>> studentsAnswers[13];
cin>> studentsAnswers[14];
cin>> studentsAnswers[15];
cin>> studentsAnswers[16];
cin>> studentsAnswers[17];
cin>> studentsAnswers[18];
cin>> studentsAnswers[19];
// array holding correct answers
char correctAnswers[20];
correctAnswers[0] = 'B';
correctAnswers[1] = 'D';
correctAnswers[2] = 'A';
correctAnswers[3] = 'A';
correctAnswers[4] = 'C';
correctAnswers[5] = 'A';
correctAnswers[6] = 'B';
correctAnswers[7] = 'A';
correctAnswers[8] = 'C';
correctAnswers[9] = 'D';
correctAnswers[10] = 'B';
correctAnswers[11] = 'C';
correctAnswers[12] = 'D';
correctAnswers[13] = 'A';
correctAnswers[14] = 'D';
correctAnswers[15] = 'C';
correctAnswers[16] = 'C';
correctAnswers[17] = 'B';
correctAnswers[18] = 'D';
correctAnswers[19] = 'A';
return 0;
}
ANSWER: Well OK
You can have a loop in the main to enter the answers like this:
cout<<"Please enter the students answers"<<endl;
for(int i = 0; i < 20; ++i) cin >> studentAnswers[i];
You can also make your correctAnswers array more concise like this:
char correctAnswers[20] = { 'B', 'D', 'A', 'A' .... and so on }
then, also in main, add the call to the function I outlined in my last post like this:
compareAnswers(studentAnswers, correctAnswers, 20);
then create the function, above main:
void compareAnswers(char studentAnswers[], char realAnswers[], int numAnswers)
{
/*
here you have a loop that goes from 0 to numAnswers-1, and compares the individual elements of the arrays.
If the elements match, increment a counter. After the loop print out the statistics or return the counter to
the main program and let the main program print the statistics
*/
}
Now I've shown you how to write a for loop, and how to use a variable to access a particular element in the array, so write the loop outlined in the compareAnswers comment.
Keep trying.
---------- FOLLOW-UP ----------
QUESTION: i hope i am not annoying you, but i am going crazy here trying to figure this out...i have gotten further but am now stuck again..when i try to compile i am getting 2 errors at the bottom of main where i call the function..its saying "no conversion from int to void"
#include <iostream>
using namespace std;
void compareAnswers(char studentAnswers[], char realAnswers[], int numAnswers=0)
{
for(int i = 0; i < 20; i++)
if ( studentAnswers[i] == realAnswers[i] )
numAnswers = numAnswers++;
}
int main()
{
//array with users answers
char studentsAnswers[20];
cout<<"Please enter the students answers"<<endl;
for(int i = 0; i < 20; ++i) cin >>studentsAnswers[i];
// array holding correct answers
char correctAnswers[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
compareAnswers(studentsAnswers, correctAnswers, 20);
if (compareAnswers >= 15 )
cout<<"You answered "<<compareAnswers<<" correct. Congrats! You passed."<<endl;
else
cout<<" You answered "<<compareAnswers<< " correct. Sorry, you did not pass the exam."<<endl;
return 0;
}
AnswerEdit: Sorry, there was an error in the code I sent you. The corrected is below.
Hello Christopher.
No, you are not annoying me.
If you intend to return a value from compareAnswers, you must delcare it as an int. That is the cause of the errors. You need to declare a return value in compareAnswers, and accept the return value in main. Currently you are using the function name as the return value.
Also, for good form, you should pass the length of the arrays to compareAnswers and use that instead of the hardcoded value of 20. That is the purpose of the numAnswers parameter.
Good effort but it could be made easier to use.
Here is the corrected code. make sure you compare it against what you have, and ask questions about what is unclear.
Best regards
Zlatko
#include <iostream>
using namespace std;
int compareAnswers(char studentAnswers[], char realAnswers[], int numAnswers)
{
int correct = 0;
for(int i = 0; i < numAnswers; i++)
{
if ( studentAnswers[i] == realAnswers[i] )
{
correct++;
}
}
return correct;
}
int main()
{
//array with users answers
char studentsAnswers[20];
cout<<"Please enter the students answers"<<endl;
for(int i = 0; i < 20; ++i) cin >>studentsAnswers[i];
// array holding correct answers
char correctAnswers[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
int correct = compareAnswers(studentsAnswers, correctAnswers, 20);
if (correct >= 15 )
cout<<"You answered "<<correct<<" correct. Congrats! You passed."<<endl;
else
cout<<" You answered "<<correct<< " correct. Sorry, you did not pass the exam."<<endl;
return 0;
}