Hi again guys, last time I posted a problem I got a lot of help... and here I'm back again with new issue.
This is the assignment:
Contains a function called sumN() which takes an int n as an argument and returns an int which is the sum of all integers between 1 and n.
In the main() asks the user:
How many values (s)he wants to enter (maximum 50);
Asks for the values and stores them into an array int a[].
Prints to the console sumN(a) for all the elements of a[] that have been entered by the user.
I have nearly finished it, except I cant figure out how to make function read each number in the array and calculate the sum from 1 to each number. From my understanding of the second part, this is whats required.
So far I have this
#include <iostream>
using namespace std;
int sumN(int n){
int sum = 0;
for (int i=1; i<=n; ++i){
sum += i;
}
return (sum);
}
int main()
{
int a[50], size; // variables
// Program asks for number of values which the user would like to enter.
cout << "How many values would you like to enter? (maximum 50)\n";
cin >> size;
// Program asks for values and stores each value into an array.
cout << "Please enter the values:\n";
for (int i=0; i<size; i++)
cin >> a[i];
// Prints to the console sumN(a[i]) for all the elements of a[]
// that have been entered by the user.
cout << "The sum of all your elements is:\n";
cout << sumN(a[i]) << endl;
return 0;
}
Please help me... We also covered pointers in the lectures, maybe I need to use them here somehow? If so, please explain as they are quite confusing :(
Kind regards,
Yuugib