Not Yet Answered # Function that return an amount from 0 to ?

Ancient Dragon 5,243 firstPerson 761 Discussion Starter foxmulder firstPerson 761 Discussion Starter foxmulder Ancient Dragon 5,243 Discussion Starter foxmulder firstPerson 761 Discussion Starter foxmulder amrith92 119 Ancient Dragon 5,243 Discussion Starter foxmulder Ancient Dragon 5,243 samsons17 -4 Discussion Starter foxmulder samsons17 -4 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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

*Edited 7 Years Ago by Ancient Dragon*: n/a

0

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

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

0

Hi!

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

Thanks in advance!

//Adam

0

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.

0

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

0

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

0

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

//adam

0

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

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

0

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!

*Edited 7 Years Ago by amrith92*: n/a

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.

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

0

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

0

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

0

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

0

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

0

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

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...