I was given a series of question that will lead to a final aswer.
Suppose we want to display the following pattern on the screen:
ssssOOOOssssOOOO
ssssOOOOssssOOOO
ssssOOOOssssOOOO
ssssOOOOssssOOOO
OOOOssssOOOOssss
OOOOssssOOOOssss
OOOOssssOOOOssss
OOOOssssOOOOssss
ssssOOOOssssOOOO
ssssOOOOssssOOOO
ssssOOOOssssOOOO
ssssOOOOssssOOOO
OOOOssssOOOOssss
OOOOssssOOOOssss
OOOOssssOOOOssss
OOOOssssOOOOssss
The size of the pattern determines the number of groups of characters in a row or a column and also the number of characters in the row or column of one group. The size of the above example is therefore 4, because each group, whether it be a group of s’s or a group of O’s, has 4 characters in each row and column of the group. There are also
4 groups across and 4 groups down the total pattern. If the size is 2, the pattern will look as follows:
ssOO
ssOO
OOss
OOss
Question 3a: One value parameter
I was given the function drawPattern that will display such a pattern. The function has one parameter of type
int, representing the size of the pattern. The function uses a nested for loop. The outer loop runs from 1 to size
* size. This will handle the rows of the pattern. The inner for loop also runs from 1 to size * size,
completing each column. The for loop is a powerful loop construct.
Your task is to write the main function. Ask the user the input the size of the pattern, using a size between 1 and 10,
and then call the function drawPattern to display the pattern. Test the program with different sizes. Make sure
that the pattern displays correctly, but do not submit printouts of the output.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
void drawPattern(int sizeP)
{
for (int i = 0; i < (sizeP * sizeP) ; i++)
{
cout << endl;
for (int j = 0; j < (sizeP * sizeP); j++)
{
if ( (i / sizeP) % 2 == 0)
if ((j / sizeP) % 2 == 0)
cout << 's';
else
cout << 'O';
else
if ((j / sizeP) % 2 == 0)
cout << 'O';
else
cout << 's';
}
}
}
int main()
{
//complete the main function
}
| |
Question 3b: Three value parameters
In Question 3a, the character ‘s’ is used to start the pattern, and ‘O’ is used for every alternate pattern. Now
change the main function so that the user can input both the characters that form the pattern. Use proper cout
statements, so that it is clear to the user what the characters are used for. Define two char variables start and
next for this purpose. The calling statement must now include three parameters – one of type int for the size of
the pattern, and two of type char for the characters to display the pattern. You also need to change the function
drawPattern to have three value parameters - an int parameter indicating the size of the pattern, and two
char parameters indicating the two character that will be used to display the pattern. Input 4 for the size, ‘Y’ for the character to be used to start the pattern, and ‘+’ for the second and every alternate group. Make sure that the
pattern displays correctly, and submit printouts of your program code and the output.
Program:
1 2 3 4 5 6 7
|
#include <iostream>
using namespace std;
//the required changed function drawPattern should be inserted here.
int main()
{
// the changed main function should be inserted here
}
| |
This is my answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
using namespace std;
void drawPattern(int sizeP, char startP, char nextP)
{
for (int i = 0; i < (sizeP * sizeP); i++)
{
cout << endl;
for (int j = 0; j < (sizeP * sizeP); j++)
{
if ((i / sizeP) % 2 == 0)
if ((j / sizeP) % 2 == 0)
cout << startP ;
else
cout << nextP;
}
}
}
int main( )
{
int size;
char start, next;
cout << "Enter size(1 to 10): ";
cin >> size;
cout << "Enter two characters: ";
cin >> start >> next;
drawPattern(size, start, next);
return 0;
}
| |
Output
Enter size(1 to 10): 4
Enter two characters: Y +
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++
YYYY++++YYYY++++ |
The space between the pattens is the concern. Help.