0

I am a c++ beginner. I just want to check students' answers to a set of answer keys. Let say, I have a student and is declare like this:

//Declare and array of student answers;
string student1 [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};

And answer keys are declare like this:

//Declare an array set of answer key
string keys [] = {"A", "B", "C", "A", "B","C","A","B","A","A"};

Imagine answer keys are the correct answers of question from 1 - 10. Then I want to check if the students answer match the declared answer keys:

for(const string &key : keys){
    for(const string &answer : answers){
        if(key == answer){
            cout << "Correct" << endl;
        }else{
            cout << "Wrong" << endl;
        }
    }
}

My first problem is, it gives me the following results:

Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct                                                                                                                                                
Correct 

And the second one is, I want to add 5 more students to check their answer. Thanks.

jreloz
  • 413
  • 4
  • 18

3 Answers3

3

Try to make it one loop:

for(int i = 0;i < 10;i++)
    {
        if (student1[i] == keys[i])
        {
            cout << "Correct" << endl;
        }
        else
        {
            cout << "Wrong" << endl;
        }
    }

For the second one, just make an array of students:

string students[5][10] = {{...},{...},{...},{...},{...}};

Add the new loop:

for (int j = 0; j < 5; j++)

And change the if statement:

if (students[j][i] == keys[i])

I hope you understand.

IshaySela
  • 143
  • 6
1

Let me first check what you want correctly. You have a set of students and a test with a set of correct answers. Each student has a specific list of answers. All you want is to compare the answers of the students to the key answers or the correct answers.

Let's start with student1 in your example. We have ten questions and all of the answers of student1 are correct then why your code is showing more than ten lines?

You made two for loops while you simply need one.

You can proceed like this:

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

            if (student1[i] == keys[i]) {
                std::cout << "Correct " <<student1[i]<<" "<< keys[i]<< std::endl;
            }else std::cout<< "Wrong" <<std::endl;

    }

You simply need to compare the answer i from the first array (student1) to the key answer in the same position i in the second array (keys). You do not need to make two for loops unless I did understand something wrong.

For your second question, it is a vague one. Depending on your own current level in C/C++, you will have many.

If you want to add more students, you can simply declare more students like what you did with student1:

std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
//...

Then you can define an array of arrays as a list of the students.

string list[3][10] = {student1,student2,student3};

Here is a code that tests the results of three students:

#include <iostream>
#include <string>
#include<vector>


using namespace std;

void showResult(const string student1[],const string keys[]){

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

            if (student1[i] == keys[i]) {
                std::cout << "Correct " << std::endl;
            }else std::cout<< "Wrong" <<std::endl;
    }

}


int main(int argc, char** argv) {

    std::string student1[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };
    std::string student2[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };
    std::string student3[] = { "A", "B", "C", "A", "C","C","A","B","A","A" };

    string list[3][10] = {student1,student2,student3};


    std::string keys[] = { "A", "B", "C", "A", "B","C","A","B","A","A" };


    for(int i=0;i<3;i++){
        cout<<"student "<<i+1<<endl;
        for(int j=0;j<10;j++) cout<<list[i][j]<<" ";
        cout<<endl;
        showResult(list[i],keys);
        cout<<endl;
    }



    return 0;
}

Otherwise if you want something more dynamic, you should try Vectors from std::vector. Vectors in C++ are far more versatile than classic arrays. You can read more on this topic in this thread:

c++ array assignment of multiple values

Khaled Adrani
  • 171
  • 3
  • 7
0

You can do this with a for-loop and others have shown this, but you can do this with std::transform and a lambda too, just for fun.

#include <algorithm>
[...]
std::vector<string> correctness;
std::transform(student1,student1+10,keys,std::back_inserter(v),[](const string& a,const string& b){if (a==b) return "Correct"; else return "Wrong";});
for (const auto& ans : correctness) std::cout << ans << endl;
Wolfgang Brehm
  • 1,491
  • 18
  • 21