How to print this using for loop?

This is a discussion on How to print this using for loop? within the C Programming forums, part of the General Programming Boards category; please help me..! how to print this output using for loop? output: Code: S S S S S S S ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    How to print this using for loop?

    please help me..!
    how to print this output using for loop?

    output:
    Code:
    S S S S S
    S          S
    S          S
    S          S
    S S S S S

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,339
    Announcements - C Programming
    You need to read your book / course notes, and at least make an effort at solving your homework.

    You're not going to get far with just dumping your assignment.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    Code:
    /*
    
    
    S S S S S
    S       S
    S       S
    S       S
    S S S S S
    
    
    */
    
    
    #include<stdio.h>
    
    
    main()
    
    
    {
        int i,j;
            for(i=1;i<=5;i++)
                {
                    for(j=1;j<=5;j++)
                            {
                             if((i==2&&j==2)||(i==2&&j==3)||(i==2&&j==4)||(i==3&&j==2)||(i==3&&j==3)||(i==3&&j==4)||(i==4&&j==2)||(i==4&&j==3)||(i==4&&j==4))
                                printf("  ");
                             else
                                printf("S ");
                            }
                    printf("\n");
                }
    }
    i done this, but don't understand that how to get this output using for loop!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of using large multi-logic expressions, try to break the problem down into it's few cases, according to the row and column that is being printed out at that time.

    In pseudo code:
    Code:
    for each row in the box {
       for each column in the row I'm printing {
          if the row == the first one || the row == the last one, 
             print out the S (regardless of the column)
           
          if the row is > the first one and less than the last one {
             if column == the first or column == the last one 
                print the S
             else print a space char ' '.
          }
       }
    }
    It's a good idea to use the nested for loops for all row and column printouts, or "walking" through any 2D array. First one handles the rows, and that leaves you to concentrate on the for loops for the columns.


    BTW, when you post a figure that you want help with, be SURE it's EXACT. There's a difference between the square diagram in your program, and the first diagram you posted.

    Rather inconsistent pseudo code, but I'm trying to bring in just a bit of syntax, as well.
    Last edited by Adak; 07-10-2012 at 12:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple while loop print
    By ar15match in forum C Programming
    Replies: 11
    Last Post: 07-01-2012, 11:48 PM
  2. loop help print this
    By gnomelook in forum C Programming
    Replies: 4
    Last Post: 11-17-2011, 09:48 PM
  3. Print matrice with one loop?
    By cowa in forum C Programming
    Replies: 3
    Last Post: 12-25-2009, 07:34 PM
  4. Add a Loop to print the output?
    By big-tony in forum C Programming
    Replies: 23
    Last Post: 05-22-2009, 10:38 AM
  5. Print from 1 to 10 without using a loop
    By Guti14 in forum C++ Programming
    Replies: 11
    Last Post: 12-23-2003, 09:02 PM

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