Answer to Question #174653 in C for saivenkat

Question #174653

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


1
Expert's answer
2021-03-23T12:16:39-0400
#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;
}

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