Thread: Tracing a asterisks triangle program

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Tracing a asterisks triangle program

    Code:
    # include <stdio.h>
    
    void printTriangle(int num_rows)
            {
                    int currentRow;
                    int currentStar;
                    for(currentRow = 0; currentRow < num_rows; currentRow++)
                    {
                            for(currentStar = 0; currentStar <= currentRow + (currentRow / 3 ); currentStar++)
                            {
                                    printf("*");
                            }
    
                            printf("\n");
                    }
            }
    
    int main()
    {
        printTriangle(5);
    
        return 0;
    }
    On Line 9, Why must there be an integer division of current_row and 3? Can anyone elaborate on this concept please?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Learning to follow a program step by step is a vital skill when you're trying to master the concepts.

    Let's look at the inner loop.

    Code:
    /* loop 1 */
    currentRow = 0
    
    currentStar <= currentRow + (currentRow / 3 )
    
    This is how many stars you want to print on the first loop.
    currentRow + (currentRow / 3 ) = 0 + 0/3 = 0 + 0 = 0
    Because of the "<=", you will print one star.
    
    /* loop 2 */
    currentRow = 1
    
    currentStar <= currentRow + (currentRow / 3 )
    
    currentRow + (currentRow / 3 ) = 1 + 1/3 = 1 + 0 = 1
    Two stars will be printed.
    
    /* loop 3 */
    currentRow = 2
    
    currentStar <= currentRow + (currentRow / 3 )
    
    currentRow + (currentRow / 3 ) = 2 + 2/3 = 2 + 0 = 2
    Three stars will be printed.
    
    /* loop 4 */
    currentRow = 3
    
    currentStar <= currentRow + (currentRow / 3 )
    
    currentRow + (currentRow / 3 ) = 3 + 3/3 = 3 + 1 = 4
    Five stars will be printed.
    
    etc...
    Notice that this is not a perfectly stepped printout, as dictated by the logic.

    Also remember that integer division truncates the result, which is why "currentRow/3" produces zero when "currentRow" is 0, 1, and 2.
    Last edited by Matticus; 12-06-2012 at 09:39 PM.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Matticus strikes again! Thank you!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome! And I just correct an error in my post - for the last case, I accidentally had "Four stars will be printed."

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > On Line 9, Why must there be an integer division of current_row and 3? Can anyone elaborate on this concept please?
    You could
    change it to /2, run the code and observe the changes to the output.
    change it to /4, run the code and observe the changes to the output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could also note that the number of stars to be printed on each row, equals the current row number + 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Right Triangle Program
    By Chinnie15 in forum C Programming
    Replies: 5
    Last Post: 10-12-2011, 09:52 PM
  2. Tracing through a recursive program in C
    By Hybodus in forum C Programming
    Replies: 3
    Last Post: 12-18-2010, 05:48 PM
  3. Pascal's Triangle Program Only Accurate to 12 Rows
    By quintaessentia in forum C Programming
    Replies: 6
    Last Post: 03-29-2010, 04:12 PM
  4. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  5. Write a program that prints a pattern of asterisks?
    By Basia in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 05:56 PM