Thread: drawing a square with loops

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    drawing a square with loops

    Hi all,

    Pretty simple task to do, but for some reason when run the square is a little off. The code I have done so far is:

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using std::cin;		using std::endl;
    using std::cout;	using std::string;
    
    int main()
    
    {
    
    	
    	const int cols = 10;
    	const int rows = 10;
    
    	for (int r = 0; r != rows; ++r)
    	{
    		int c = 0;
    		
    		while (c != cols)
    		{
    			if (r == 0 || r == rows - 1 || c == 0)
    			{
    				cout << "*";
    			}
    			
    			else if ( c == cols - 1)
    			
    			{
    				cout << "*" << endl;
    			}
    
    			else
    			{
    				cout << " ";
    			}
    
    
    			++c;
    		}
    		
    	}
    
    
    	
    
    	getch();
    	return 0;
    
    
    	
    
    	
    
    	
    
    }
    When run, the only problem is the top row has 11 * symbols and then a space of about 10 and then one more * symbol. Could anybody shed any light as to where I have gone wrong.

    Thanks,

    Darren.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I hav sorted it now by drawing the top and bottom lines seperately.

    Code:
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    
    {
    
    	
    	// declare border length of square
    	
    	const int sides = 10;
    
    	// draw top line
    	for (int r = 0; r != sides; ++r)
    	{
    		cout << "*";
    	}
    
    	cout << endl;
    
    	
    	int row = 0;
    	
    	// draw middle lines
    	while (row < sides)
    	{
    
    	int col = 0;
    
    	while (col < sides)
    	{
    		++col;
    			if (col == 1 || col == sides)
    			{
    				cout << "*";
    			}
    			
    			else 
    			{
    				cout << " ";
    			}
    
    	}
    	cout << endl;
    	++row;
    	}
    
    	// draw bottom line
    	for (int r = 0; r != sides; ++r)
    	{
    		cout << "*";
    	}
    
    
    	getch();
    	return 0;
    
    
    }
    Is that a better way to do it?

    Thanks.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Code:
    // draw top line
    	for (int r = 0; r != sides; ++r)
    	{
    		cout << "*";
    	}
    A little misleading when I first read it because I saw r thinking rows but in reality your only printing a single row.

    Why not have one loop that controls ALL the rows and inside of the loop check which row you are on. An inner column loop can check exactly what * to print and how many depending on what row you are on. (You already have most of it)

    I believe this is the first thing covered in the Accelerated C++ Book. They have a good solution there.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by gamer4life687 View Post
    Code:
    // draw top line
    	for (int r = 0; r != sides; ++r)
    	{
    		cout << "*";
    	}
    A little misleading when I first read it because I saw r thinking rows but in reality your only printing a single row.

    Why not have one loop that controls ALL the rows and inside of the loop check which row you are on. An inner column loop can check exactly what * to print and how many depending on what row you are on. (You already have most of it)

    I believe this is the first thing covered in the Accelerated C++ Book. They have a good solution there.

    Thanks for the response. It is Accelerated C++ that I am currently working through, however no solutions to the problems are provided.

    Will have a go at doing it like you have suggested also.

    Thanks.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    The code needed for drawing the top and bottom rows is different than that needed for the middle rows. Therefore, it makes sense to separate it. It could be crammed into one loop, but it doesn't make the code any clearer. (If anything it runs a bit more slowly since it has to test for top/bottom rows on every row.)

    Code:
    int width = 10, height = 10;
    
    for (int h = 0; h < height; ++h) {
    	if (h == 0 || h == height - 1) {
    		// Top and bottom rows.
    		for (int w = 0; w < width; ++w) {
    			std::cout << "*";
    		}
    		std::cout << "\n";
    	} else {
    		// Middle rows.
    		std::cout << "*";
    		for (int w = 0; w < width - 2; ++w) {
    			std::cout << " ";
    		}
    		std::cout << "*\n";
    	}
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    1
    Hi all.

    Code:
    #include<iostream.h>
    
    int main()
    {
    	int side;
    	
    	cout <<"Enter Side: "; cin>>side;
    
    	side -= 1;
    
    	for (int i = 0; i<=side; i++)
    	{ 
    		for(int j=0; j<=side; j++)
    			cout<<(((i*j==0) || ((i*j)%side==0))? "*" : " ");
    
    		cout<<endl;
    	}
    
    	return 0;
    }

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Seriously? You wanted to brag so bad that you found an 18 month old thread?

    Don't bump old threads.

    Soma

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by Gabby View Post
    Hi all.

    Code:
    #include<iostream.h>
    
    int main()
    {
    	int side;
    	
    	cout <<"Enter Side: "; cin>>side;
    
    	side -= 1;
    
    	for (int i = 0; i<=side; i++)
    	{ 
    		for(int j=0; j<=side; j++)
    			cout<<(((i*j==0) || ((i*j)%side==0))? "*" : " ");
    
    		cout<<endl;
    	}
    
    	return 0;
    }
    Should i be impressed by the fact that you solved it in such a complicated and unnecessary way or by that you're using the good ol' C++ libraries?
    Devoted my life to programming...

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Ops, didn't notice it. Sorry!
    Devoted my life to programming...

  10. #10
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    Quote Originally Posted by Gabby View Post
    Code:
    	int side;
    	
    	cout <<"Enter Side: "; cin>>side;
    
    	side -= 1;
    that is such gorgeous code. now go write it in LISP in one line. someday our whole computers will be one giant ternary operation!

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Ternary?! Why ternary?... Not sure i get what you mean.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Comp Move help
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2008, 11:05 AM
  2. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  3. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  4. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  5. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM