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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started