Question: Using C++ Just like we can allocate run-time memory in C++ by using newand delete, we can allocate run-time memory in C Programming aswell. For
Using C++
Just like we can allocate run-time memory in C++ by using newand delete, we can allocate run-time memory in C Programming aswell. For this, we use malloc() and/or calloc() functions toallocate memory. For example, int *ptr=(int*)malloc(10,sizeof(int)); 2 This allocates space for a dynamic array of 10integers. To deallocate this memory, we need to use the free()function. For example, free(ptr) Unlike with new and delete, if theallocate dynamic memory is not deemed sufficient, we can increasethe allocated run-time memory. This is done via realloc() function.For example, ptr=realloc(ptr, 20*sizeof(int)); This statement nowincreases the allocated memory size to an array of 20 integers.Now, write a C-program that calculates the average of doubles. Yourprogram will prompt the user for the value of double in a loop. Theloop needs to terminate if the user decides there are no morenumbers to enter. The user does not know how many numbers are therein total, so your allocated must grow as the user decides to entermore numbers. Initially, allocate run-time memory for one doubleonly and obtain the value of the double from the user. In the loop,use the realloc() function to increase the memory allocated. Keepgrowing your memory and reading new double values as long as theuser wants to enter new numbers. For every entry, you needs toupdate the sum and the average of the values entered by the user,and display the updated average. After writing the code, save it asLabEC_A.c. Compile and test it using the gcc compiler, not the g++compiler and upload on Blackboard. Remember, you have to useprintf() and scanf() for I/O, cin/cout will not work inCprogramming.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
