Question: For this assignment, we shall exercise dynamic memory allocation using malloc and then free the memory after it has been utilized. We shall create a
For this assignment, we shall exercise dynamic memory allocation using malloc and then free the memory after it has been utilized. We shall create a mathematics addition quiz which is expandable to as many questions as the user inputs.
Write a program that 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:
1. Two arrays for storing each of the operands. (example -> 5 + 6, 5 and 6 are the operands, 76 + 45, 76 and 45 are operands) Use rand() function to generate random 2 digit number ( video tutorial to generate the dice numbers (1-6) randomly, can adapt that to generate a 2 random 2 digit integer) Fill the operands array with random 2 digit numbers (video tutorial to fill an array with random numbers)
2. One array for storing the answer the user enters for each question presented
3. One array for storing the correct calculated answer to verify against what the user entered in (2) So 4 dynamically created arrays of the size the user entered in response to (1) above.
Presents the score card.
Frees all dynamically allocated memory Some Hints: Use the attached starter file to help you get started.
Attached starter file:
#define _CRT_SECURE_NO_WARNINGS
#include
#include
main()
{
int numberOfQuestionstoTake;
int *answer;
int *op1;
int *op2;
char *result;
int x;
int correctAnswers = 0;
printf(" Math Quiz ");
printf("I shall first ask you to for a number (keep it within 3 -10) ");
printf("I shall create a simple math addition quiz of that size for you ");
printf("Enter # of problems: ");
scanf("%d", &numberOfQuestionstoTake);
//printf("Ok, I shall first start by creating 4 arrays of size %d ", numberOfQuestionstoTake);
printf("Ok, based on your wish to take a QUIZ of %d questions, let me first allocate enough memory to hold question data. ", numberOfQuestionstoTake);
/* Based on the number of questions the user wishes to take, allocate enough memory to hold question data. */
op1 = (int *)calloc(numberOfQuestionstoTake, sizeof(int));
op2 = (int *)calloc(numberOfQuestionstoTake, sizeof(int));
answer = (int *)calloc(numberOfQuestionstoTake, sizeof(int));
result = (char *)calloc(numberOfQuestionstoTake, sizeof(char));
if (op1 == NULL || op2 == NULL || answer == NULL || result == NULL) {
printf(" Out of Memory! ");
return;
} // end if
printf(" All right. I have created 4 arrays dynamically each of size %d ", numberOfQuestionstoTake);
printf("\t1. Array (1) will store the first randomly genererated 2 digit operand 1 ");
printf("\t2. Array (2) will store the second randomly genererated 2 digit operand 2 ");
printf("\t3. Array (3) will store the answer the user enters to the addition of (1) and (2) ");
printf("\t4. Array (4) will store the machine calculated/ expected answer to the addition of (1) and (2) ");
printf(" \t(3) and (4) is to compare and check how many answers did the user get right or wrong ");
printf(" Now you would iterate %d times and at each iteration ", numberOfQuestionstoTake);
printf("\t1. generate a 2 digit number randomly and store it in Array 1 ");
printf("\t1. generate another 2 digit number randomly and store it in Array 2 ");
printf("\t1. Present (1) + (2) above to the user and store their answer in Array 3 ");
printf("\t1. Store the calculated answer of (1) + (2) above in Array 4 ");
printf("... ");
printf("Fill up the rest ");
printf("\t1. Video help 1 -> https://www.youtube.com/watch?v=QCe2ks9b8YI ");
printf("\t2. Video help 2 -> https://www.youtube.com/watch?v=2-o6KEpV630 ");
//
printf("... ");
//free memory
printf(" Do not forget to free the dynamically allocated memory ");
free(op1);
free(op2);
free(answer);
free(result);
printf("Done freeing the dynamically allocated memory ");
} // end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
