I have been working on this assignment for a while and cant get the end result to work. Can anyone help?

Assignment

Write a function called sumTo that takes as arguments two integers. Your function should use a loop to sum all of the numbers between the two (This includes the starting and ending numbers) and return the sum.

Your function should allow for either argument to be the larger. for instance, passing the numbers 1, 5 or 5, 1 should return the same sum

From main ask the user for two numbers, call the function, and print the sum when the function returns.
Here is an example:

Enter two number
1 5

The sum of all the numbers is 15

What I have so far.

``````#include <iostream>
using namespace std;
int sumTo(int, int);

int main() {
int numb1,numb2;
cin>>numb1;

cin>>numb2;

sumTo(numb1,numb2);
return 0;
}

int sumTo(int num1, int num2) {
int counter = 0;
int result = 0;

if(num1 < num2) {
while(counter < num2) {
result += num1;
cout<<result<<endl;
counter++;
}
}
return result;
}``````

Thanks :)

6
Contributors
12
Replies
13
Views
8 Years
Discussion Span
Last Post by Salem

The assignment wants you to start at x and add each number to a sum while counting up to y, like so:

``````sum = 0

while x < y do
sum = sum + x
x = x + 1
loop``````

Compare that to your current code and you'll see the problems. As for allowing any combination of x and y, you can do something like this:

``````if x < y then
start = x
end = y
else
start = y
end = x
endif

sum = 0

while start < end do
sum = sum + start
start = start + 1
loop``````

And before you complain, yes, I'm well aware that the code I've posted is not C++. It's pseudocode, and can easily be translated to C++.

The assignment wants you to start at x and add each number to a sum while counting up to y, like so:

``````if x < y then
start = x
end = y
else
start = y
end = x
endif

sum = 0

while start < end do
sum = sum + start
start = start + 1
loop``````

I am having trouble still understanding your code :( sorry I am just a beginner trying my best to learn. Could you explain it a little better please

>I am having trouble still understanding your code
That's about as basic as it gets. What don't you understand?

Alright I made it work now, but I need help making it be able have a bigger number first. Heres what I have so far. Can anyone help?

``````#include <iostream>
using namespace std;
int sumTo(int, int);
int result=0;
int main() {
int numb1,numb2;
cout << "Please enter two numbers:" << endl;
cin >> numb1 >> numb2;
if (numb1 > numb2)
{

}
else
{

}
sumTo(numb1,numb2);
cout << "The sum of all the numbers is " << result << endl;
return 0;
}

int sumTo(int num1, int num2) {
for(int i=num1;i<=num2;i++)
{
result += i;
}
return result;
}``````
``````#include <iostream>
using namespace std;
int min(int x, int y) {
return x < y ? x : y;
}
int max(int x, int y) {
return x > y ? x : y;
}
int sumTo(int, int);

int main() {
int numb1, numb2;
cout << "Please enter two numbers:" << endl;
cin >> numb1 >> numb2;

cout << "The sum of all the numbers is " << sumTo(numb1, numb2) << endl;
return 0;
}

int sumTo(int num1, int num2) {
int result = 0;
for (int i = min(num1, num2); i <= max(num1, num2); i++) {
result += i;
}
return result;
}``````
``````#include <iostream>
using namespace std;
int min(int x, int y) {
return x < y ? x : y;
}
int max(int x, int y) {
return x > y ? x : y;
}
int sumTo(int, int);

int main() {
int numb1, numb2;
cout << "Please enter two numbers:" << endl;
cin >> numb1 >> numb2;

cout << "The sum of all the numbers is " << sumTo(numb1, numb2) << endl;
return 0;
}

int sumTo(int num1, int num2) {
int result = 0;
for (int i = min(num1, num2); i <= max(num1, num2); i++) {
result += i;
}
return result;
}``````

Well instead of using functions min() & max(), I prefer using the swap of numbers, which was shown in this thread below. Calling function's is always expensive than creating a temporary variable.

you can swap two ints with x^=y^=x^=y;
edit: btw you can use

``#define max(x,y) (((x) < (y)) ? (y):(x))``

:D

>Calling function's is always expensive than creating a temporary variable.
Note that when you say "always" or "never", all I have to do is find one exception to prove you wrong.

>you can swap two ints with x^=y^=x^=y;
No, you can't. That statement invokes undefined behavior by assigning to an object multiple times between sequence points. This is the correct way of doing it:

``````x ^= y;
y ^= x;
x ^= y;``````

Of course, while the XOR swap was a brilliant technique in olden times where memory was akin to gold bars, these days it's a dubious optimization, especially when the people who use it tend not to fully understand the nuances.

>edit: btw you can use
If you're trying to suggest an optimization then assigning the result to a variable is much better. Even if you inline max (which happens to be a standard name, by the way), you're still introducing a choice in the loop condition, which results in an extra test and jump in the object code for every iteration.

What does t "^=" Operator Mean?

It's the assignment XOR operator. `x ^= y` is equivalent to `x = x ^ y` , and ^ is the bitwise XOR operator.

> you can swap two ints with x^=y^=x^=y;
Yeah, and when it isn't an int, then what?
http://c-faq.com/expr/xorswapexpr.html

The same old hack gets posted time and time again.

If you actually use a temp variable, a modern compiler will typically recognise that it is a swap, and do something sensible. Which may in fact mean that it eliminates the temporary, and in some cases eliminates the swap (by keeping track of where the data should be, and currently is).
Plus, even the most pedestrian of programmers will instantly recognise it for what it is.
Plus, it's going to work exactly as expected for all data types.

The xor trick is basically smoke and mirrors, which few can even understand, and fewer still understand the traps which await.

You may as well have written
encrypt(x,y)
magic(x,y)
decrypt(x,y)
The optimiser has NO chance of figuring out just how smart you've been to achieve a simple effect, and will just fall back to doing exactly what you asked, on the assumption that you know what you're doing.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.