Results 1 to 3 of 3

Thread: For and If statement

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Question For and If statement

    Version: Dev-C++ 4
    OS : Win98
    Subject: Using “For” and “If” statement to produce a square.


    Hello,

    Working my way through some excises. One of the tasks is to draw a square( not rocket science I know).
    I can produce the square, but I’m sure there is a more efficient means.

    Some pointers please!

    Code:
    #include <iostream.h>

    void drawSquare(int x);

    int main ()
    {
    drawSquare(20);
    return 0;
    }

    void drawSquare(int x )
    {
    for (int a =1 ; a <=x ; a++)
    for (int b =1; b <=x;b++)
    if (a==1 || a==x ) cout << "a" ;
    else
    if (a==2 && b==1) cout << "\n" << "b";
    else
    if(b>1 && b< x) cout << " ";
    else
    if (b ==1)cout <<"b";
    else
    cout <<"b"<<"\n";

    Thanks

    hmm

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    I hope this helps:

    Code:
    #include <iostream.h> 
    
    void drawSquare(int x); 
    
    int main () 
    { 
    drawSquare(20); 
    return 0; 
    } 
    
    void drawSquare(int x ) 
    { 
    	for (int a =1 ; a <=x ; a++) 
    	{
    		for (int b =1; b <=x;b++) 
    		{
    			if (a==1 || a==x)
    				cout<<"a";
    			else
    			{
    				if (b==1 || b==x)			
    					cout<<"b";
    				else
    					cout<<" ";
    			}
    		}
    		cout<<"\n";
    	}
    };

  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    UK
    Posts
    40

    Thanks Chris

    Thanks Chris! A lot cleaner way of achieving it



    hmm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width