Answer to Question #251305 in C for Samzzy

Question #251305
write and test a function that sets each element in an array to the sum of the corresponding elements in two other arrays. for example, if array 1 has the values { 2, 4, 5, 8 } and array 2 has the values { 1, 0, 4, 6 }, the function should assign array 3 the values { 3, 4, 9, 14 }. be sure to include all the tests you performed to demonstrate that this function works as specified.
1
Expert's answer
2021-10-14T07:55:18-0400
// c
int *sum_arrays(int *a, int *b) {
  int a_size = sizeof(a) / sizeof(int);
  int b_size = sizeof(b) / sizeof(int);
  int n = min(a_size, b_size);
  int *arr = malloc(sizeof(int) * n);
  for (int i = 0; i < n; i++) {
    arr[i] = a[i] + b[i];
  }
  return arr;
}

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