Comparing two dimentional array with one dimensional array

#include <iostream>
using namespace std;

int main () {
int student1mark = 0, student2mark = 0, student3mark = 0, student4mark = 0, student5mark = 0, student6mark = 0, student7mark = 0, student8mark = 0, student9mark = 0, student10mark = 0;
char studentanswer[10][10] = {{'A','A','A','A','A','A','A','A','A','A'}, {'B','B','B','B','B','B','B','B','B','B'},
{'B','C','A','D','B','B','B','C','B','A'}, {'A','C','A','D','B','B','B','C','B','A'}, {'A','B','A','D','B','A','B','C','B','A'},
{'C','C','C','C','C','C','C','C','C','C'}, {'D','D','D','D','D','D','D','D','D','D'}, {'A','D','D','D','D','A','D','D','D','D'},
{'D','A','D','D','D','D','D','D','D','D'}, {'D','D','D','D','A','A','A','A','A','A'} };
char correctanswer[10] = {'A','A','A','A','A','A','A','A','A','A'};


for( int x = 0; x!= 10; ++x ) {
for( int y = 0; y!= 10; ++y ){

cout << studentanswer[x][y] << ", ";
}
endl(cout);

}endl(cout);

for( int y = 0; y < 10; ++y ){

cout << correctanswer[y] << ", ";
} endl(cout);


for( int y = 0; y < 10; ++y ){


if (studentanswer[0][y] == correctanswer[y]){
student1mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[1][y] == correctanswer[y]){
student2mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[2][y] == correctanswer[y]){
student3mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[3][y] == correctanswer[y]){
student3mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[4][y] == correctanswer[y]){
student4mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[5][y] == correctanswer[y]){
student5mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[6][y] == correctanswer[y]){
student6mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[7][y] == correctanswer[y]){
student7mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[8][y] == correctanswer[y]){
student8mark += 1; }

}

for( int y = 0; y < 10; ++y ){

if (studentanswer[9][y] == correctanswer[y]){
student9mark += 1; }

}


cout << "\nStudent 1 correct answers is:" << student1mark << endl;
cout << "Student 2 correct answers is:" << student2mark << endl;
cout << "Student 3 correct answers is:" << student3mark << endl;
cout << "Student 4 correct answers is:" << student4mark << endl;
cout << "Student 5 correct answers is:" << student5mark << endl;
cout << "Student 6 correct answers is:" << student6mark << endl;
cout << "Student 7 correct answers is:" << student7mark << endl;
cout << "Student 8 correct answers is:" << student8mark << endl;
cout << "Student 9 correct answers is:" << student9mark << endl;
cout << "Student 10 correct answers is:" << student10mark << endl;
}


Hello b0ss im new here.
I am trying to write a program that can grade multiple choice question but it doesn't seem to show the correct result.
Pls help me b0ss
Look closer.
1
2
3
4
5
6
7
8
9
10
11
12
if (studentanswer[2][y] == correctanswer[y]){
student3mark += 1; }

}

for( int y = 0; y < 10; ++y ){


if (studentanswer[3][y] == correctanswer[y]){
student3mark += 1; }

}


Why not use an array of student marks instead of making 10 separate variables/loops? It's kinda ridiculous, and, as you can see, error prone. What would you have done if you had 100 students?
Last edited on
Hello b0ss how do you set the variable mark back to zero after the first student answer have been evaluated ?

#include <iostream>
using namespace std;

int main () {
int mark, d;
char studentanswer[10][10] = {{'A','A','A','A','A','A','A','A','A','A'}, {'B','B','B','B','B','B','B','B','B','B'},
{'B','C','A','D','B','B','B','C','B','A'}, {'A','C','A','D','B','B','B','C','B','A'}, {'A','B','A','D','B','A','B','C','B','A'},
{'C','C','C','C','C','C','C','C','C','C'}, {'D','D','D','D','D','D','D','D','D','D'}, {'A','D','D','D','D','A','D','D','D','D'},
{'D','A','D','D','D','D','D','D','D','D'}, {'D','D','D','D','A','A','A','A','A','A'} };

char correctanswer[10] = {'A','A','A','A','A','A','A','A','A','A'};


for( int x = 0; x!= 10; ++x ) {
for( int y = 0; y!= 10; ++y ){

cout << studentanswer[x][y] << ", ";
}
endl(cout);

}endl(cout);


for( int x = 0; x < 10; ++x ){
for( int y = 0; y < 10; ++y ){

if (studentanswer[x][y] == correctanswer[y]){
mark += 1;
}
}
cout << "Student " << d << " correct answers is: "<< mark << endl;
d++;

}
Thank you b0ss for your help
Topic archived. No new replies allowed.