Thread: making a math quiz program

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    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;
           }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So what is the problem with the code?

    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    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

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    yeah theyre supposed to be pointer arrays. are they declared right as pointer arrays?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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("&#37;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
    Last edited by ssharish2005; 09-17-2007 at 02:44 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by mackieinva View Post
    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]);

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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 *answer;
           int *op1;
           int *op2;
           char *result;
           int x;
           
           
    
    
           
           
           printf("Enter # of problems:");
           scanf("&#37;d", &response);
           
           answer = malloc(response * sizeof(*answer));
           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;
                                          }
           
           if (answer==NULL){
                     printf("\nOut of memory!\n");
                     return;
                     
                            }
           
           for (x = 0; x < response; x++) {
               printf("%d + %d = ",op1[x], op2[x]);
               scanf("%d", &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 \t\t%c\n",op1[x], op2[x], answer[x], result[x]);
                                          }
               
          
           free(answer);
           free(op1);
           free(op2);
           free(result);
           system("PAUSE");
           return 0;
           }
    Last edited by mackieinva; 09-17-2007 at 03:20 PM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by mackieinva View Post
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Help with making a Math Expression DLL
    By MindWorX in forum C Programming
    Replies: 19
    Last Post: 07-19-2007, 11:37 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Simple Math Program
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 03-04-2004, 06:22 PM