Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming Write a program that * Asks the user for number of questions in th

ID: 3599012 • Letter: C

Question

C programming

Write a program that

* Asks the user for number of questions in the quiz.

* Generates a quiz which has as many questions as the user entered in (1) above.

* Allocates memory dynamically for storing

- the question (both the operands -> 5 + 6, 5 and 6 are the operands)

- the answer.

- The veracity (correct / incorrect)

* Presents the score card.

* Frees all dynamically allocated memory

SAMPLE OUT PUT

Addition Quiz Enter number of problems that you want to attempt: 5 95 + 81 = 76 72 + 91 = 163 50 + 17 = 67 97 + 14 = 111 90 + 29 = 05 Quiz Results Question 95 81 72 91 50 17 97 14 9029 Correct No Yes Yes Yes No Your Answer 163 67 Your score card is 3 / 5-) 60.00 %

Explanation / Answer


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

int main()
    {
      int nQ, *oprnd1, *oprnd2, *res, i, cQ=0; srand(time(NULL));

      printf(" Addition Quiz Enter number of problems that you want to attempt: ");
      scanf("%d",&nQ);

      oprnd1=malloc( nQ*sizeof(int));
      oprnd2=malloc( nQ*sizeof(int));
      res=malloc( nQ*sizeof(int));

      for(i=1; i<= nQ ; i++)
        {
           oprnd1[i] = (rand()+1)%100;
           oprnd2[i] = (rand()+1)%100;
           printf(" %d + %d = ",oprnd1[i],oprnd2[i]);
           scanf("%d",&res[i]);
        }

      printf(" Quiz Results Question     Your Answer    Correct");

      for(i=1; i<= nQ ; i++)
        {
           printf(" %2d + %2d      %-3d            %s",oprnd1[i],oprnd2[i],res[i],
             (oprnd1[i]+oprnd2[i]==res[i])?"Yes":"No");
           cQ= cQ + ((oprnd1[i]+oprnd2[i]==res[i])?1:0);
        }
      printf(" ===================================");
      printf(" Your score card is %d / %d -> %.2f%%", cQ, nQ, (100.0*cQ)/nQ);
      printf(" ===================================");

      free(oprnd1); free(oprnd2); free(res);//free the memory
      return 0;
    }