Thread: How to draw shapes with only for loops?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    you could use a 2d array for it, you need to be able to store and check whether it is a "#" or a "-", and you can't do that with cout and cin(?). here's some code below, as you can see you have to set the values of the array before you use it otherwise you'd just have a block of 0's (you could also set these by reading values from a file)
    Code:
    int screen[10][8] = {1, 0, 0, 0, 0, 0, 0, 0,
    		     1, 1, 0, 0, 0, 0, 0, 0,
    		     1, 1, 1, 0, 0, 0, 0, 0,
    		     1, 1, 1, 1, 0, 0, 0, 0,
    		     1, 1, 1, 1, 0, 0, 0, 0,
    		     1, 1, 1, 1, 1, 0, 0, 0,
    		     1, 1, 1, 1, 1, 0, 0, 0,
    		     1, 1, 1, 1, 1, 1, 0, 0,
    		     1, 1, 1, 1, 1, 1, 1, 0,
    		     1, 1, 1, 1, 1, 1, 1, 1}; //[row][column] 10 down eight across
    
    //"draw" your triangle
    for (int row = 0; row < 9; row++){
    	for (int column = 0; column < 7; column++){
    		if (screen[row][column] == 1){
    			cout << "#";
    		}
    		else{
    			cout << "-";
    		}
    	}
    	cout << "\n";
    }
    cout << "\n";
    //flip  # into - and vice versa
    for (int row = 1; row < 10; row++){ // for some reason 0 and 9 was making this get offset by one line? however this should make it go out of the array... but it doesn't...
    	for (int column = 7; column > 0; column--){//draw last column first and first column last, thus flipping the image horizontally
    		if (screen[row][column] == 1){
    			cout << "#";
    			}
    		else{
    			cout << "-";
    		}
    	}
    	cout << "\n";
    }
    cout << "\n";
    Last edited by robolee; 10-17-2009 at 04:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to draw shapes and output?
    By Diablo02 in forum C# Programming
    Replies: 3
    Last Post: 11-20-2007, 07:11 PM
  2. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11:56 PM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  5. Draw Shapes.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-19-2002, 09:22 AM