Is this the right way to see this program?

This is a discussion on Is this the right way to see this program? within the C Programming forums, part of the General Programming Boards category; This is a set of nested for statements. The program asks you to pick an integer "n" which based on ...

  1. #1
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Smile Is this the right way to see this program?

    This is a set of nested for statements.
    The program asks you to pick an integer "n" which based on the initializations of "i=1" & "j=1" must be positive for the loops to even execute.
    I would say the outer for loop where "i = 1" executes n-1 times and I think the inner loop executes n-1 times as well.
    The output "sum" counts the number of loops that are executed for both outer and inner for loops and then adds 1.
    Is this correct?
    Code:
    {
        int i,j,n, sum=0;
        
        printf("Enter n \n");
        scanf("%d",&n);
        
        for (i=1; i<=n; i++)
        {
            for(j=1; j<=i; j++)
              sum = sum +1;
        }
        printf("Sum = %d\n",sum);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you get the results you expect after trying it with multiple values? Try small ones, and calculate it by hand. Compare with the results your program gives you.

    The sooner you learn to debug your own code, the better off you'll be.


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

  3. #3
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    The program was written by someone else and I am to figure out the value of "sum" at the end of the program and justify my answer.
    Thanks quzah. I will get down to the brass tacks after I go for a walk.

  4. #4
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    The program asks you to pick an integer "n" which based on the initializations of "i=1" & "j=1" must be positive for the 2 loops to even execute.

    Based on the structure i loops (n-1 times) and the inner for loops once for i = 2 but as i increases by one the inner for loops in an arithmetic progression of +1 so that if n = 4 then the outer for loops 3 times but the inner for loops one time for i = 2 then i = 3 the inner for loops 2 times and finally as the outer for loops the third time to make i = 4 the inner for loops 3 times.

    This makes for 3 outer loop executions and 1+2+3 = 6 inner loop executions which sum to 9 which then adds 1 to equal 10.

    Is there a better way to say this?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First we clean up the loop...
    Code:
    for ( i = 0; i < n; i++ )
    {
        for( j = 0; j < i; j++ )
              sum++;
    }
    Now we do a test run "on paper"
    Code:
    for ( i = 0; i < n; i++ )
        for( j = 0; j < i; j++ )
    
    n = 2
    i = 0
        j = 0, sum = 1
    i = 1
        j = 0, sum = 2
        j = 1, sum = 3
    Now let's see if we can't do something about that pesky inner loop...
    Code:
    i = 0
       sum += i + 1
       (ie: sum = 1)
    i = 1
       sum += i + 1
       (ie: sum = 3)
    So you see you can skip the inner loop entirely. Now, I've changed it a bit, because I like counting from 0 instead of 1, because it looks cleaner to me. You could do the same, if you start at 1, and do a <= like you did, removing the "+1" segment.
    Code:
    for( i = 1; i <= n; i++ )
        sum += i;
    Should give us...
    Code:
    i = 1, sum = 1 ( 0 + 1 )
    i = 2, sum = 3 ( 1 + 2 )
    You can skip the whole loop...
    Code:
    sum = ((n + 1) * (n / 2))


    [edit]
    Hm..

    Actually you can only sum that way (n+1)*(n/2) if N is even. If it is odd, you have to add a bit.
    n is even
    ( n + 1 ) * ( n / 2 )

    n = 2
    (1 + 2 = 3) == ( (2+1)*(1) )
    n = 4
    (1 + 2 + 3 + 4 = 10) == ( (4+1)*(2) )

    n is odd
    ( ( n + 1 ) * ( n / 2 ) ) + ( (n + 1 ) / 2 )

    n = 3
    ( 1 + 2 + 3 = 6) == ( (3+1)*(n/2)+((n+1)/2) ) == ( (4)*(1)+(2) )
    n = 5
    ( 1 + 2 + 3 + 4 + 5 == 15 ) == ( (5+1)*(5/2)+(6/2) ) == ( 6*2+3 )

    That looks about right.

    Oddly enough...the number of objects, summed 1 to N, equal the number of loop iterations.
    [/edit]

    Quzah.
    Last edited by quzah; 09-29-2006 at 11:50 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    The program asks you to pick an integer "n" which based on the initializations of "i=1" & "j=1" must be positive for the 2 loops to even execute.

    Based on the structure i loops (n-1 times) and the inner for loop executes once for i = 2 but as i increments by one the inner for loop executes an additional time so that you can actually conceive of it as an arithmetic series with a difference of +1 so that if n = 4 then the outer for loops 3 times but the inner for loops one time for i = 2 then i = 3 the inner for loops 2 times and finally as the outer for loops the third time to make i = 4 the inner for loops 3 times.

    This makes for 3 outer loop executions and 1+2+3 = 6 inner loop executions which sum to 9 which then adds 1 to equal 10.

    The formula for the number of outer loops is n -1.
    The formula for the inner loop can be simplified into the algebraic term:
    (n-1)*[2 + {n-2}])/2 = Sn
    Thus we can figure out the value of sum if we apply this equation first:
    Outer loop executions = n-1 (with n >=1)
    Inner loop executions Sn
    Add outer and inner loops (n-1) + Sn +1 = sum

    Thanks for the advice quzah.
    Last edited by ralphwiggam; 09-29-2006 at 11:14 AM.

  7. #7
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by ralphwiggam
    Based on the structure i loops (n-1 times)
    No, it doesn't!

    Quote Originally Posted by ralphwiggam
    and the inner for loop executes once for i = 2
    No, it doesn't!

    Here is some info about for loops where you'll learn things like "initialization allows you to give a value to a variable, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself".

    It appears that you do not know this and have ignored Quzah's attempts to get you to visualize the process.

    In your code i is initialized to 1 and the loop executes once for every value up to and including n. So how is that n-1 times. Sames goes for j.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  8. #8
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32

    Smile

    Quote Originally Posted by risby
    It appears that you do not know this and have ignored Quzah's attempts to get you to visualize the process.
    quzah & risby Okay you are right and I mean no disrespect. I was trying to understand the nature of the relationship between the outer & inner loops and I have read over the lesson and read 2 different books on C on for loops and gone over my own program listed above.

    I was so excited to see the arithmetic series with my pencil work that all the rest of my considerations went out the door.

    Thank you for taking the time to educate me. I promise you that you are not wasting your time and that my education will benefit other people.
    Last edited by ralphwiggam; 09-30-2006 at 06:16 PM.

  9. #9
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by ralphwiggam
    Thank you for taking the time to educate me. I promise you that you are not wasting your time and that my education will benefit other people.
    OK Ralphy, that's good to hear, keep up the good work.

    Now, he he, there's just one little thing. How many times will each loop execute?
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  10. #10
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    n must be a positive integer
    Outer loop executes n times
    Inner loop executes the current i # of times before it exits and the outer loop increments by one and retests condition and if true (i<=n) then goes back into body and loops the incrementalized i an i # of times until j=i and exits and goes back to outer loop to increment i by 1 and retests condition and if true goes back into body and so on until outer loop condition is false
    I sound like a clutz. . .is there a better way of saying this?

  11. #11
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by ralphwiggam
    n must be a positive integer
    Outer loop executes n times
    Inner loop executes the current i # of times before it exits and the outer loop increments by one and retests condition and if true (i<=n) then goes back into body and loops the incrementalized i an i # of times until j=i and exits and goes back to outer loop to increment i by 1 and retests condition and if true goes back into body and so on until outer loop condition is false
    I sound like a clutz. . .is there a better way of saying this?
    Yeah, it is mighty difficult to translate to human speech. Try this:

    given n is a non-zero positive integer the outer loop executes n times
    each time limiting the inner loop index to vary from 1 to the value of the index of the outer loop, inclusively
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  12. #12
    Registered User ralphwiggam's Avatar
    Join Date
    Sep 2006
    Posts
    32
    Thank you
    I am saving this thread

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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