I need to write a program that can create 4 different patterns of varying sizes.
ID: 3640959 • Letter: I
Question
I need to 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. The size must be between 2 and 9. user inputs choice of pattern and size.Here are the 4 patterns in size 5 (for example)
pattern 1 pattern2 pattern3 pattern4
5**** ***** 5***5 *555*
*5*** 5**** ***** 55555
**5** 55*** ***** 55555
***5* 555** ***** 55555
****5 5555* 5***5 *555*
per assignment, i have to use separate functions to display the menu, get user's menu choice, get user's size choice, and display pattern.
I have a good portion of this program running, i just can't figure out the combination of loops and (++) (--) to get patterns 2,3, and 4 to display correctly.
Below is what i have so far that's working:
#include <iostream>
using namespace std;
void showmenu ();
double Get_option ();
double 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 ();
size1 = Get_size ();
if (option1 != quit)
{
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;
}
}
} 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;
}
double 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;
}
double 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;
}
}
Explanation / Answer
Please rate...
Here is the complete code...
The quit part of the program is also rectified...:)..
#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;
}
}