# making a math quiz program

• 09-17-2007
mackieinva
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 *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));

printf("\nOut of memory!\n");
return;

}

for (x = 0; x < response; x++) {
printf("%d + %d = ",op1[x], op2[x]);
}

for (x = 0; x < response; x++) {
result[x] = 'c';  }
result[x] = 'w';
}

printf("\nQuiz Results");

for (x = 0; x < response; x++) {
printf("%d + %d \t%d \%s",op1[x], op2[x], answer[x], result[x]);
}

system("PAUSE");
return 0;
}

• 09-17-2007
ssharish2005
So what is the problem with the code?

ssharish
• 09-17-2007
mackieinva
i try to run it and it just crashes, im not sure where i messed up at. was hoping it might stick out to someone here who could offer advice. thanks again.
• 09-17-2007
robwhit
Quote:

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)
those aren't arrays.

http://www.cprogramming.com/tutorial/c/lesson8.html
http://www.cprogramming.com/tutorial/c/lesson6.html
• 09-17-2007
mackieinva
yeah theyre supposed to be pointer arrays. are they declared right as pointer arrays?
• 09-17-2007
ssharish2005
Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int response;
int *op1;
int *op2;
char *result;
int num1 = 20;
int num2 = 30;
int x;

*op1 = num1;
*op2 = num2;

printf("Enter # of problems:");
scanf("&#37;d", &response);

/* Dont cast malloc */
answer= (int *) malloc(response * sizeof(int));

printf("\nOut of memory!\n");
return;

}

for (x = 0; x < response; x++) {
/* Have you noticed this, op1, op2 is not a array. It is just pointing to a some int's see
above */

printf("%d + %d = ",op1[x], op2[x]);
/* You missed & */
}

/* You havnlt allocated memeory for result array */
for (x = 0; x < response; x++) {
{
result[x] = 'c';
}
result[x] = 'w';
}

printf("\nQuiz Results");

for (x = 0; x < response; x++) {
printf("%d + %d \t%d \%s",op1[x], op2[x], answer[x], result[x]);
}

/* Dont use system() function instead use getchar() */
system("PAUSE");
return 0;
}

I have commented few things on your code. Have a look at it. And you need a proper indendation.

ssharish
• 09-17-2007
matsp
Code:

*op1 = num1;
*op2 = num2;

printf("Enter # of problems:");
scanf("%d", &response);

answer= (int *) malloc(response * sizeof(int));

op1 and op2 aren't pointing at anything.

Why are you asking the user for "response" and then allocating that many ints, when there is really only one question?

--
Mats
• 09-17-2007
robwhit
Quote:

Originally Posted by mackieinva
yeah theyre supposed to be pointer arrays. are they declared right as pointer arrays?

no, they are pointers, not an array of pointers or a pointer to an array, because you haven't pointed it to an array. maybe you wanted to do something like this?
Code:

int *pointer_to array;
pointer_to_array = malloc(15 * sizeof(*pointer_to_array)); /*array of 15 elements, 0-14*/
pointer_to_array[3] = 234;
printf("&#37;d\n,pointer_to_array[3]);

• 09-17-2007
mackieinva
yeah i was trying to make it so the array would constantly store the number in it with op1 having each cell in the array with 20 and op2 with 30 but i guess that wasnt the right way of doing it.

There is one question but its how many times the question is asked. So if i say i want 5 questions I get 5 math questions, but in this example it will always be the same question.

thanks for the little things ssharish, definately a step in the right direction for me.
• 09-17-2007
mackieinva
ooooh thanks for the help rob. now op1 and op2 have the right number assigned to them. also i allocated memory for the other arrays...dont really know why i didnt do that before. it runs now, takes in the answer, and then prints it again for you. now i just need to make it so it prints out Yes if answer was correct and No if answer is incorrect.

would it be ok to make a for loop to run through the result array, and if the array = w print no for wrong and if it = c have it print correct?

thanks again for the help rob.

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{

int response;
int *op1;
int *op2;
char *result;
int x;

printf("Enter # of problems:");
scanf("&#37;d", &response);

op1 = malloc(response * sizeof (*op1));
op2 = malloc(response * sizeof (*op2));
result = malloc(response * sizeof (*result));

for (x = 0; x < response; x++) {
op1[x] = 20;
op2[x] = 30;
}

printf("\nOut of memory!\n");
return;

}

for (x = 0; x < response; x++) {
printf("%d + %d = ",op1[x], op2[x]);
}

for (x = 0; x < response; x++) {
result[x] = 'c';  }
result[x] = 'w';
}

printf("\nQuiz Results");

for (x = 0; x < response; x++) {
printf("%d + %d    \t%d \t\t%c\n",op1[x], op2[x], answer[x], result[x]);
}

free(op1);
free(op2);
free(result);
system("PAUSE");
return 0;
}

• 09-17-2007
robwhit
Quote:

Originally Posted by mackieinva
would it be ok to make a for loop to run through the result array, and if the array = w print no for wrong and if it = c have it print correct?

yes. why not try it?