Patterns Write a program that can create 4 different patterns of varying sizes.
ID: 3852185 • Letter: P
Question
Patterns
Write a program that can create 4 different patterns of varying sizes. The size of each pattern is determined by the number of columns or rows. For example, a pattern of size 5 has 5 columns and 5 rows. Each pattern is made of the character * and a digit, which shows the size. The size must be between 5 and 9.
Here are the four patterns in size 5:
Pattern 1
Pattern 2
Pattern 3
Pattern 4
*5555
*****
55555
5555*
5*555
*555*
5***5
555*5
55*55
*555*
5***5
55*55
555*5
*555*
5***5
5*555
5555*
*****
55555
*5555
Your program must display a menu and ask the user to choose a pattern and size. Please note that your program must be robust; it must prompt the user to choose an option only between 1 and 5 and a pattern size only between 5 and 9. Use data validation loops to make sure that a number in the appropriate range is entered for both the option and the pattern size.
A sample menu, with sample user input is shown below:
M E N U
1. Pattern One
2. Pattern Two
3. Pattern Three
4. Pattern Four
5. Quit
Enter Option (1 to 5): 11
Option incorrect. Try again.
Enter option (1 to 5): 3
Enter pattern size (5 to 9): 22
Pattern size incorrect. Try again.
Enter Pattern Size (5 to 9): 7
Use separate functions to get the option, get the size, and to print each pattern. Be sure to pass the size into the pattern functions. When your program has processed the first user option, it should loop back around to re-print the menu, read another option from the user, process it, and so on, until the user enters 5, which should cause the program to stop.
Each pattern must be printed using a set of nested loops. The functions to print the patterns must use some sort of method to recognize when to print the number that is the size and when to print the *. The following function prints Pattern 1 for any size. Note that as we study the pattern, we see that when the row and column are equal, the * is printed but for all other locations in the pattern, size is printed.
void pattern1(int size)
{
int row, col;
// loop through the rows
for (row=0; row < size; row++)
{
// print one row
for (col=0; col < size; col++)
{
if (row == col)
printf(“*”);
else
printf(“%c”, size); }
printf(“ ”);
}
}
To determine the logic needed for the rest of the patterns, it may be helpful to draw a grid on a piece of paper, including the row and column number of each ‘cell’. Put the pattern into the cells, then look for something consistent for all *’s and for all numbers and code this into your function. Hint: For Pattern 4, consider the sum of the row and column number for each cell.
Pattern 1
Pattern 2
Pattern 3
Pattern 4
*5555
*****
55555
5555*
5*555
*555*
5***5
555*5
55*55
*555*
5***5
55*55
555*5
*555*
5***5
5*555
5555*
*****
55555
*5555
Explanation / Answer
#include <iostream>
#include<stdlib.h>
using namespace std;
void showmenu ();
int Get_option ();
int Get_size ();
void pattern1 (int);
void pattern2 (int);
void pattern3 (int);
void pattern4 (int);
int main ()
{
int size1,option1;
const int pattern_1 = 1,
pattern_2 = 2,
pattern_3 = 3,
pattern_4 = 4,
quit = 5;
do
{
showmenu ();
option1 = Get_option ();
if(option1!=5)size1 = Get_size ();
switch (option1)
{
case pattern_1:
pattern1 (size1);
break;
case pattern_2:
pattern2 (size1);
break;
case pattern_3:
pattern3 (size1);
break;
case pattern_4:
pattern4 (size1);
break;
case quit:
exit(1);
break;
}
} while (option1 != quit);
return 0;
}
void showmenu ()
{
cout << " M E N U " << endl;
cout << "1. Pattern One" << endl;
cout << "2. Pattern Two" << endl;
cout << "3. Pattern Three" << endl;
cout << "4. Pattern Four" << endl;
cout << "5. Quit " << endl;
}
int Get_option ()
{
int option;
cout << "Enter option (1-5): " << endl;
cin >> option;
while (option < 1 || option > 5)
{
cout << "Incorrect option. Try again" << endl;
cout << "Enter option (1-5): " << endl;
cin >> option;
}
return option;
}
int Get_size ()
{
int size;
cout << "Enter pattern size (2-9): " << endl;
cin >> size;
while (size < 2 || size > 9)
{
cout << "Incorrect pattern size. Try again" << endl;
cout << "Enter pattern size (2-9): " << endl;
cin >> size;
}
return size;
}
void pattern1 (int size1)
{
int row, col;
for (row = 0; row < size1; row++)
{
for (col = 0; col < size1; col++)
{
if (row == col)
cout << size1;
else
cout << '*';
}
cout << endl;
}
}
void pattern2 (int size1)
{
int row, col;
for (row = 0; row < size1; row++)
{
for (col = 0; col < size1; col++)
{
if (col>=row)
cout << '*';
else
cout << size1;
}
cout << endl;
}
}
void pattern3 (int size1)
{
int row, col;
for (row = 0; row < size1; row++)
{
for (col = 0; col < size1; col++)
{
if ((col==0 && row==0)||(col==size1-1 && row==size1-1))
cout << size1;
else if((col==0 && row==size1-1)||(col==size1-1 && row==0))
cout<< size1;
else
cout << '*';
}
cout << endl;
}
}
void pattern4 (int size1)
{
int row, col;
for (row = 0; row < size1; row++)
{
for (col = 0; col < size1; col++)
{
if ((col==0 && row==0)||(col==size1-1 && row==size1-1))
cout << '*';
else if((col==0 && row==size1-1)||(col==size1-1 && row==0))
cout<< '*';
else
cout << size1;
}
cout << endl;
}
}