You need to submit printouts for question 4c only.

We want to display one of the following blocks on the screen, using the same program:

``````***************************          ************************
*            *            *          *                      *
*            *            *          *                      *
*            *            *          *                      *
***************************          *                      *
*            *            *          *                      *
*            *            *          *                      *
*            *            *          ************************
***************************``````

The size of the pattern is determined by the number of rows in the block. The size of the first block is 9 and the size of the second block is 8. The number of ‘*’ characters across is equal to 3 times the number of rows, therefore the first block has 9 * 3 = 27 ‘*’ characters across and the second block has 8 * 3 = 24 ‘*’ characters across. The cross in the middle of the block only gets displayed when the number of rows (and therefore also the number of columns) is an odd number.

Your program must decide, according to the size of the block, whether it should display the cross, or not. The minimum size of the block is 4. The given main function takes care of that already.

Do not submit printouts for 4a and 4b, only for 4c.

Question 4a: One value parameter

We give the main function below. Write a void function drawBlock that will display such a block on the screen, according to the size specified as input. The function must have one parameter of type int, representing the size of the block.
Hint: Use a nested for loop. The outer loop runs from 1 to size. This will handle the number of rows in the block. The inner for loop runs from 1 to size * 3, completing each row. You should only have one nested for loop in your function that will handle odd and even input values, and therefore both types of blocks. Do not write separate nested for loops for the two types of blocks.

Test the program with input sizes 7 and 10 and make sure that the block of size 7 has the cross in the middle. Do not hand in the printouts of the program.

Program:

``````//Assignment 2 question 4a
#include <iostream>
using namespace std;

// The function drawBlock must be inserted here

int main()
{
int size = 0;

do
{

cout << endl << "Please enter the size of the block that you want "
<< "displayed. " << endl;
cout << "The Size must be at least 4 : ";
cin >> size;
}
while ( size < 4);
drawBlock(size);

return 0;``````

This is what I've got so far, it displays the block, but I'm not to sure about the cross for odd numbers, maybe the % should be used to check for odd or even numbers?

``````//Assignment 2 question 4a
#include <iostream>
using namespace std;

void drawBlock (int p)
{
cout << endl;
for (int i = 1; i <= p; i++)                 //rows
{
for (int j = 1; j <= p * 3; j++)         //columns
{
if ((i == 1) || (i == p))            //top and bottom
cout << "*";
else if ((j == 1) || (j == p * 3))   //left and right
cout << "*";
else
cout << " ";                     //"empty" body
if ((i > 1) || (i < p))            //top and bottom
cout << "";
else if ((j == 1) || (j == p * 3))   //left and right
cout << "";
else
cout << " ";
}
cout << endl;
}
}

int main()
{
int size = 0;

do
{

cout << endl << "Please enter the size of the block that you want "
<< "displayed. " << endl;
cout << "The Size must be at least 4 : ";
cin >> size;
}
while ( size < 4);
drawBlock(size);

return 0;
}``````

Any suggestions would greatly be appreciated.

Try something like this for your drawBlock method.

``````void drawBlock (int p)
{
int width = p * 3;
bool isodd = false;
int oddrow = 0;
int oddcol = 0;
if(p % 2)		// Test if odd;
{
isodd = true;
oddrow = (p / 2);
oddcol = width / 2;
}

cout << endl;

for (int row = 0; row < p; row++)                 //rows
{
for (int col = 0; col < width; col++)         //columns
{
if ((row == 0) || (row == p - 1) || (col == 0) || col == width - 1
|| (isodd && (col == oddcol)) || (isodd && (row == oddrow)))
cout << "*";
else
cout << " ";			//"empty" body
}
cout << endl;
}
}``````

Cheers Milton

Thanks for the help Milton

-Solved-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.