Thread: simple summation question

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    14

    simple summation question

    I'm completely new to C programming, and programming in general, and I just have a quick question.

    If I am trying to find the sum of the integers from n to 2*n (ex : if n = = 5, then sum = 5 + 6 + 7 + 8 + 10) how would I write that?

    Thanks for the help guys!

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    for loop. start at n and add until greater than 2*n.

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    You could also take advantage of the equivalence:

    sum_up_to(n) = (n * (n + 1)) / 2

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by gardhr View Post
    You could also take advantage of the equivalence:

    sum_up_to(n) = (n * (n + 1)) / 2
    Which while nice, doesn't help him learn loops.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    I have the following code and I'm not getting the result I want and I was wondering if you guys could lead me in the right direction.

    Code:
    int n;
    printf("Enter Non-Zero Integer\n");
    scanf("%d",n);
    for(n > 0; n <= n*2; n++){
        printf("%d\n", n);
        break;
        }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    scanf("%d",n);
    Turn on your compiler warnings. You need a & there.
    Code:
    for(n > 0; n <= n*2, n++){
        printf("%d\n", n);
        break;
        }
    Why use a loop if you are just going to break out of it on the first pass?
    The first part of a loop is to initialize a variable if you need to, not to test if something is true.


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

  7. #7
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by quzah View Post
    Which while nice, doesn't help him learn loops.


    Quzah.
    Actually, I was just pointing it out in the event that he ever need to calculate it with large numbers (in which case a loop might be prohibitively expensive).

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    First off, thanks a lot for your help I appreciate it.

    I've fixed the things you said and have this as my code now :

    Code:
    int n;
    printf("Enter A Non-Zero Integer\n");
    scanf("%d", &n);
    for( ; n < (n*2) + 1 ; n++){
        printf("%d\n", n);
    }
    I now just get a loop of numbers being added without stopping and I manually have to stop the loop after I execute the program.

    I didn't put anything in the first part of the for loop since I already initialized n. The second part of the loop I have it running while n is less than n*2 + 1 (so if my n was 5 it should stop at 10). And of course the third part is it loops and keeps adding 1 until the second condition is reached.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( ; n < (n*2) + 1 ; n++){
        printf("%d\n", n);
    }
    Replace n with the value n has every time through the loop:
    Code:
    n = 3
    for( ... ; 3 < 3*2+1; 4 )
    for( ... ; 4 < 4*2+1; 5 )
    for( ... ; 5 < 5*2+1; 6 )
    See the problem yet?


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

  10. #10
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    As quzah stated above, you need to you a different variable to increment the loop.
    Last edited by rmatze; 10-12-2011 at 05:59 PM. Reason: corrected myself

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    I see..

    This is my new code :

    Code:
    int n, sum;
    printf("Enter Non-Zero Integer\n");
    scanf("%d", &n);
    sum = 0;
    for( ; n >= 2 *n; ++n){
         sum += n;
         printf("%d\n", sum);
    }
    So now the loop should stop when n is greater than or equal to 2*n (so using 5, when n is 10), and until that condition is met it keeps adding one. Now whenever I run the program I enter a value and just get no sum. What am I doing wrong now?

    Thanks again for all the help.
    Last edited by uural4792; 10-12-2011 at 06:22 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by uural4792 View Post
    I see..
    Clearly you don't.
    Quote Originally Posted by uural4792 View Post
    What am I doing wrong now?
    The same thing you were doing the last time I posted. You are changing your end-loop-here value every time you change n.
    Code:
    #include<stdio.h>
    int main( void )
    {
        int n;
        for( n = 0; n < n * 2; n++ )
        {
            printf( "for( ...; %d < %d; %d++ )\n", n, n * 2, n );
        }
        return 0;
    }
    Run that.


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

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by uural4792 View Post
    I see..
    So now the loop should stop when n is greater than or equal to 2*n (so using 5, when n is 10), and until that condition is met it keeps adding one. Now whenever I run the program I enter a value and just get no sum. What am I doing wrong now?

    Thanks again for all the help.
    Ok... so N is 5 ... the loop should stop at 10...
    but the very next thing you do is ++n ...
    so what is the value of N now?
    Yep it's 6 and the loop would stop at 12...

    Think about this logically.... you are continuously changing the exit condition of the loop... what do you think is going to happen...

    Code:
    int N = 5;
    
    if (N > 2*N)
      beep();
    Translates to...
    Code:
    int N = 5;
    
    if (5 > 10)
      beep();
    What are the odds of it ever beeping?

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    What are the odds of it ever beeping?
    That depends if I can say signed overflow is defined or not.

    1 in INT_MAX ?


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

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    14
    Alright guys so I went and got some help from a TA and found the mistakes I was making. I needed another variable besides sum and n to make this work. The final code I ended up with was

    Code:
     #include <stdio.h>
    int main(void)
        int n, sum, i;
        printf("Enter a Non-Zero Integer\n");
        scanf("%d", &n);
        sum = 0
        for(i = n ; i <= (2*n); i++){
            sum += i; 
            printf("sum = %d"\n, sum);
        }
    }
    Thanks for those that helped! I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix summation
    By chaklader in forum C Programming
    Replies: 19
    Last Post: 05-30-2010, 03:51 PM
  2. summation in c
    By bertazoid in forum C Programming
    Replies: 24
    Last Post: 10-20-2008, 02:52 PM
  3. partially summation
    By joerg in forum C Programming
    Replies: 0
    Last Post: 11-23-2007, 10:47 AM
  4. Help With Simple Summation...
    By TOPFLOR in forum C Programming
    Replies: 19
    Last Post: 08-09-2006, 10:50 AM
  5. Summation of an array using pointers
    By therminator53 in forum C Programming
    Replies: 3
    Last Post: 11-30-2003, 01:23 AM