Printing hollow squares

This is a discussion on Printing hollow squares within the C++ Programming forums, part of the General Programming Boards category; Hi there, I am a student who needs help with a part of his project. I need to print out ...

  1. #1
    Unregistered
    Guest

    Question Printing hollow squares

    Hi there,
    I am a student who needs help with a part of his project. I need to print out a square which is completely hollow on the inside. I've figured out that it has to be a nested loop but I am not sure about how to start it and whether or not I have to put a for statment in it or not. The range also 1-20. I am almost positive that the first statement is:
    for ( i=1; i <=20; i++ )
    The output should look like this:

    Enter size of square (1-20): 9

    * * * * * * * * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * * * * * * * * *

    Please help me. Thank you.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    There are better ways to approach but this will do.

    Code:
    #include <iostream>
    using namespace std;
    
    void main()
    {
        int size;  // holds the size of the square
        int i, j;    // counters
    
        // get the size
        cout << "Enter size of square (1-20): ";
        cin >> size;
    
        // do top row
        for( i = 0; i < size; ++i)
        {
           cout << "*";
        }
        cout << endl;
    
        // do middle rows
        // subtract 2 to adjust for first and last row
        for( i = 0; i < (size - 2); ++i)
        {
           cout << "*";
           // subtract 2 to adjust for first and last columns
           for( j = 0; j < (size - 2); ++j)
           {
              cout << " ";
           }
           cout << "*" << endl;
        }
    
        // do bottom row
        for( i = 0; i < size; ++i)
        {
           cout << "*";
        }
        cout << endl;
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Generating an image of squares?
    By vopo in forum C# Programming
    Replies: 3
    Last Post: 10-21-2008, 10:15 AM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM

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