Thread: sum

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

    sum

    Ok I know one way of using the formula n*(n+1)/2) and have used it to calculate the sum already but I just wanted to know how I can access many numbers in a loop and add them together. Here is what I did so far.

    Code:
    #include <iostream>
    using namespace std;
    int main (){
    int num, num1, sum;
    cout<<"Please enter a number\n";
    cin>>num;
    cout <<"Please enter the number to which you want to count\n";
    cin>>num1;
    for (; num<=num1; num++)
    cout<<num<< ","; //I do get the numbers but from here
    //I want to know what I can do so that I can add each 
    // of these numbers together?
    do (sum=num+num);//I want to add them like 0+1+2+...
    //tried using loops but couldn't get it
    while (num<=sum);
    cout<<sum;//but this does not work so any help is appreciated
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm a little unsure on what you are trying to do. Do you want the user to enter an initial number, then have the user enter that many numbers and add them together. So if the user enters "3", the program will prompt the user to enter 3 numbers which get added together?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    The user enters a number first. Then s/he enters a second number (that should be greater than the first one) and so the program counts from the number entered first to the second number. So the user enters for example enters 3 first, and then 10 and the program will count to 10 i.e. it will display 3,4,5,6,7,8,9,10. Now what I want is to get the sum, average and standard deviation of these numbers such that the program sums up 3+4+5+6+7+8+9+10 to get the sum and then divides by8 to get the average and calculate for the deviation and so on.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well let's start with the sum. What you basically have so far is:
    Code:
    for (; num<=num1; num++)
    {
        cout << num << endl;
    }
    Now for each of those numbers, you need to add them up. So a quick change to your code:
    Code:
    int total = 0;
    for (; num<=num1; num++)
    {
        cout << num << endl;
        total = ... // what goes here?
    }
    From this, can you figure out what to set total equal to?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by bithub View Post
    Well let's start with the sum. What you basically have so far is:
    Code:
    for (; num<=num1; num++)
    {
        cout << num << endl;
    }
    Now for each of those numbers, you need to add them up. So a quick change to your code:
    Code:
    int total = 0;
    for (; num<=num1; num++)
    {
        cout << num << endl;
        total = ... // what goes here?
    }
    From this, can you figure out what to set total equal to?
    total = total + num?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    bingo.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Thanks!

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    But is there a way in which I can access the digits individually? I mean if I want to calculate the standard deviation, then I have to access each digit alone and subtract from average etc.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The for loop is how you access them individually. You have already done this in your code up above.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    The other things I don't understand is why is it that it looks like there is an extra number when calculating average? For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dontgiveup
    For example you have numbers from 1 to 9 and you get the right total which is 45 but instead of getting an average of 5, you get 4. Why is that?
    Sounds like integer division, so if you are required to use integers only, 4 would be correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What does your code look like? I'm guessing that you are dividing 45 by 10 instead of 9.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bithub
    I'm guessing that you are dividing 45 by 10 instead of 9.
    Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Oh yes, sum from 1 to 9 rather than 0 to 9, so yeah, an off by one error is more likely (or should I say "as likely"?).
    Here is my code.
    Code:
    #include <iostream>
    using namespace std;
    int main (){
    int num, num1, average;
    
    cout<<"Please enter a number\n";
    cin>>num;
    cout <<"Please enter the number to whcih you want to count\n";
    cin>>num1;
    int sum = 0;
    for (; num<=num1; num++)
    {
        cout << num << endl;
        sum = sum +num; // this is fine and all is OK
    }
    average=(sum/(num+1));//but here is the problem. Try with 1 and 12 and you get 
    // the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
    cout<<sum<<average;
    return 0;
    }

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You probably want the average as a floating point variable.
    Code:
    #include <iostream>
    using namespace std;
    int main (){
    int num, num1;
    float average;
    
    cout<<"Please enter a number\n";
    cin>>num;
    cout <<"Please enter the number to whcih you want to count\n";
    cin>>num1;
    int sum = 0;
    for (; num<=num1; num++)
    {
        cout << num << endl;
        sum = sum +num; // this is fine and all is OK
    }
    average=(static_cast<float>(sum)/(num+1));//but here is the problem. Try with 1 and 12 and you get 
    // the sum of all numbers from 1 to 12 i.e. 78 but you get the average as 5!
    cout<<sum<<average;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. 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
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM