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

Write a Program in C that includes the functions attached in the pictures( The t

ID: 3605003 • Letter: W

Question

Write a Program in C that includes the functions attached in the pictures( The topic of the homework is Dynamic Memory Allocation) . I have completed part B and D and need help with parts A, C, and E. All help is appreciated.

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

A)

double * allocate(unsigned els) {

return (double *)malloc(sizeof(double)*els);

}

E)

double *enormous(unsigned *retEls, const double a[], unsigned aEls) {

unsigned j,i = 0, count = 0;

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

if (a[i] >= 100.0)

count++;

/*allocate memory*/

double *retArr = allocate(count);

j = 0;

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

if (a[i] >= 100.0) {

retArr[j] = a[i];

j++;

}

*retEls = count;

return retArr;

}

C)

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

double *result = (double *)malloc(sizeof(double)*els);

unsigned i;

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

result[i] = f(i);

return result;

}

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