Write a C++ program using arrays that can make patterns for you. Details: - The
ID: 3573820 • Letter: W
Question
Write a C++ program using arrays that can make patterns for you.
Details:
- The pattern will be held in a 15 x 15 multi-dimensional array and there are 6 different patterns
1. Box 2. X 3. Plus 4. Bordered X- box around the x 5. Bordered Plus- box around the plus 6. Checkerboard
x x x x x x x x x x x
x x x x x x x
x x x x x x x x x x x
x x x x x x x
x x x x x x x x x x x
-The pattern can be filled in with any character
-Fill in the grid with the given character
- Print the array to the file
-All patterns must be created using nested loops.
- Validate the pattern number
-If the pattern number is invalid, print an error message
- Program must have at least 3 functions other than main
-The function chain cannot be more than 2 levels deep
Input Info:
-All Input will be read from a file
-All input will be the proper format and within the proper range for the data type
-Each line in the file will contain 2 pieces of information: the pattern number and the fill character
-The format of each line will be: <pattern number><space><fill character><new line>
-The last line in the file will not contain a new line character.
Output Info:
-All output will be written to a file
-Each pattern will be drawn to the file using the specified fill character
-Print 2 blank lines between each pattern/error message
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
void p1(char c);
void p2(char c);
void p3(char c);
void p4(char c);
void p5(char c);
void p6(char c);
int main()
{
int t;
clrscr();
cin>>t;
ifstream myfile("input.txt");
char line[5];
if(myfile.is_open())
{
while(getline(myfile,line))
{
int t=line[0] - '0';
char c=line[2];
switch(t)
{
case 1:
p1(c);
break;
case 2:
p2(c);
break;
case 3:
p3(c);
break;
case 4:
p4(c);
break;
case 5:
p5(c);
break;
case 6:
p6(c);
break;
default:
cout<<"pattern number is invalid";
}
}
}
getch();
return 0;
}
void p1(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i!=0 || i!=14)
{
if(j==0 || j==14)
arr[i][j]=c;
}
if(i==0 || i==14)
arr[i][j]=c;
}
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
void p2(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i==j ||j==(13+1)-i)
{
arr[i][j]=c;
}
}
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
void p3(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
if(i==7 || j==7)
arr[i][j]=c;
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
void p4(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i!=0 || i!=14)
{
if(j==0 || j==14)
arr[i][j]=c;
}
if(i==0 || i==14)
arr[i][j]=c;
if(i==j ||j==(13+1)-i)
{
arr[i][j]=c;
}
}
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
void p5(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i!=0 || i!=14)
{
if(j==0 || j==14)
arr[i][j]=c;
}
if(i==0 || i==14)
arr[i][j]=c;
if(i==7 ||j==7)
{
arr[i][j]=c;
}
}
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
void p6(char c)
{
char arr[15][15];
for(int r=0;r<15;r++)
for(int s=0;s<15;s++)
arr[r][s]=' ';
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(i!=0 || i!=14)
{
if(j==0 || j==14)
arr[i][j]=c;
}
if(i==0 || i==14)
arr[i][j]=c;
if(i%2==0 ||j%2!=0)
{
arr[i][j]=c;
}
}
}
for(int k=0;k<15;k++)
{
for(int l=0;l<15;l++)
{
cout<<arr[k][l];
}
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.