Question: C++ why does it only return the first value of my array? trying to use a function to find the sum of an array. #include
C++
why does it only return the first value of my array?
trying to use a function to find the sum of an array.
#include
using namespace std;
int sum(int array[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += array[i]; return sum; } }
int main() { int array[50]; int n;
cout << "Enter the number of elements: " << endl; cin >> n;
cout << "Enter the elements for the array: " << endl; for (int i = 0; i < n; i++) { cin >> array[i]; }
int total = sum(array, n);
cout << "the sum of the array is " << total << endl;
system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
