Thread: Sum help

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Sum help

    Write a program that for the integers from 1 to 100, inclusive:

    1) determines the sum
    2) counts how many of the integers from the end of the summation it takes to reach at least half of the total sum. That is, when calculating 100 + 99 + 98 + ... + n = total_sum/2, how many integers does it take to reach n?



    I have this half way done.

    Output I am getting:

    The sum from 1 to 100 is 5050
    Half the sum is 0

    Code:
    #include <stdio.h>
    
    int main()
    {
            int i;
            int sum;
    
            for( i = 1; i <= 100; i++)
            {
                    sum += i;
            }
            printf("The sum from 1 to 100 is %d\n", sum);
    
            for (i = 1; i <= 100; i++)
            {
                    sum /= i;
            }
            printf("Half the sum is %d\n", sum);
    }
    How do I divide the sum by 2 to get 2525? I'm having trouble with it.

    Thanks for your help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "/ 2"?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Why are you dividing for the second part? All that's happening is you're dividing more and more (making the number smaller and smaller) and C just truncates it to 0 by the end of it all.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,334
    You need to zero sum out before you start your first loop.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    So I should change

    int sum;

    to

    int sum = 0;?

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    That would be one of your (potential) issues, yes. The other issue is previously mentioned: sum /= 2 might be okay, but then you went ahead and put it in a for loop. It will continuously divide the answer by i up the loop ends. In your case, that's 100 times. Say your sum is 500. You are dividing that number by !100, which is a number with 157 places. 500 divided by !100, as far as C is concerned, is 0.

  7. #7
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Making some progress

    My Output now:

    The sum from 1 to 100 is 5050
    Half the sum is 50

    Code:
    #include <stdio.h>
    
    int main()
    {
            int i;
            int sum = 0;
    
            for( i = 1; i <= 100; i++)
            {
                    sum += i;
            }
            printf("The sum from 1 to 100 is %d\n", sum);
    
             sum /= i;
    
            printf("Half the sum is %d\n", sum, i);
    }
    I want it to say "Half the sum is 2525"

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,334
    When you divide, i is not equal to 2, but 101.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Got it

    Output:

    The sum from 1 to 100 is 5050
    Half the sum is 2525

    Code:
    #include <stdio.h>
    
    int main()
    {
            int i;
            int sum = 0;
            int x = 2;
    
            for( i = 1; i <= 100; i++)
            {
                    sum += i;
            }
            printf("The sum from 1 to 100 is %d\n", sum);
    
             sum /= x;
    
            printf("Half the sum is %d\n", sum );
    }


    Now I am looking output to something like this

    The sum of the last 30 integers in the
    range 1 to 100 is 2565.

    Would I use a for loop?

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    I would use a for loop. Just have it start at 100 and end at 70.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    sum = (min + max) * ((max+((min+max)&1?0:1))/2);
    I'm tired, but I think that's right.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    You don't need to create a new variable to do the division.

    sum /= 2

    Also, you could just have the counter just start counting for numbers >= 70.

    if(i >= 70) sum += i

    Or just another for loop starting at 70 like suggested.

  13. #13
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Doesn't work but am I on the right track?

    Code:
    
    #include <stdio.h>
    
    int main()
    {
            int i;
            int sum = 0;
    
            for( i = 1; i <= 100; i++)
            {
                    sum += i;
            }
            printf("The sum from 1 to 100 is %d\n", sum);
    
                     sum /= 2;
    
            printf("Half the sum is %d\n", sum );
    
            for( i = 70; i <= 100; i++)
            {
                    sum += i;
            }
            printf("The sum of the last 30 integers in the
    range 1 to 100 is %d\n", sum);
    }

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't work because sum still has its old value before you start adding more to it in your second loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    How do I change the sum?

    I am trying to find the sum of the last 30 integers.

    So the sum from 70 to 100 = ?

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