Write a function to add two 1D array elements. Use call by reference and return the sum array elements to the main function to print.
#include <stdio.h>
#include <stdlib.h>
void sumArrayElements(int elements1[],int elements2[],int* sum);
void printArray(int elements[]);
//The start point of the program
int main() {
//two 1D array elements.
int elements1[5]={5,6,9,8,7};
int elements2[5]={7,8,1,2,3};
int* sum=(int*) malloc(sizeof(int)*5);
int i;
// printing elements of the array elements1
printf("elements1 array values: ");
printArray(elements1);
// printing elements of the array elements2
printf("\nelements2 array values: ");
printArray(elements2);
sumArrayElements(elements1, elements2,sum);
// printing elements of the array sum
printf("\nThe sum array elements: ");
printArray(sum);
//delay
getchar();
getchar();
return 0;
}
//calls by reference and returns the sum array elements to the main function to print.
void sumArrayElements(int elements1[],int elements2[],int* sum){
int i;
for(i = 0; i < 5; ++i) {
sum[i]=elements1[i]+elements2[i];
}
}
//prints the array
void printArray(int elements[]){
int i;
for(i = 0; i < 5; ++i) {
printf("%d ", elements[i]);
}
}
Comments
Leave a comment