making a math quiz program
Hi all. The next one im trying to do is math quiz. Im reading about maloc and caloc and realoc and found this example to do with it. It asks the user how many problems they want to do (it will always be 30 + 20). These are the only variables im allowed to declare for the program.
int response; - to hold the users answer
int *answer; - an array to hold the answer for each question
int *op1; - an array to hold the first operator of each question.
int *op2; - an array to hold the second operator of each question.
char *result; - a character array to hold either c(correct)
-or w(wrong) for each question.
int x; - for loop counter
using array pointers and maloc to set the size of the array. thanks for all help, im sure im annoying to some of yall but im getting better i promise, i almost understand what im doing instead of typing stuff and hoping for the best :)
so i thought i was going in the right direction with this. initializing everything, asking for number of questions and malocating the array for it. Then using the response number in the for loops to print the two number arrays and retrieve the answer. after that, use a for loop to go through the answer array and determine right or wrong. following that with a loop that prints out the arrays.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int response;
int *answer;
int *op1;
int *op2;
char *result;
int num1 = 20;
int num2 = 30;
int x;
*op1 = num1;
*op2 = num2;
printf("Enter # of problems:");
scanf("%d", &response);
answer= (int *) malloc(response * sizeof(int));
if (answer==NULL){
printf("\nOut of memory!\n");
return;
}
for (x = 0; x < response; x++) {
printf("%d + %d = ",op1[x], op2[x]);
scanf("%d\n", answer[x]);
}
for (x = 0; x < response; x++) {
if (answer[x] == 50) {
result[x] = 'c'; }
else if (answer[x] != 50)
result[x] = 'w';
}
printf("\nQuiz Results");
printf("\nQuestion \tYour Answer \tCorrect\n");
for (x = 0; x < response; x++) {
printf("%d + %d \t%d \%s",op1[x], op2[x], answer[x], result[x]);
}
free(answer);
system("PAUSE");
return 0;
}