Question

In: Computer Science

Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a...

Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) . Don't use function.

Example 1:

Input width: 16
Input height: 11

Shape:
***   ***   ***
***   ***   ***
***   ***   ***
   ***   ***   *
   ***   ***   *
   ***   ***   *
***   ***   ***
***   ***   ***
***   ***   ***
   ***   ***   *
   ***   ***   *

Example 2:

Input width: 27
Input height: 27

Shape:
***   ***   ***   ***   ***
***   ***   ***   ***   ***
***   ***   ***   ***   ***
   ***   ***   ***   ***   
   ***   ***   ***   ***   
   ***   ***   ***   ***   
***   ***   ***   ***   ***
***   ***   ***   ***   ***
***   ***   ***   ***   ***
   ***   ***   ***   ***   
   ***   ***   ***   ***   
   ***   ***   ***   ***   
***   ***   ***   ***   ***
***   ***   ***   ***   ***
***   ***   ***   ***   ***
   ***   ***   ***   ***   
   ***   ***   ***   ***   
   ***   ***   ***   ***   
***   ***   ***   ***   ***
***   ***   ***   ***   ***
***   ***   ***   ***   ***
   ***   ***   ***   ***   
   ***   ***   ***   ***   
   ***   ***   ***   ***   
***   ***   ***   ***   ***
***   ***   ***   ***   ***
***   ***   ***   ***   ***

Solutions

Expert Solution

C++ Program

#include <iostream>
using namespace std;

int main() {
  
int width,height; // Declare two integer variables width and height
// input two integer variables
cout<<"Input width: "; cin>>width;
cout<<"Input height: "; cin>>height;

string board = ""; // declare string obj board initialize ""
  
cout<<"\n\nShape:"<<endl;
  
for (int i = 0; i < height; ++i) // create for loop i until height outer loop
   {
for (int j = 0, c = '*'; j < width; ++j, c = '*') // create for loop j and declare initeger c='*' until width
       {
           // check condition with bitwise operator 'and' and 'xor' values if it true c=' '(space)
           // calculate every 3 rows display gap
if (((j / 3) & 1) ^ ((i / 3) & 1))
c = ' ';

board += (char)c; // else, convert c integer into character and add character '*' into board object
}

board += '\n'; // the control is passed into new line
}

cout <<board; // print the entire string outside for loops
return 0;
}

OUTPUT-1

Input width: 16
Input height: 11


Shape:
*** *** ***
*** *** ***
*** *** ***
*** *** *
*** *** *
*** *** *
*** *** ***
*** *** ***
*** *** ***
*** *** *
*** *** *

OUTPUT-2

Input width: 27
Input height: 27


Shape:
*** *** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** ***
*** *** *** *** ***
*** *** *** *** ***
*** *** *** *** ***


Related Solutions

Task G. Checkerboard (3x3) Write a program checkerboard3x3.cpp that asks the user to input width and...
Task G. Checkerboard (3x3) Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) Don't use function. Please explain in detail and make it simple. And be clear with the {}. Thank you in advance. Example 1: Input width: 16 Input height: 11 Shape: *** *** *** *** *** *** *** *** *** *** *** *...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Must implement a simple program that asks the user for a width and a height, storing...
Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height. Area and Perimeter Calculator Enter width: 3 Enter height: 5 If your shape is a rectangle, its area is 15.0...
Write a short program that asks the user for a sentence and prints the result of...
Write a short program that asks the user for a sentence and prints the result of removing all of the spaces. Do NOT use a built-in method to remove the spaces. You should programmatically loop through the sentence (one letter at a time) to remove the spaces.
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a program that asks the user for an integer and then prints out all its...
Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 84, the program should print 2 2 3 7 Validate the input to make sure that it is not a character or a string using do loop.in c++
Write a Python program that takes as input two numbers, the height, and width of a...
Write a Python program that takes as input two numbers, the height, and width of a rectangle. It then prints a rectangle with height lines and width characters in each line. The rectangle should contain information regarding its dimensions and area inside (centered vertically but not horizontally). If any part of the information (including two stars on each end and a space before and after the line) does not fit in the rectangle, then print the complete information after the...
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT