Q2: Suppose you have a main() with three local arrays, all the same size and type (say
float). The first two are already initialized to values. Write a function called
sumArrays() that accepts the addresses of the three arrays as arguments; adds the contents
of the first two arrays together, element by element; and places the results in the
third array before returning. A fourth argument to this function can carry the size of the
arrays. Use pointer notation throughout; the only place you need brackets is in defining
void sumArrays(float *a, float *b, float *c, int n) {
for (int i = 0; i < n; i ++)
c[i]=a[i]+b[i];
}
int main() {
int n = 3;
float arr1[n] {2,3.0f,5}, arr2[n] {-2,4.5,2.0994}, arr3[n];
sumArrays(arr1, arr2, arr3, n);
return 0;
}
Comments
Leave a comment