Find the sum of all elements of an array with and without using functions.

Find the sum of all elements of an array with and without using functions.

How to pass array to a C++ function ?

            Passing array to a C++ function is same as you pass other variables or values. Just one modification is needed when you pass array to a function, you need to pass array name and size both while calling the function in the main function. In the actual function you need to declare two variable one for array and other for size. See the syntax below 


main function(){

//calling a function

function(array_name, array size);

}


function_type function(variable 1 to for array with square brackets, variable 2 for array size){

}

example: int demo(inr arr[], int s){

                }

where arr[] is for array & s is for size


Note: Square brackets must be given with array variable in function definition



Program to find sum of all elements of array using function

        Take the array size and array elements from the user and pass them to the function. In function take the sum variable and initialize it to zero. Access each array element using a loop and add it to the sum variable. Once the sum is calculated return it to the main function. In the main function print the sum.


Code

#include <iostream>

using namespace std;


int sum_of_array(int arr[], int size){              //Function to calculate sum of all elements of array

    int sum=0;                                                          //to store and return sum

    for(int i=0;i<size;i++){                                    //loop to access all elements of array

        sum=sum+arr[i];                                        //adds each element of array

    }

    return sum;                                                 //returns sum

}


int main() {

    cout<<"Enter array Size "<<endl;                                                                 //Requesting size

    int array_size;                                                                                              //to store size

    cin>>array_size;                                                                                        //reading size

    

    cout<<"Enter elements of array "<<endl;                       //Requesting array elements 

    int array[array_size];                                                                          //array declaration

        

    for(int i=0;i<array_size;i++){                   //loop to read and store each element of array

        cin>>array[i];

    }

    

    int s=sum_of_array(array, array_size);                    //calling sum of array function 

    

    cout<<"The sum of array is: "<<s<<endl;                                         //print the sum

    

    return 0;

}


Input 

5
1 2 3 4 5

Output

The sum of array is: 15



C++ program to find sum of all elements of array without using functions

        Take the array size and array elements from the user. Take the sum variable to store the final sum and initialize it to zero. Using loop add each element to sum. Finally, print the total sum.

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;                                          //array size
    cin>>n;
    
    int a[n];                                     //array declaration
    
    for(int i=0;i<n;i++){                       //reading the array elements
        cin>>a[i];
    }
    
    
    
    int sum=0;                                             //set sum to zero
    
    for(int i=0;i<n;i++){                               //traversing the array
        sum=sum+a[i];
    }
    
    cout<<"The sum is: "<<sum;                     //printing the sum
    
    return 0;
}




Input 

5
1 2 3 4 5

Output

The sum is: 15

Post a Comment

0 Comments