Expand|Select|Wrap|Line Numbers
- Create a program to print nested squares
- Input: Accept the width of the largest square
- Output: A series of nested squares with 1 space between them
- Sample:
- > eca4.exe 10
- **********
- * *
- * ****** *
- * * * *
- * * ** * *
- * * ** * *
- * * * *
- * ****** *
- * *
- **********
* Top Lines of inside squares would be 1-8 , 1-3 - 6-8 (Don't count first and last lines of largest square)
* Bottom Lines of inside squares would be 2-7, 2-4 5-7 (Don't count first and last lines of largest square)
Here is what I got so far. Please help me with some ideas to develop
Expand|Select|Wrap|Line Numbers
- #include <iostream> //Required if your program does any I/O
- #include <string> //Required if your program uses C++ strings
- using namespace std; //Required for ANSI C++ 1998 standard.
- void drawRectangle(int w, char symbol)
- {
- for ( int i = 0; i < w; i++) //How many Lines we need to draw?
- {
- if(i == 0 || i == (w - 1)) //Is it the first line or the last line?
- {
- for(int cFnL = 0; cFnL < w; cFnL++) //Print out full char for the first and the last line
- {
- cout << symbol;
- }
- cout << endl;
- }
- else //Draw middle lines
- {
- if(i % 2 == 0 && i < (w/2))
- {
- for(int cFnL = 0; cFnL < w; cFnL++) // Top half of top side of inside squares
- {
- cout << "-";
- }
- cout << endl;
- }
- else if(i % 2 == 1 && i >= (w/2))
- {
- for(int cFnL = 0; cFnL < w; cFnL++) //Bottom haft of bottom side of inside squares
- {
- cout << "-";
- }
- cout << endl;
- }
- else
- {
- for(int cFnL = 0; cFnL < w; cFnL++) //Space lines between squares
- {
- cout << " ";
- }
- cout << endl;
- }
- }
- }
- }
- int main (int argc, char **argv) //Arguments to main are command line arguments
- {
- char reply; //A character variable to hold user input
- int iWidth;
- cout << "Enter integer the Width of the largest square: ";
- cin >> iWidth;
- cout << endl;
- drawRectangle(iWidth, '*'); //Call function to draw out
- cout << endl;
- // This section stops the program 'flashing' off the screen.
- cout << "Press q (or any other key) followed by 'Enter' to quit: ";
- cin >> reply;
- return 0;
- }