Write a function to add two 1D array elements. Use call by reference and return the sum array elements to the main function to printWrite 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>
int sum_def(int* arr)
{
return arr[0] + arr[1];
}
int sum(int* arr, int fir, int sec)
{
return arr[fir] + arr[sec];
}
int main()
{
int* arr = (int*)malloc(4 * sizeof(int));
printf("Enter 1st number: ");
scanf("%d", &arr[0]);
printf("Enter 2nd number: ");
scanf("%d", &arr[1]);
printf("%d + %d = %d", arr[0], arr[1], sum_def(arr));
printf("\n\nEnter 3rd number: ");
scanf("%d", &arr[2]);
printf("Enter 4th number: ");
scanf("%d", &arr[3]);
printf("%d + %d = %d", arr[2], arr[3], sum(arr, 2, 3));
return 0;
}
Comments
Leave a comment