denny wong
denny wong

Reputation: 11

Make a square pattern in C++

I have a task to make a half-square pattern, where the input must be an even number

Example 1

Enter row & column = 4
Output
WWOO
wWOO
OOWW
OOWW

Example 2

Enter row & column = 8
Output
WWWWOOOO
WWWWOOOO
WWWWOOOO
WWWWOOOO
OOOOWWWW
OOOOWWWW
OOOOWWWW
OOOOWWWW

here's my code so far,i have tried to show even number output, but i don't have any ide to show half square pattern

int main ()
{
    int size;
    cout<<"Square Pattern\n";
    cout<<"==============\n";
    cout<<"Input :";cin>>size;
    
    if(size%2!=0)
    {
        cout<<"Input must be even number !";
    }
    else
    {
        for(int i = 0; i < size; i++)
        { 

            for(int j = 0; j < size; j++)
            {  
                if( ? )
                {
                    cout << "W";
                }
                else
                {
                    cout << "O";
                }

            }
            cout << endl;
        }
    }
        

Upvotes: 0

Views: 522

Answers (4)

CiaPan
CiaPan

Reputation: 9570

You need to output 'W' if both the number of the row and the number of the character in the row are less than size/2 or both are greater than or equal to size/2; and 'O' otherwise.

    for(int row = 0; row < size; row++)
    {
        for(int column = 0; column < size; column++)
            if( (row < size/2) == (column < size/2) )
                cout << "W";
            else
                cout << "O";

        cout << endl;
    }
        

Upvotes: 1

zoldxk
zoldxk

Reputation: 1

This will return the pattern you want :

#include <iostream>

using namespace std;

int main ()
{
    int size;
    cout<<"Square Pattern\n";
    cout<<"==============\n";
    cout<<"Input :";cin>>size;
    
    if(size%2!=0)
    {
        cout<<"Input must be even number !";
    }
    else
    {
        for(int i = 0; i < size/2; i++)
        { 

            for(int j = 0; j < size; j++)
            {  
                if( j<size/2 )
                {
                    cout << "W";
                }
                else
                {
                    cout << "O";
                }

            }
            cout << endl;
        }
        for(int i = size/2; i < size; i++)
        { 

            for(int j = 0; j < size; j++)
            {  
                if( j<size/2 )
                {
                    cout << "O";
                }
                else
                {
                    cout << "W";
                }

            }
            cout << endl;
        }
    }
}

Upvotes: 0

doomsday
doomsday

Reputation: 173

I modified your snippet to which gives the required result:

int main ()
{
    int size;
    cout<<"Square Pattern\n";
    cout<<"==============\n";
    cout<<"Input :";cin>>size;
    
    if(size%2!=0)
    {
        cout<<"Input must be even number !";
    }
    else
    {
        for(int i = 0; i < size; i++)
        { 

            for(int j = 0; j < size; j++)
            {  
                //Actual different part from your code
                /* There will be 2 nested if condition because your pattern is 
                dependent on both "i" and "j" */ 
                if(i<size/2){
                    if( j<size/2)
                    {
                        cout << "W";
                    }
                    else
                    {
                        cout << "O";
                    }
                }else{
                    if( j<size/2)
                    {
                        cout << "O";
                    }
                    else
                    {
                        cout << "W";
                    }
                }
            }
            cout << endl;
        }
    }
}

Upvotes: 0

Suyash Krishna
Suyash Krishna

Reputation: 241

Using two loops. Keeping two characters c1 and c2. In the first half, c1 is set to 'W' and c2 is set to 'O'. Interchanging the contents of the variables after the first half of the total iterations. Try this :

#include<iostream>
using namespace std;
int main ()
{
    int size;
    cout<<"Square Pattern\n";
    cout<<"==============\n";
    cout<<"Input :";cin>>size;
    char c1,c2;
    if(size%2!=0)
    {
        cout<<"Input must be even number !";
    }
    else
    {
        for(int i = 0; i < size; i++)
        {
            if((i+1)<=size/2)
            {
                c1 = 'W'; c2 = 'O';
            }
            else
            {
                c1 = 'O'; c2 = 'W';
            }
            for(int j = 0; j < size; j++)
            {  
                if((j+1)<=size/2)
                {
                    cout << c1;
                }
                else
                {
                    cout << c2;
                }
            }
            cout << endl;
        }
    }
}

Upvotes: 0

Related Questions