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);
/* Dont cast malloc */
answer= (int *) malloc(response * sizeof(int));
if (answer==NULL){
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 & */
scanf("%d\n", &answer[x]);
}
/* You havnlt allocated memeory for result array */
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);
/* 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