Sum up of numbers

This is a discussion on Sum up of numbers within the C++ Programming forums, part of the General Programming Boards category; Hi I would like to know how I can write a program that gives me the sum of 1 to ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Sum up of numbers

    Hi I would like to know how I can write a program that gives me the sum of 1 to n numbers. Example:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int i =0;
    
    //Here is a loop that, for example, gives me numbers from 0 to 4
    //My problem is, I want to add these numbers
    //I get 01234, now all I want is add them together
    //I was thinking of maybe using an array to save these numbers, then add them
    //one by one but I am a beginner and have no idea how to do it
       
        for (i; i<5; i++)
        {
            cout<<i
            }
             return 0;
    }
    Thanks in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Rather than saving them, why not just add them to some other variable, named say sumOfNumbers ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    I didn't understand properly what you meant by add them to another variable.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    What Salem means is that instead of asking 10 numbers and remembering them, then summing them, why not ask for numbers continuously and add them as you go?
    For example, instead of

    User inputs 1
    Remember 1
    User inputs 2
    Remember 2
    ...
    Sum all inputted numbers

    you do

    User inputs 1
    Total sum = 1
    User inputs 2
    Total sum = previous sum (1) + 2 = 3
    User inputs 3
    Total sum = previous sum (3) + 3 = 6
    ...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    I wasn't asking for user inputs. I just want to know how I can sum up the individual numbers in a for loop. As in the simple example, I have numbers from 0 to 4 produced by the for-loop. I want to be able to sum these numbers up, that is, 0+1+2+3+4 = 10. Problem is, I can't get to individually access the numbers so that I can sum them up.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    You HAVE the numbers already, they're in i, your for loop variable.

    You managed to print them, why can't you add them?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Yeah that is the problem I guess. I get to print them. They are there but how can I get to sum them up? That is where I am stuck. I guess I need to sum them up recursively or something like that but I am stuck here.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Initialise the total to zero, then add each of those numbers in turn to the total.
    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

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Yeah got it finally, thanks all. I must be so thick. This is how I did it:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int i =0, sum =0;
            for (i; i<5; i++)
        {
            sum +=i;
            }
            cout<<sum;                
             return 0;
    }
    Last edited by Dontgiveup; 03-11-2011 at 09:44 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Good, though this is how I would have written it:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int sum = 0;
        for (int i = 0; i < 5; ++i)
        {
            sum += i;
        }
        cout << sum << endl;
        return 0;
    }
    You could also have implemented a formula to compute the sum for 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

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks laserlight, why would you write it that way? I mean I am new so I am just curious and want to know why would you do it that way. Any explanation, tips etc. are appreciated.

    Ooh and I can see you have ++i instead of the i++ that I wrote, what is the difference?
    Last edited by Dontgiveup; 03-11-2011 at 09:56 AM.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Say you wrote h = i++; somewhere. The value of h is actually the previous value of i before you added. In h = ++i; h is actually the same as i. There are times where you will use the value of i in this step, but a for loop is not really one of those times. You could safely use either.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by Dontgiveup
    Thanks laserlight, why would you write it that way?
    • No more unused variable named x.
    • i is only used within the loop, so it is moved to be within the loop.
    • It does not matter here, but later you will find that conventionally, ++i is no slower than, but could be faster than i++.
    • Consistent indentation and more common style (e.g., spacing between operators).
    • Nice to write a newline at the very end, so I chose to do that with std::endl.
    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

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by laserlight View Post
    • No more unused variable named x.
    • i is only used within the loop, so it is moved to be within the loop.
    • It does not matter here, but later you will find that conventionally, ++i is no slower than, but could be faster than i++.
    • Consistent indentation and more common style (e.g., spacing between operators).
    • Nice to write a newline at the very end, so I chose to do that with std::endl.
    Thanks so much. Will keep that in mind next time.

    Quote Originally Posted by whiteflags View Post
    Say you wrote h = i++; somewhere. The value of h is actually the previous value of i before you added. In h = ++i; h is actually the same as i. There are times where you will use the value of i in this step, but a for loop is not really one of those times. You could safely use either.
    Thanks whiteflags!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-30-2010, 10:08 PM
  2. Replies: 1
    Last Post: 05-28-2009, 02:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 01:17 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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