1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tresset_1 [31]
2 years ago
12

Write a function that returns a chessboard pattern ("B" for black squares, "W" for white squares). The function takes a number N

as input and generates the corresponding board.
Input The first line of the input consists of an integer- num, representing the size of the chessboard.
Output Print N lines consist of N space-separated characters representing the chessboard pattern.

Computers and Technology
1 answer:
Kryger [21]2 years ago
8 0

Answer:

Here is the C++ program.

#include <iostream>  //to use input output functions

using namespace std;  // to access objects like cin cout

 

void chessboard(int N){  // function that takes a number N as parameter generates corresponding board

   int i, j;  

   string black = "B";  // B for black squares

   string white = "W"; // W for white squares

   for(i=1;i<=N;i++){  //iterates through each column of the board

       for(j=1;j<=N;j++){  //iterates through each row of the board

           if((i+j)%2==0)  // if sum of i and j is completely divisible by 2

               cout<<black<<" ";  //displays B when above if condition is true

           else  //if (i+j)%2 is not equal to 0

           cout<<white<<" ";  }  // displays W when above if condition is false

      cout<<endl;    }    }  //prints the new line

       

int main(){    //start of the main function

   int num;  //declares an integer num

   cout << "Enter an integer representing the size of the chessboard: ";  //prompts user to enter size of chess board

   cin >> num;  //reads value of num from user

   chessboard(num); } //calls chessboard function to display N lines consist of N space-separated characters representing the chessboard pattern

Explanation:

The function works as follows:

Lets say that N = 2

two string variables black and white are declared. The value of black is set to "B" and value of white is set to "W" to return a chessboard pattern in B and W squares.

The outer loop for(i=1;i<=N;i++) iterates through each column of the chess board. The inner loop  for(j=1;j<=N;j++) iterates through each row of chess board.

At first iteration of outer loop:

N = 2

outer loop:

i=1

i<=N is true because i=1 and 1<=2

So the program enters the body of the outer for loop. It has an inner for loop:

for(j=1;j<=N;j++)

At first iteration of inner loop:

j = 1

j<=N is true because j=1 and 1<=2

So the program enters the body of the inner for loop. It has an if statement:

if((i+j)%2==0) this statement works as:

if(1+1) % 2 == 0

(1+1 )% 2 takes the mod of 1+1 with 2 which gives the remainder of the division.

2%2 As 2 is completely divisible by 2 so 2%2 == 0

Hence the if condition evaluates to true. So the statement in if part executes:

cout<<black<<" ";

This prints B on the output screen with a space.

B

The value of j is incremented to 1.

j = 2

At second iteration of inner loop:

j = 2

j<=N is true because j=2 and 2=2

So the program enters the body of the inner for loop. It has an if statement:

if((i+j)%2==0) this statement works as:

if(1+2) % 2 == 0

(1+2 )% 2 takes the mod of 1+2 with 2 which gives the remainder of the division.

3%2 As 3 is not completely divisible by 2

Hence the if condition evaluates to false. So the statement in else part executes:

cout<<white<<" ";

This prints W on the output screen with a space.

B W

The value of j is incremented to 1.

j = 3

Now  

j<=N is false because j=3 and 3>2

So the loop breaks and program moves to the outer loop to iterate through the next row.

At second iteration of outer loop:

N = 2

outer loop:

i=2

i<=N is true because i=2 and 2 = 2

So the program enters the body of the outer for loop. It has an inner for loop:

for(j=1;j<=N;j++)

At first iteration of inner loop:

j = 1

j<=N is true because j=1 and 1<=2

So the program enters the body of the inner for loop. It has an if statement:

if((i+j)%2==0) this statement works as:

if(2+1) % 2 == 0

(2+1 )% 2 takes the mod of 2+1 with 2 which gives the remainder of the division.

3%2 As 3 is not completely divisible by 2

Hence the if condition evaluates to false. So the statement in else part executes:

cout<<white<<" ";

This prints W on the output screen with a space.

B W

W

The value of j is incremented to 1.

j = 2

At second iteration of inner loop:

j = 2

j<=N is true because j=2 and 2=2

So the program enters the body of the inner for loop. It has an if statement:

if((i+j)%2==0) this statement works as:

if(2+2) % 2 == 0

(2+2 )% 2 takes the mod of 2+2 with 2 which gives the remainder of the division.

4%2 As 4 is completely divisible by 2 so 4%2 == 0

Hence the if condition evaluates to false. So the statement in if part executes:

cout<<black<<" ";

This prints B on the output screen with a space.

B W

W B

The value of j is incremented to 1.

j = 3

Now  

j<=N is false because j=3 and 3>2

So the loop breaks and program moves to the outer loop. The value of outer loop variable i is incremented to 1 so i = 3

N = 2

outer loop:

i=3

i<=N is false because i=3 and 3>2

So this outer loop ends.

Now the output of this program is:

B W

W B

Screenshot of this program along with its output is attached.

You might be interested in
What is a custom information field that helps users to find a specific document?
gtnhenbr [62]
Go to start then type the name of the document or file
6 0
3 years ago
I have tried installing "windows media player" on my computer but at a point, something disrupts its instalment or I am asked to
Minchanka [31]
Windows media player should come pre-installed on your system and does not cost anything. You likely have malware on your system. I would download Malwarebytes antimalware from here

https://www.malwarebytes.org/mwb-download/thankyou/

There is a premium version but just do the free one. Install and run. Let it get rid of the bad stuff it finds. I would also make sure you have an up to date Antivirus software and run a scan. There are plenty of good free ones if you don't have one installed already.
6 0
2 years ago
________ work(s) by using radio waves to communicate with radio antennas placed within adjacent geographic areas.
RSB [31]
Cell phones work(s) by using radio waves to communicate with radio antennas placed within adjacent geographic areas.
4 0
3 years ago
________ is used for supervisory messages at the internet protocol layer.
Natalija [7]
"ICMP" <span>is used for supervisory messages at the internet protocol layer.
ICMP stands for </span>Internet Control Message Protocol and it refers to a supporting protocol in the Internet protocol suite. It is utilized by network devices which includes routers and these are used to send error messages and operational data showing, for instance, that asked for service which isn't accessible or that a host or switch couldn't be come to.
4 0
3 years ago
True or False: It is illegal to park in a location that you block or create a hazard for other vehicles.
Feliz [49]
The answer to question is true
5 0
3 years ago
Read 2 more answers
Other questions:
  • What can act as a buffer against unemployment
    11·1 answer
  • A device that make it possible for a muitiple customer to share one address is called
    13·1 answer
  • If your TV was showing a flat black or blue screen, or had "snow", what steps would you take to fix it?
    13·1 answer
  • Write 3 things that can't be done without technology. write your opinion?
    14·2 answers
  • Specifications that establish the compatibility of products and the ability to communicate in a network are called:
    10·1 answer
  • Which of the following technologies does NOT facilitate the transferring of data between computers in adjacent buildings?
    12·1 answer
  • Why is the Microsoft website considered the best source for information about pagefile.sys?​
    15·1 answer
  • computer have taken over a lot of boring, repetitive and time consuming as well as dangerous jobs.true or false
    7·1 answer
  • While working in a group of two the members are not getting along. You bring the two members together so they can discuss this i
    11·1 answer
  • The following Python statement instructs the program to open a file for input.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!