Answer to Question #277159 in C for Narayan

Question #277159

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

1
Expert's answer
2021-12-08T16:30:39-0500
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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS