25.5k views
2 votes
Assume a main function contains declarations for three type double arrays named c, d and e, each holding 6 elements. Also suppose a function defined as:

int add_arrays ( const double ar1[], const double ar2[], double ar_sum [], int n

The function adds the corresponding elements of ar1 and ar2 and stores the sums in the ar_sum array. Assume arrays c, d and e are full (i.e., each contains six numbers and that ar1, ar2 and ar_sum are compatible with c, d and e. Consider the following calls to add arrays; some of them are valid, some of them are not. If a call is valid, explain what the result of the call will be; if the call is invalid, explain why. (attach a separate page with your answers and explanations. Make your explanations brief and to the point)

a) add_arrays(ar1, 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, 3);
f) add_arrays(e, d, c, 6);
g) add_arrays(c, c, c, 6);
h) add_arrays(c, d, 6, 3);
i) add_arrays(c, d, e, 7);
j) add_arrays(c, d, e, c[1]; i) if c[1]=4.3; and ii) if c[1]=91.7 ?

User Jcern
by
8.9k points

1 Answer

0 votes

Final answer:

Some calls of the add_arrays function are invalid due to incorrect argument types or mismatched array sizes.

Valid calls correctly add the corresponding elements of the input arrays and store the results in a third array.

Step-by-step explanation:

The provided question centers around the use of the function add_arrays in C or C++ that adds the corresponding elements of two double arrays and stores the result in a third array.

Let's evaluate each function call:

a) add_arrays(ar1, ar2, c, 6); is invalid since ar1 and ar2 are not defined in this context as arrays. We would need them to be initialized similar to how arrays c, d, and e are.

b) add_arrays(c[6], d[6], e[6], 6); is invalid because it is passing double values instead of the arrays themselves.

c) add_arrays(c, d, e, 6); is valid and would add each element in arrays c and d and store the sums in array e.

d) add_arrays(c, d, e, 7); is invalid since the arrays are declared to hold 6 elements, not 7.

e) add_arrays(c, d, e, 3); is valid, but it would only add the first three elements of arrays c and d.

f) add_arrays(e, d, c, 6); is valid, this time storing the sum of arrays e and d into array c.

g) add_arrays(c, c, c, 6); is valid and doubles the elements of array c, storing the result back in c.

h) add_arrays(c, d, 6, 3); is invalid due to incorrect argument order.

i) add_arrays(c, d, e, 7); is a repeat of d) and is invalid for the same reason mentioned before.

j) Assuming the intention was add_arrays(c, d, e, c[1]); i) If c[1]=4.3; and ii) If c[1]=91.7; in both cases, this call is invalid because the fourth parameter should be an integer size, not a double value from the arrays.

User Jordan Ryder
by
7.8k points