sum of integers

This is a discussion on sum of integers within the C++ Programming forums, part of the General Programming Boards category; How do I write a function to take an integer and return the sum of integers from 1 to that ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    sum of integers

    How do I write a function to take an integer and return the sum of integers from 1 to that number?

    Thanks!!

  2. #2
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    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

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    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;
     	
     	}

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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

  5. #5
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    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 :
    Code:
    #include <iostream>
    using namespace std;
    at the top of your code.

    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

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    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;
     	
     	}

  7. #7
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    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

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    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;

  9. #9
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    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

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    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!

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    4
    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...

    Code:
    while (i < 10)
       {
       print "blah"
       i++;
       }
    or something of that nature.

  12. #12
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Good example for recursion.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    You could also 'cheat' by applying the formula for this case.
    i.e.
    sum = (num*(num+1))/2

    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;
    }
    Of course, you arent actually checking if the number entered is between 1 and 10 (inclusive?)...
    but I'll leave that part to you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of the numbers between two integers
    By Sridar in forum C++ Programming
    Replies: 16
    Last Post: 02-01-2011, 09:44 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  3. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. Replies: 5
    Last Post: 03-12-2002, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21