Thread: Box dimension program

  1. #1
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18

    Question Box dimension program

    I'm creating a simple program that asks the user to input the size of a box and then displays the box using asterisks. I'm have a problem with the rows printing. Here is the code that I have so far...
    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) 
    {
        int column, size, row;
        column = 0;
        row = 0;
        printf("Enter size of box:");
        scanf("%d", &size);
    
        while (row != size)
        {
            row = size - 1;
            row++;
            printf("*", row);
            
            while (column != size) 
            {
                column++;
                printf("*", column);
                printf("\n");
                
            }
            
        }
        
        return 0;
    }

    result:

    Enter size of box:2
    **
    *

    How do I fix this?

  2. #2
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18

    Question

    I've updated the code but now it only works when '2' is input by user. Producing a 2 x 2 square... here is the updated code
    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) 
    {
        int size, row, column;
        row = 0;
        column = 0;
        printf("Enter size of box:");
        scanf("%d", &size);
      
        while (row != size)
        {
            row++;
            printf("*", row);
            
        }
        printf("\n");
        while (column != size) 
        {
            ++column;
            printf("*", column);
            
        }
        
        return 0;
    }
    any suggestions?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Do this size times:
    • Output size '*' characters
    • Output a newline



    This typically done with a set of nested for loops:
    Code:
    for( Do size times )
        for( Do size times )
            Output a '*'
        Output a newline
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  2. array dimension
    By julianluthor in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2003, 05:01 PM
  3. Two dimension Array
    By jimiexe in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 03:22 PM
  4. Is this correct about the 4th dimension?
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-20-2002, 02:47 PM
  5. More on Two-Dimension Arrays..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2002, 01:17 PM

Tags for this Thread