Thread: sum

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also, your average is being computed wrong. Shouldn't the average be computed more like:

    (pseudo code)
    average = sum / (second_number - first_number)
    Where second_number - first_number is the difference between the two numbers the user entered.

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by bithub View Post
    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;
    }
    I changed it to

    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=((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;
    cin>>sum;
    return 0;
    }
    but there is no difference.

  3. #18
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by bithub View Post
    Also, your average is being computed wrong. Shouldn't the average be computed more like:

    (pseudo code)
    average = sum / (second_number - first_number)
    Where second_number - first_number is the difference between the two numbers the user entered.
    Oh I see you are right on this one but I think it should be computed as sum/num1. So if the user enters 1 and 12 the average is the sum from 1 to 12 divided by 12, which is num1. Thanks for that.

  4. #19
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What if the user enters the numbers 5 and 12? Then you want to compute the average as sum / (12-5). That is why you can't just use num1.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    it should really be
    sum/(num1 - num +1)

    as you are including the boundary numbers

    EDIT: or you could do:
    Code:
    int count = 0;
    for (; num<=num1; num++)
    {
        cout << num << endl;
        sum = sum +num; // this is fine and all is OK
        count ++;
    }
    average=((sum)/(count));
    Last edited by wyliek; 04-20-2009 at 11:07 AM.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Yeah you are right, thanks. I am so thick, only thinking in one way.
    Now another question. What if there was no array? I tried to calculate using the sum using the loop alone without an array but I can't figure out how other than using the formula.

  7. #22
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    There is no array in your code. An array looks like this:
    numbers[index]

    Can you elaborate please? What do you mean by array?

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