quiz حل الأسئلة الجامعية manage_search الأرشيف

تم الحل ✓
categoryهندسة الحاسبات schoolبكالوريوس event_available2026-07-15

السؤال

Transcribed Image Text:

Assignment 9 Pointers, malloc, free memory 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. Submit a single file named pointers.c 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. 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 int main() 6- { 7 int numberOfQuestionstoTake; 8 int *answer; 9 int *op1; 10 int *op2; 11 int *result; 12 int x = 0; 13 int correctAnswers = 0; 14 15 printf("\nMath Quiz\n\n"); 16 printf("I shall 1st ask you to for a number (keep it within 3 -10)\n"); 17 18 19 20 2222222222 21 23 24 25 26 27 op2 = (int 28 calloc(numberOfQuestionstoTake, sizeof(int)); calloc(numberOfQuestionstoTake, sizeof(int)); 29 printf("I shall create a simple math addition quiz of that size for you\n\n"); printf("Enter # of problems: "); scanf("%d", &numberOfQuestionstoTake); //printf("Ok, I shall first start by creating 4 arrays of size %d\n", numberOfQuestionstoTake); printf("Ok, based on your wish to take a QUIZ of %d questions, \n" "let me first allocate enough memory to hold question data.\n", numberOfQuestionstoTake); Based on the number of questions the user wishes to take, allocate enough memory to hold question data. op1 (int calloc(numberOfQuestions to Take, sizeof(int)); (int result = (int *calloc(numberOfQuestions to Take, sizeof(int)); 30 30 31 32- { 33 34 35 if (op1 == NULL || op2 == NULL || answer NULL ||result == NULL) printf("\nOut of Memory!\n"); return -1; } // end if 36 37 printf("\nI have created 4 arrays dynamically each of size %d\n", numberOfQuestionstoTake); 38 39 printf("\t1. Array (1) will store the 1st randomly genererated 2 digit operand 1\n"); printf("\t2. Array (2) will store the 2nd randomly genererated 2 digit operand 2\n"); 40 printf("\t3. Array (3) will store the answer the user enters to the addition of (1) and (2)\n"); 41 42 printf("\t4. Array (4) will store the machine calculated/correct answer to the addition of (1) and (2)\n\n"); printf("\t(3) and (4) are to compare and check how many answers did the user get right or wrong\n\n"); 43 44 45 46 47 48 49 printf("Now iterate %d times and at each iteration\n", numberOfQuestions to Take); printf("\t1. generate a 2 digit number randomly and store it in Array 1\n"); printf("\t2. generate another 2 digit number randomly and store it in Array 2\n"); printf("\t3. Present (1) + (2) above to the user and store their answer in Array 3\n"); printf("\t4. Store the calculated answer of (1) + (2) above in Array 4\n"); printf("...\n"); 50 51 52 53 54 55 56 57 58 59 60 free(op1); 61 62 63 64 65 printf("Fill up the rest\n"); // Missing code goes here printf("\t1. Video help 1 -> https://www.youtube.com/watch?v=QCe2ks9b8YI\n"); printf("\t2. Video help 2 -> https://www.youtube.com/watch?v=2-06KEpV630\n"); // printf("...\n\n"); //free memory printf("Free the dynamically allocated memory arrays\n"); free(op2); free(answer); free(result); printf("Done freeing the dynamically allocated memory\n"); 66 } //end main 67 68

check_circle الجواب — حل مفصل خطوة بخطوة

hourglass_top