63.5k views
5 votes
Write a function called arraySum() that takes two arguments: an integer array and the number of elements in the array. Have the function return as its result the sum of the elements in the array

User SmartTree
by
5.2k points

1 Answer

6 votes

Answer:

//Function is written using C++ Programming Language

// Comments are used for explanatory purpose

// The main method is not included

using namespace std;

// Declare arraySum with two arguments. The first represents the array name while the other

// Represents the number of elements

int arraySum(int arr[], int n)

{

int total = 0; // declare and Initialise total to 0

// Iterate through the array to calculate sum

for (int i = 0; i < n; i++) {

total += arr[i];

}

return total;

}

User Nouar
by
5.5k points