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

Ch.10 PE 2 (MODIFIED, ENHANCED) Write a program that initializes an array of 5 d

ID: 3759435 • Letter: C

Question

Ch.10 PE 2 (MODIFIED, ENHANCED) Write a program that initializes an array of 5 doubles, then copies the contents of the array into 2 other arrays. Each copy should be done by a different function you write, one of which uses subscripts, the other pointers. Finally, write a function to print a variable-length array of doubles, and use it to display all 3 arrays. The program definitions will include: double source[]= {1.1, 2.2, 3.3, 4.4, 5.5}; //for example double targetA[5], targetB[5]; void copy_arr(double src[], double tar[], int size); //array copy w/subscripts void copy_ptr(double *src, double *tar, int size); //array copy w/pointers

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

void copy_arr(double src[], double tar[], int size) {

  

for (int i=0; i<size; i++) {

  

tar[i] = src[i];

  

}

  

}

void copy_ptr(double *src, double *tar, int size) {

  

for (int i=0; i<size; i++) {

  

tar[i] = src[i];

  

}

  

}

void print(double *arr, int size) {

  

for (int i=0; i<size; i++) {

printf("%.2f, ", arr[i]);

}

  

printf(" ");

}

int main() {

  

double source[]= {1.1, 2.2, 3.3, 4.4, 5.5};

  

  

  

double targetA[5], targetB[5];

  

  

  

copy_arr(source, targetA, 5);

  

copy_ptr(source, targetB, 5);

  

  

  

print(source, 5);

  

print(targetA, 5);

  

print(targetB, 5);

  

  

  

printf(" ");

return 0;

  

}