sample output: Draw an n by n square: Enter the size n: 5 Is the square solid? (
ID: 3629790 • Letter: S
Question
sample output:Draw an n by n square: Enter the size n: 5
Is the square solid? (y/n) n
Give me the character to draw the border: A
AAAAA
A A
A A
A A
AAAAA
Y or y continues, any other character terminates: y
Draw an n by n square: Enter the size n: 8
Is the square solid? (y/n) y
Give me the character to draw the border: e
Give me the character to draw the inner solid portion: x
eeeeeeee
exxxxxxe
exxxxxxe
exxxxxxe
exxxxxxe
exxxxxxe
exxxxxxe
eeeeeeee
Y or y continues, any other character terminates:
=============================================================
function should be int main ( )
void GetSqareInfo(bool& isSolid, char& border, char& inner, int& size);
void DrawSquare(bool isSolid, char border, char inner, int n);
the main function :
int main()
{
... ...
// Receive the square data entered by the user
GetSqareInfo(isSolid, border, inner, size);
// Now prepared all arguments, call this to Draw a Square based on isSolid, border, inner, and size
DrawSquare(isSolid, border, inner, size);
... ...
return 0;
}
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
void GetSqareInfo(bool& isSolid, char& border, char& inner, int& size);
void DrawSquare(bool isSolid, char border, char inner, int n);
int main()
{int size;
char yorno,border,inner;
bool isSolid;
do
{
// Receive the square data entered by the user
GetSqareInfo(isSolid, border, inner, size);
// Now prepared all arguments, call this to Draw a Square based on isSolid, border, inner, and size
DrawSquare(isSolid, border, inner, size);
cout<<"Y or y continues, any other character terminates: ";
cin>>yorno;
}while(yorno=='y'||yorno=='Y');
return 0;
}
void GetSqareInfo(bool& isSolid, char& border, char& inner, int& size)
{char yorno;
cout<<"Draw an n by n square: Enter the size n:";
cin>>size;
cout<<"Is the square solid? (y/n) ";
cin>>yorno;
cout<<"Give me the character to draw the border: ";
cin>>border;
if(yorno=='y'||yorno=='Y')
{isSolid=true;
cout<<"Give me the character to draw the inner solid portion: ";
cin>>inner;
}
else
{isSolid=false;
inner=' ';
}
}
void DrawSquare(bool isSolid, char border, char inner, int n)
{int i,j;
for(i=0;i<n;i++)
if(i==0||i==n-1)
{for(j=0;j<n;j++)
cout<<border;
cout<<endl;
}
else
{cout<<border;
for(j=0;j<n-2;j++)
cout<<inner;
cout<<border<<" ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.