Thread: Understanding the output of a C program --- Need help

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    8

    Talking Understanding the output of a C program --- Need help

    Hi I'm a newbie at programming working with stephen kochan's programming in C. I have an encountered a code in the book where I am unable to understand the output and utterly frustrated after spending hours. Below is the code

    Code:
    #include<stdio.h>
    
    
    int main(void)
    {
       int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
       int i, j;
      
       for(j = 0; j < 10; ++j)
          for(i = 0; i < j; ++i)// does i remain 0 throughout?
            numbers[j] += numbers[i];
          
      for(j = 0; j < 10; ++j)
        printf("%i ", numbers[j]);
        
      printf("\n");
      
      return 0;
    }
    in my mind, the 0th index of both j and i is 1. Therefore, the first value of numbers[j] += number[i] should be 2.
    However, the output of the program is:
    1, 1, 2, 4, 8, 16, 32, 64, 128, 256
    As i understand, the elements of the array j are getting added up with previous elements and as a result 1 + 1 = 2 and 2 + 1+ 1 =4, etc.
    So what happens to i. Please explain why does i not get executed throughout.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The best thing you can do to understand what is going on is to create (by hand) a table, that contains each value for each iteration of each loop and the result of any operations.

    Notice how the outer loop cycles "j" from 0 to 9, and the operations all occur to "numbers[j]". This would indicate that the operations occur sequentially through the array. The inner loop causes a different amount of operations on each element (including no operations at one point).

    This example of such a table will get you started:

    Code:
    /*
    +---+---+------------+------------+--------------------------------------+---------------------+
    | j | i | numbers[j] | numbers[i] |  numbers[j] = numbers[j]+numbers[i]  | Array contains...   |
    +---+---+------------+------------+--------------------------------------+---------------------+
    | 0 | 0 |     1      |     1      |  nothing happens (i < j is false)    | 1 0 0 0 0 0 0 0 0 0 |
    +---+---+------------+------------+--------------------------------------+---------------------+
    | 1 | 0 |     0      |     1      |  numbers[1] = 0 + 1 = 1              | 1 1 0 0 0 0 0 0 0 0 |
    | 1 | 1 |     1      |     1      |  nothing happens (i < j is false)    | 1 1 0 0 0 0 0 0 0 0 |
    +---+---+------------+------------+--------------------------------------+---------------------+
    | 2 | 0 |     0      |     1      |  numbers[2] = 0 + 1 = 1              | 1 1 1 0 0 0 0 0 0 0 |
    | 2 | 1 |     1      |     1      |  numbers[2] = 1 + 1 = 2              | 1 1 2 0 0 0 0 0 0 0 |
    | 2 | 2 |     2      |     2      |  nothing happens (i < j is false)    | 1 1 2 0 0 0 0 0 0 0 |
    +---+---+------------+------------+--------------------------------------+---------------------+
    */
    Now execute the code "by hand" - go through each line of code, update 'i' and 'j' fields on the table when necessary, perform the addition when necessary, and update the table accordingly.

    This will help you "see" the logic that is at work.
    Last edited by Matticus; 03-20-2015 at 01:17 PM.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    8
    Thanks a ton...that sorted everything...This technique will help me even further

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble understanding file input and output.
    By Iceboxes in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2014, 11:36 AM
  2. Replies: 2
    Last Post: 07-30-2013, 02:51 PM
  3. Need help understanding formatting output with printf
    By Kahizinn in forum C Programming
    Replies: 3
    Last Post: 02-04-2011, 07:17 PM
  4. Understanding Output Generated
    By Air in forum C Programming
    Replies: 10
    Last Post: 01-15-2009, 01:52 PM
  5. understanding output
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-11-2001, 06:19 AM

Tags for this Thread