Write a function int wrong(char key[].char results[]) The function will be used to grade multiple-choice exam.
The exan has 5 questions, each answered with a letter in the range 'a', to f. All the answers for a student are stored in the character array results.
The key-answer is also stored in array key. Both arrays (results) and (key) are passed to the function wrong as parameters.
The function wrong returns the numbers of false answers given by by the student. For example if the key is-{'a','b','a','d",'e'} and the results is {'a','c','a','d','f'}. the function return 2 (since there are 2 wrong answers).
#include <iostream>
using namespace std;
int wrong(char key[], char result[]) {
int count = 0;
for (int i=0; i<5; i++) {
if (key[i] != result[i]) {
count++;
}
}
return count;
}
int main() {
char key[5] = {'a', 'b', 'a', 'd', 'e'};
char results[5] = {'a', 'c', 'a', 'd', 'f'};
cout << "wrong return " << wrong(key, results) << endl;
}
Comments
Leave a comment