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

Write a program in C. (The homework topic is Dynamic Memory Allocation) I need h

ID: 3605040 • Letter: W

Question

Write a program in C. (The homework topic is Dynamic Memory Allocation) I need help with calling functions in main. I have called the function double * reverse(const double a[], unsigned els); but need help calling the other 4. Full homework instructions along with my code is below.

Homework: Dynam ic memory allocation For each of these functions: If the function is passed els (the number of elements), your function may assume without checking that els>0 Every attempt to allocate memory should be accompanied by a check whether the allocation was successful. If not, c&d; (complain and die by calling the die function.) Unless the program complains and dies, all memory dynamically allocated (with malloc, calloc, or realloc) must be properly deallocated by calling free. This might mean free'ing in the function, or it might mean free'ing in the caller, depending on the function's job. Of course, be sure that your program never accesses memory that has been deallocated. A. doubleallocate(unsigned els); Return a pointer to a dynamically allocated array of els doubles. On exit, the array is full of unpredictable garbage. (and don't forget what you just read about allocation failure). B. unsigned fib(unsigned els); Return a pointer to a dynamically allocated array of els unsigneds. Fill the array with Fibonacci numbers, starting with setting the first element (at position 0) to 0. For example fib(10) should return an array of 10 unsigneds, containing 0,11,2, 3, 5, 8, 13 21, 34,3. As you probably recal, the first 2 Fibonacci numbers are 0 and 1, and after that, every Fibonacci number is the sum of the last 2 Fibonacci numbers. C. double func(unsigned els, double f(double x)): func is like fib, except that instead of necessarily using the fibonacci function, func lets the caller specify the function. For instance, if the function half were available: double half(double x) return x/2;) func(10, half) [0.0. 0.5, 1.0, 1.5, 2.0,2.5, 3.0, 3.5, 4.0, 4.5] then would return a dynamically allocated array of 10 doubles, containing Each element is set equal to the result of calling the function with the value of the index

Explanation / Answer

Few suggestion :

Change declaration of fib function like below:

double fib(double els) ;

Change declaration of enormous function like below:

double * func(unsigned els, double (* f)(double x) );

Here is Main code:

int main() {

double *ret;

double rev[] = { 4.3, 4.5, 5.7, 600.3, 100.25 };

/*Displaying use of enormous function*/

unsigned count;

ret = enormous(&count, rev, 5);

unsigned i, j;

cout << "Enromous elements: ";

for (i = 0; i < count; i++)

cout << ret[i] << " ";

cout << endl;

/*Displaying use of func functions and fib functions*/

ret = func(5, fib);

cout << "Fibonacci numbers: ";

for (i = 0; i < 5; i++)

cout << ret[i] << " ";

cout << endl;

/*Displaying use of calling allocate functions*/

//allocate funciton was used in enormous function so it is done

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote