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, 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

## Recommended Answers

It's s bit confusing, but it seems to be asking you to apply sumN() to each element of your array. This would be a good spot to use a loop over each of the #size inputs. Keep it in main() and model it after your input loop.

Great! Yeah, you were right on the cusp of it.

I think your assignments are slightly different. The OP had the assignment to input 50 values and output 50 more numbers, each of which was the sum from 1 to N, where N was the original number.

So for the OP if the original array was [2 6 ...] …

If your for loop looks like

``````for (int i = 0;i<size;i++) //the key is the int i portion
{

}

cout<<i; //won't work, i doesn't exist.``````

the variable i will exist only within the for loop. This is useful if you have multiple for loops and you …

## All 13 Replies

It's s bit confusing, but it seems to be asking you to apply sumN() to each element of your array. This would be a good spot to use a loop over each of the #size inputs. Keep it in main() and model it after your input loop.

I'm trying to do this at the moment, but I cant seem to attach each input to sumN function.

I'm trying to use this code now

``````cout << "The sum of all your elements is:\n";

for (int t=0; t<size; t++) {
cout << sumN(t) << endl;
}``````

However, in this case it just gives me numbers from 0 to size.

I also tried

``````for (t=0; t<a[i]; t++) {
cout << sumN(t) << endl;
}``````

and it just started to run a lot of lines of nubers non stop..

Thank you, I got it just now!

``````for (i=0; i<size; i++) {
cout << sumN(a[i]) << endl;
}``````

Great! Yeah, you were right on the cusp of it.

I am also doing this assignment. My code is below, it is very similar to the code above and I cannot figure out why it won't run properly, it just gives 50 random numbers. Any help would be appreciated.

``````#include <iostream>

using namespace std;

int sumN(int n) {
int sum = 0;
for (int i=0; i<=n; ++i){
sum += i;
}
return (sum);
}

int main()
{
int size;

cout << "This program will create an array of a size of your choice (Maximum of 50)." << endl; //Explains program.
cout << "It will ask you for values and store them in the array." <<endl;
cout << "Finally it will add them up and ouput them.\n" <<endl;
cout << "Please enter a size for the array: "; //Asks for the size of the array.
cin >> size; //Stores the chosen size into integer size.

int a; //Declares the array of integers.

if(size<=50) { //Runs if size chosen is less than or equal to 50.
cout << "\nPlease enter each of the " << size << " values for the array.\n" << endl; //Asks for the values.

for(int i=0;i<size;i++) { //Starts a loop asking for values 1 to size.
cout << "Value " << i+1 << ": ";
cin >> a[i]; //Stores values in the array.
cout << endl;
}

cout << "\nThe sum of the values is:" << endl; //Ouputs the sum.
for (int i=0; i<a[size]; i++) {
cout << sumN(i) << endl;
}
}

else {
cout << "The maximum size of the array is 50"; //Runs if a number above 50 is inputted.
}
return 0; //Ends program.
}``````
``````cout << "\nThe sum of the values is:" << endl; //Ouputs the sum.
for (int i=0; i<a[size]; i++) {
cout << sumN(a[i]) << endl;``````

Try this maybe?

and also, declare int i outside the for loop, helped me.

I think your assignments are slightly different. The OP had the assignment to input 50 values and output 50 more numbers, each of which was the sum from 1 to N, where N was the original number.

So for the OP if the original array was [2 6 ...] the output is 1+2=3 and 1+2+3+4+5+6=21 etc.

@NU8 Your assignment is to sum the original array of 50 elements together. So, for the same array [2 6 ...] your sum is 2+6+... See if you can tweak your function to do that instead.

Hi, I'm also doing this assignment. An error keeps coming up in this part of my code:
for (i=0; i<size; i++)
{cout << sumN(a) << endl;
}

It says: name lookup of 'i' changed for ISO 'for' scoping.
But I don't know what that means. Could anyone help me please?

If your for loop looks like

``````for (int i = 0;i<size;i++) //the key is the int i portion
{

}

cout<<i; //won't work, i doesn't exist.``````

the variable i will exist only within the for loop. This is useful if you have multiple for loops and you want to reuse i as a variable in all of them, it would be permitted.

Having

``````int i;
for (i = 0;i<size;i++)
{

}

cout<<i; //you can access i out here because it is within scope``````

So your error message is from the compiler saying (I think) that even though it is not declared that way, "i" is being considered local to that for loop like in the first case I showed.

Thanks a million, it runs now. Something is still missing though. I ran the code and it all worked ok but i entered two numbers; 2 and 6 but instead of outputting 1+2=3 and 1+2+3+4+5+6=21 it just outputs 2. It says: The sum of all the elements is two. My code is:

[

``````#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, size;

cout << "How many values would you like to enter? (maximum 50): ";
cin >> size;

cout << "Please enter the values: ";
for (int i=0; i<size; i++)
cin >> a[i];

cout << "The sum of all the elements is: ";
int i;
for (i = 0;i<size;i++)
{
}
cout<<i;

return 0;
}``````

]

I'd greatly appreciate the help if you could. I don't know why its outputting like this, could be right in front of me but i have no idea.

and sorry ignore the [] and the beginning and end of the code

Please use [code] //code here [/code] tags next time (also, really you should have started your own thread even though it's the same class problem)

You misunderstood me, the cout<<i part was just for illustrating the scope issues. Instead, in that loop, call your function on a and output the results.

Ok sorry I didn't know how to do it. Sorry. Will do in future.

Thanks a million. Code works perfectly now. Thankyou so much for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.