Answer to Question #174402 in C for Hanbun

Question #174402

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. 


1
Expert's answer
2021-03-23T01:44:46-0400
#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]);
	}
}

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