cant figure out how to use parallell arrays for this program

Aug 2, 2008 at 5:22pm
Hi, im trying to write a program that askes a user to enter in test grades and than checks them to see if ther right or wrong. than says you have Q blah blah and blah wrong .. ect. i need to use parallell 1d arrays for this.. into what im supposed to be doing?.. if not is there any way i can assign each if statment as like true or false and at the end call all the true ones or false ones so i can state you got x questions wrong?? any help would be greatly appreciated.. thanks,

#include <iostream>
using namespace std;
int main()
{

const int num_questions=20;
char userval[num_questions];
int count; // loop counter

cout<<"automated grader,, please enter in testees grades"<<endl;
cout<<"remember use caps only"<<endl;
cout<<endl;

for (count=0; count <num_questions; count++)
{
cout<< "enter the answer to question "<<(count + 1)<< " : ";
cin>> userval[count];
}

int num1 = 65;
int num2 = 66;
int num3 = 67;
int num4 = 68;

if (userval[0]!= static_cast<char>(num2))
cout<<"you got the question 1 wrong correct answer is "<<static_cast<char>(num2)<<endl;
if (userval[1]!= static_cast<char>(num4))
cout<<"you got the question 2 wrong correct answer is "<<static_cast<char>(num4)<<endl;
if (userval[2]!= static_cast<char>(num1))
cout<<"you got the question 3 wrong correct answer is "<<static_cast<char>(num1)<<endl;
if (userval[3]!= static_cast<char>(num1))
cout<<"you got the question 4 wrong correct answer is "<<static_cast<char>(num1)<<endl;
if (userval[4]!= static_cast<char>(num3))
cout<<"you got the question 5 wrong correct answer is "<<static_cast<char>(num3)<<endl;
if (userval[5]!= static_cast<char>(num1))
cout<<"you got the question 6 wrong correct answer is "<<static_cast<char>(num1)<<endl;
if (userval[6]!= static_cast<char>(num2))
cout<<"you got the question 7 wrong correct answer is "<<static_cast<char>(num2)<<endl;
if (userval[7]!= static_cast<char>(num1))
cout<<"you got the question 8 wrong correct answer is "<<static_cast<char>(num1)<<endl;
if (userval[8]!= static_cast<char>(num3))
cout<<"you got the question 9 wrong correct answer is "<<static_cast<char>(num3)<<endl;
if (userval[9]!= static_cast<char>(num4))
cout<<"you got the question 10 wrong correct answer is "<<static_cast<char>(num4)<<endl;
if (userval[10]!= static_cast<char>(num2))
cout<<"you got the question 11 wrong correct answer is "<<static_cast<char>(num2)<<endl;
if (userval[11]!= static_cast<char>(num3))
cout<<"you got the question 12 wrong correct answer is "<<static_cast<char>(num3)<<endl;
if (userval[12]!= static_cast<char>(num4))
cout<<"you got the question 13wrong correct answer is "<<static_cast<char>(num4)<<endl;
if (userval[13]!= static_cast<char>(num1))
cout<<"you got the question 14 wrong correct answer is "<<static_cast<char>(num1)<<endl;
if (userval[14]!= static_cast<char>(num4))
cout<<"you got the question 15 wrong correct answer is "<<static_cast<char>(num4)<<endl;
if (userval[15]!= static_cast<char>(num3))
cout<<"you got the question 16 wrong correct answer is "<<static_cast<char>(num3)<<endl;
if (userval[16]!= static_cast<char>(num3))
cout<<"you got the question 17 wrong correct answer is "<<static_cast<char>(num3)<<endl;
if (userval[17]!= static_cast<char>(num2))
cout<<"you got the question 18 wrong correct answer is "<<static_cast<char>(num2)<<endl;
if (userval[18]!= static_cast<char>(num4))
cout<<"you got the question 19 wrong correct answer is "<<static_cast<char>(num4)<<endl;
if (userval[19]!= static_cast<char>(num1))
cout<<"you got the question 20 wrong correct answer is "<<static_cast<char>(num1)<<endl;

return (0);
}
Aug 4, 2008 at 10:46am
if i understood what "parallell arrays" are corectly this is the solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;
int main()
{

const int num_questions=20;
int userval[num_questions];
int count; // loop counter

cout<<"automated grader,, please enter in testees grades"<<endl;
cout<<"remember use caps only"<<endl;
cout<<endl;

for (count=0; count <num_questions; count++)
{
	cout<< "enter the answer to question "<<(count + 1)<< " : ";
	cin>> userval[count];
}

int n1 = 65;
int n2 = 66;
int n3 = 67;
int n4 = 68;

int answers[]={n2,n4,n1,n1,n3,n1,n2,n1,n3,n4,n2,n3,n4,n1,n4,n3,n3,n2,n4,n1};

for (count=0; count <num_questions; count++)
{
	if(userval[count]!=answers[count])
		cout<<"you got the question "<<count+1<<" wrong. correct4 answer is "<<answers[count]<<endl;
}
return 0;
}


btw use # when inserting code.
Topic archived. No new replies allowed.