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

O t Page: 801 of 1067 - + Automatic Zoom + * >> #include #include #include void

ID: 3702502 • Letter: O

Question

O t Page: 801 of 1067 - + Automatic Zoom + * >> #include #include #include void show_array(const double ar[], int n); double * new_d_array(int n, ...); int main() double * pl; double * p2; pl = new_d_array(5, 1.2, 2.3, 3.4, 4.5, 5.6); p2 = new_d_array(4, 100.0, 20.00, 8.08, -1890.0); show_array(pl, 5); show_array(p2, 4); free(pl); free(p2); return 0; The new_d_array() function takes an int argument and a variable number of double arguments. The function returns a pointer to a block of memory allocated by malloc(). The int argument indicates the number of elements to be in the dynamic array, and the double values are used to initialize the elements, with the first value being assigned to the first element, and so on. Complete the program by providing the code for show_ array() and new_d_array().

Explanation / Answer

double *new_d_array(int n, ...) {
   va_list valist;
   va_start(valist, n);
   int i;
   double *arr = (double *)malloc(sizeof(double)*n);
  
   for (i = 0; i < n; i++) {
       arr[i] = va_arg(valist, double);
   }
   return arr;
}