Find the sum of the first half and second half elements of an array | C ++ program to find the sum of the first half elements and second half elements of an array ; Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sum_of_elements ( int arr [ ] , int n ) { int sumfirst = 0 , sumsecond = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) sumfirst += arr [ i ] ; else sumsecond += arr [ i ] ; } cout << " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is ▁ " << sumfirst << endl ; cout << " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is ▁ " << sumsecond << endl ; } int main ( ) { int arr [ ] = { 20 , 30 , 60 , 10 , 25 , 15 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sum_of_elements ( arr , n ) ; return 0 ; }