How do I write a function to take an integer and return the sum of integers from 1 to that number?
Thanks!!
How do I write a function to take an integer and return the sum of integers from 1 to that number?
Thanks!!
you should try and put something together first. We can then help you with anything your struggling with.
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
ok I came up with this,but it want you to put in 10 numbers. I want to put in a number between 1 and 10, so if I put in the number 5 it should return 15 since 1 + 2+3+4+5=15. I know this code isn't right.
Code:int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; { for (int num = 1; num <= 10; num ++) sum += num; } cout << "The sum is " << sum ; return 0; }
Very close. Why don't you name your loop index something else? You named your global variable num that gets set to the users input. A common choice for loop index is i. Your stopping condition is "num <= 10". You don't always want it to go to 10 though. You want it to stop when you get to the number the user entered.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
As MrW said, its all about getting your algorithm correct first then building on that.
Yous syntax is pretty much correct (except you don't need the braces around your loop).
Just put an include file at the top so cin and cout can be recognised. You'll also need to declare standard namespace to make things easier at the moment.
Translated :
at the top of your code.Code:#include <iostream> using namespace std;
Now just work out your algorithm. You don't need to be a programmer to work it out, its simple maths.
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
Ok I have this, I don't know how to make it stop at what the user enters.
Code:#include <iostream> using namespace std; int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; { for (int i = 1; num <= 10; i ++) sum += i; } cout << "The sum is " << sum ; return 0; }
Think aboutr it. Your getting the user to input a number, then your saying
start the loop at 1
loop until the entered number reaches 10
sum = sum + 1
That will give you
0+1
1+1
2+1
3+1
4+1
you need to be using the sum to add with and looping until the sum is reached.
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
So should it be something like this? I understand what you are saying, just having a hard time coding it.
Code:#include <iostream> using namespace std; int num; int sum = 0; int main() { cout << "Enter a number between 1 and 10: "<< endl; cin >> num; { for (int i = 1; sum = num; ) sum += num; } cout << "The sum is " << sum ; return 0;
lol, I give in. Here is your code. Please read up on the basics before your next attempt.
Good luck
Code:#include <iostream> using namespace std; int main() { int num; int sum = 0; cout << "Enter a number between 1 and 10: "; cin >> num; for (int i=1; i<=num; i++) { sum += i; } cout << "The sum is " << sum << endl; return 0; }
Open source isn't a matter of life or death......
.......its much more important than that!!
SuSE Linux - GCC 3.4.2
XP Pro - Visual Studio 2005 TS, MinGW 3.4.2
Oh my gosh!! Ok, I am tired! I totally missed that.
Thanks for the help!! I am reading and asking and trying very hard. I am learning from all of you that are helping me.
Thanks again!
Sometimes it is hard to get right into the text editor and put your code right in. Try writing it out on paper without all the syntax. Sometimes it makes it easier to think. For example...
or something of that nature.Code:while (i < 10) { print "blah" i++; }
You could also 'cheat' by applying the formula for this case.
i.e.
sum = (num*(num+1))/2
Of course, you arent actually checking if the number entered is between 1 and 10 (inclusive?)...Code:#include <iostream> using namespace std; int main() { int num; cout<<"Enter a number between 1 and 10: "; cin>>num; int sum = (num * (num + 1)) / 2; cout<<"The sum is:"<<sum<<endl; return 0; }
but I'll leave that part to you.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)