## foxmulder

Hi!

I put this simple, I got an assignment that's following:

Write a function that return the amount of all integer from zero (0) to a number that was given as an argument.

How do I do, I dont even understand the question?

## Ancient Dragon 5,243

Must mean integers, because there are an infinite quantity of floats or doubles between two numbers. The word amount is ambiguous. Does it mean sum or quantity ? For example, there are 11 numbers betwen 0 and 10, but the sum is 1+2+3...+10 = 55

## firstPerson 761

Do you have a sample run? Is it something like this you think :

``````Enter n : 10
From [0,10] there are 11 different numbers.``````

## foxmulder

Hi!

I mean the sum of all numbers, not the quantity.

## firstPerson 761

Hi!

I mean the sum of all numbers, not the quantity.

Then you do understand the question. So start at it.

## foxmulder

Hi!

Hehe, I understood that from the beginning, just expressed me a bit wrong with saying amount :P

Really appreciate any kind of example-code that I can work on.

## Ancient Dragon 5,243

I already posted example (not code). Just create a loop and keep running counter of current sum.

## foxmulder

Im feeling very lost here, I just cant come up with anything right now :/

## firstPerson 761

here is puedo code :

``````#include<iostream>
using namespace std;

int main()
{
//create a variable called sum = 0;
// create a variable called max = 0;
//get user input for the max amount
// make it equal to max

//for i  = 0 to max
//sum = sum + i;

//print out sum
return 0;
}``````

## foxmulder

Hi again!

Im not so good at this, I tried to transform the peudo code to regular c++ code and came up with this.

``````#include<iostream>

using namespace std;

int main()

{

int min = 0;
int max = 0;
int maxnumber;
int sum;

cout << "Input the max integer: ";
cin >> maxnumber;
maxnumber = max;

for(i  = 0, i > max)

sum = sum + i;

cout << "Sum is: " << sum << endl << endl;

return 0;

}``````

## amrith92 119

First of all, I would highly recommend you go through this tutorial, that'll help you brush up some basics in C++, before starting to make your own programs.

You've got some major problems in the code, like your `for` loop, which has a statement missing. Even before that, there's logically no need for the variables `max` and `min` .

Here's some pseudo-code, that might help you get clearer idea of what you are trying to achieve:

``````Get input from the user, and store in max, i.e cin >> max;
find sum of the first max terms using a for loop
for i = 0, i <= max till i becomes max
increase sum by i
display sum to user``````

-------------------- OR --------------------

If you don't like loops, you could use a direct formula, to find the sum of the first 'n' integers, using the formula:
sum = n*(n+1)/2
Pseudo code for this:

``````Get input from the user, and store in max, i.e cin >> max;
find sum of the first max terms using the formula
sum = max*(max+1)/2;
display sum to user``````

But, as I said before, go through the basics of C++, and loops before trying this out...

Hope this helped!

## Ancient Dragon 5,243

Hi again!

Im not so good at this, I tried to transform the peudo code to regular c++ code and came up with this.

You already have all the code you need -- just put it in a function, return the result.

``````int foo(int x)
{
// put code here
}

int main()
{
int x = foo(5);
}``````

## foxmulder

Im sorry but I dont understand :/ I've already got two main and cant even run the program as it is.. :/

## Ancient Dragon 5,243

Programs can only have one main(). Delete one of the two you have.

Create another function and move the code you posted in main into that function. Then change main to just call that function. The code I posted is just an example of what your program should look like when you are done. foo() is just a generic name that means anything -- don't use it in your program but name it something else.

## samsons17 -4

So,in the code below i use the name sum_of_numbers for the function...
The other thing that you have to do is declare a variable that could take all of the sum inside the loop that read all the numbers until reach the max ...This could be done like this :

``````int sum=0;
for (int x=0; x<max; x++) {

sum+=x;
sum++;
}``````

and i hope this will make you clear :

``````#include <iostream>
using namespace std;

double sum_of_numbers(int max) {
int sum=0;
for (int x=0; x<max; x++) {

sum+=x;
sum++;
}
return sum;
}

int main () {

cout<<"Enter The Max Integer :";
int max;
cin>>max;
cout<<endl;

cout<<"Sum is :"<<sum_of_numbers(max);
}``````

## foxmulder

Hi again!

Thank you so very much all of you!

I hope the confusion is because Im from Sweden and I dont know all the "names, words".

The code you posted was so clear when I saw it.

Thanks again you all!

## samsons17 -4

So,in the code below i use the name sum_of_numbers for the function...
The other thing that you have to do is declare a variable that could take all of the sum inside the loop that read all the numbers until reach the max ...This could be done like this :

``````int sum=0;
for (int x=0; x<max; x++) {

sum+=x;
sum++;
}``````

and i hope this will make you clear :

``````#include <iostream>
using namespace std;

double sum_of_numbers(int max) {
int sum=0;
for (int x=0; x<max; x++) {

sum+=x;
sum++;
}
return sum;
}

int main () {

cout<<"Enter The Max Integer :";
int max;
cin>>max;
cout<<endl;

cout<<"Sum is :"<<sum_of_numbers(max);
}``````