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?

Thanks in advance!

//Adam

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

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

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

Hi!

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

Thanks in advance!

//Adam

Hi!

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

Thanks in advance!

//Adam

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

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.

//Adam

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

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

//adam

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;
}

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;

}

Can someone please help me with this :/

/adam

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!

Hi again!

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


Can someone please help me with this :/

/adam

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);
}

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

>>I've already got two main
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.

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);
    }

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!

//Adam

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);
    }
Be a part of the DaniWeb community

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