C++ Program to find the sum of array elements using functions and pointers


In this program, You will learn how to find the sum of array elements using functions and pointers in C++.


Sum of array elements using function and pointers

sumOfArray(arr,size,&sum);


Example: How to find the sum of array elements using functions and pointers in C++

Run it Now
#include<iostream>
using namespace std;

void sumOfArray(int arr[], int n, int *s) {

    for (int i = 0; i < n; i++) {
        *s = *s + arr[i];
    }
}

int main() {

    int arr[10], i, n, s = 0;

    cout << "Enter Size of an array : ";
    cin>>n;

    cout << "\nEnter array elements : ";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    sumOfArray(arr, n, &s);

    cout << "\nSum of array elements is : " << s;

    return 0;
}

Output:

Enter Size of an array : 5
Enter array elements : 3 4 5 6 7
Sum of array elements is : 25