Thread: how to print out these structures??? plz help

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    5

    Question how to print out these structures??? plz help

    friends help me how to print out these structures using loops.. i'm unable to find any solution.. it is not necessary to give me the entire code, if u can only explain me the solution , it'll also b ok..

    how to print out these structures??? plz help-tree-png

    plzz help me printing out these outputs using loops.. thanx everyone..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To understand the problem better, get a piece of graph paper, and start with your last problem. (You can make graph paper columns just using regular paper, just add the column lines in with a pencil and straight edge.)

    Now imagine your pencil is the print head (or cursor position on the screen), and you can only move the pencil point to the right, and then down once, at the end of the row.

    Never to the left, or upward.

    Now print out the last diagram, SLOWLY. Note how you are doing it, at a very simple level. The logic you are using to do this, is the very same logic that your computer can use, once you make it simple enough.

    Do that exercise with the pencil nice and slowly a few times, and your understanding of the problem, will deepen. The "lightbulb" will turn on!

    In general, using two nested loops is a good idea. Use the outer for loop to control the row counter, and the inner for loop to handle everything else.

    Code:
    for(each row in the diagram) {
        for(each column in the row you're printing atm) {
              //put your logic in here
        }
    }
    Naturally, you're going to increment a row and col variable in each for loop, and use if statements probably, as part of the logic code.

    Programming involves a lot of critical thinking and problem solving, so this is a great exercise to start with.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    adak.. i'm goin to try wit ur advice.. i'll ask u again if i face some problem.. thanxxx for xplaining ...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Rbsupercool View Post
    adak.. i'm goin to try wit ur advice.. i'll ask u again if i face some problem.. thanxxx for xplaining ...
    What, you are on the phone and sending sms?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    @adak your advice really worked... i experimented a little bit with that graph nd solved the last problem.. it feels so good..thank u very much for ur advice.. this is what i've done..
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int i,a ;
    for(i=1;i<=5;i++)
    {
    int j=1;
    
    
    while(j<= 5-i)
    {
        printf(" ");
        j++;
    }
    
    
    
    
    for(a=1;a<=i;a++)
    {
        printf("%d ",i);
    }
    printf("\n");
    
    
    }
    return 0;
    }
    last problem solved.. it was easiest though.. the first three problems are yet to solve.. any advice from anyone will help me..

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Once you get into these things, the solution goes a lot faster.

    #1 - look at the vertical columns the digits all line up in.

    Code:
    ░░1
    2░░░3
    etc.
    Three spaces maybe, between the digits in the same row? Eazy-peazey!

    Repeat the graph paper exercise if you need it, with every diagram. It gets much faster with practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM