Write a C function named apply_all that expects two arrays of integers and their sizes and dynamically allocates a new array of integers whose size is the product of the 2 array sizes. The function should loop through the second array and multiplies each element across each element of array 1 and store the product in the newly created array. The function should return a pointer to the newly allocated array.

Respuesta :

Answer:

The function C is as follows:

int *apply_all(int arr1[], int arr2[], int size) {

  int result[size];

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

      result[i] = arr1[i] * arr2[i];

      printf(" %d", result[i]);

  }

  return result;

}

Explanation:

This line defines the function

int *apply_all(int arr1[], int arr2[], int size) {

This declares an array that holds the result of the function

  int result[size];

This iterates through the array

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

This multiplies the corresponding elements of both arrays

      result[i] = arr1[i] * arr2[i];

This prints the result of the multiplication

      printf(" %d", result[i]);

  }

This return the a pointer to the new array

  return result;

}

See attachment for complete program that includes the main

Ver imagen MrRoyal
ACCESS MORE
EDU ACCESS
Universidad de Mexico