w3resource

C Exercises: Compute the sum of the two given arrays of integers of length 3 and find the array which has the largest sum


45. Compare Sum of Two Arrays

Write a C program to compute the sum of the two given arrays of integers, length 3 and find the array that has the largest sum.

C Code:

#include <stdio.h>
#include <stdlib.h>

// Function prototype for 'print_array'
void print_array(int parray[], int size);

int main(void){
    // Declaration and initialization of variables
    int arr_size = 3;

    // Declaration and initialization of arrays
    int nums1[] = {10, 20, -30};
    int nums2[] = {10, 20, 30};

    // Printing elements in the first original array
    printf("Elements in original array are: ");  
    print_array(nums1, arr_size);    

    // Printing elements in the second original array
    printf("Elements in original array are: ");  
    print_array(nums2, arr_size);    

    // Declaration of result array and loop index 'i'
    int result[arr_size], i;

    // Comparing sums of elements in the two arrays
    if (nums1[0] + nums1[1] + nums1[2] >= nums2[0] + nums2[1] + nums2[2])
    {
        // Copying elements from the first array to the result array
        for (i = 0; i < arr_size; i++) {
            result[i] = nums1[i];
        }   
    }
    else
    {
        // Copying elements from the second array to the result array
        for (i = 0; i < arr_size; i++) {
            result[i] = nums2[i];
        }   
    }

    // Printing the array with the largest sum
    printf("The array which has the largest sum.: ");  
    print_array(result, arr_size);        
} 

// Definition of the 'print_array' function
void print_array(int parray[], int size)
{
    int i;      
    for( i=0; i<size-1; i++)  
    {  
        // Printing each element with a comma and a space
        printf("%d, ", parray[i]);  
    } 
    // Printing the last element without a comma and space
    printf("%d ", parray[i]);  
    // Printing a new line to separate the elements
    printf("\n");   
}

Sample Output:

Elements in original array are: 10, 20, -30 
Elements in original array are: 10, 20, 30 
The array which has the largest sum.: 10, 20, 30

Pictorial Presentation:

C Programming Algorithm: Compute the sum of the two given arrays of integers of length 3 and find the array which has the largest sum

Flowchart:

C Programming Algorithm Flowchart: Compute the sum of the two given arrays of integers of length 3 and find the array which has the largest sum

For more Practice: Solve these Related Problems:

  • Write a C program to compare the product of two arrays of length 3 and print the array with the larger product.
  • Write a C program to calculate and compare the average of two 3-element arrays.
  • Write a C program to sum elements of two arrays and return the array with the smallest sum.
  • Write a C program to check two arrays of length 3 and return true if the sum of the first array is a multiple of the sum of the second.

Go to:


PREV : Modify Array: 5 Followed by 7 to 5 and 1.
NEXT : Extract Middle Elements from Even Array.

C Programming Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.