118k views
5 votes
Assume a main function contains declarations for three type double arrays-c, d, and e, each with six elements. Also, assume that values have been stored in all array elements. Explain the effect of each valid call to add_arrays. Explain why each invalid call is invalid.

Function to Add Two Arrays
/*
* Adds corresponding elements of arrays arl and ar2, storing the result in
* arsum. Processes first in elements only.
* Pre: First n elements of arl and ar2 are defined. arsum's corresponding
* actual argument has a declared size >= n(n >= 0)
*/
void
add_arrays (const double arl[ ], /* input- */
const double ar2U, /* arrays being added */
double arsum /* output - sum of corresponding elements of arl and ar2 */
int n) /* input - number of element
pairs summed */
{
int i;
/* Adds corresponding elements of arl and ar2 +/ for (i = 0; i < n; ++i)
arsum[i] = arl[i] + ar2[i];

a) a add_arraysarl, ar2, c, 6);
b. add_arrays(c[6], d[6], e[6], 6);
c. add_arrays(c, d, e, 6);
d. add_arrays(c, d, e, 7);
e. add_arrays(c, d, e, 5);
f. add_arrays(c, d, 6,3);

1 Answer

6 votes

Final answer:

The add_arrays function adds corresponding elements of two input arrays. Valid calls require correctly passing the arrays and specifying a number of elements to sum that does not exceed the arrays' sizes.

Step-by-step explanation:

The function add_arrays is designed to add corresponding elements of two arrays and store the result in a third array. Here are explanations for each call:

  • a) add_arrays(c, d, e, 6); - This is a valid call because it passes three arrays with six elements each, adding the first six elements of c and d, storing the results in e.
  • b) add_arrays(c[6], d[6], e[6], 6); - Invalid call. It passes elements instead of arrays, resulting in a compilation error due to incompatible types.
  • c) add_arrays(c, d, e, 6); - This is a repeat of option a) and is a valid call.
  • d) add_arrays(c, d, e, 7); - Invalid call. All arrays only have six elements, thus accessing the seventh would be out of bounds.
  • e) add_arrays(c, d, e, 5); - Valid call. It adds the first five elements of c and d, storing the results in e.
  • f) add_arrays(c, d, 6,3); - Invalid call. The parameters are out of order, and the array size is not specified before the number of elements to sum.

The function assumes arrays have been properly initialized and that the number of elements to be summed does not exceed the array size.

User Sabrina
by
8.2k points