Write a C++ program which displays a \'desk\' made out of asterisks. Prompt the
ID: 3634212 • Letter: W
Question
Write a C++ program which displays a 'desk' made out of asterisks. Prompt the user for desktop thickness and desk width.The leg height will be equal to the desktop thickness. The leg width is also equal to the desktop thickness.Display the desk. After the desk has been displayed, your program will display the string "PROGRAM ENDS", followed by a single newline character.
Your program must use the three instructor-supplied functions 'printStar', 'printSpace', and 'printNewline' to display the desk. Credit will only be awarded for programs which use these three functions to generate the desk.
NOTES:
0) width and thickness are valid ints in the specified ranges.
1) width is in the range 7 - 12 inclusive.
2) thickness is in the range 1 - 5 inclusive.
ASSUMPTIONS:
0) all values entered will be ints in the specified ranges.
1) in all cases, width > 2 * thickness.
EXAMPLES:
Explanation / Answer
#include<iostream.h>
//create class desk
class desk{
private:
//define variables
int thickness;
int width;
int legheight;
int legwidth;
public:
//define functions
void setdata(int,int);
void printstar();
void printspace();
void printnewline();
};//end class
//Definition of function setdata to store values
void desk :: setdata(int dt,int dw)
{
thickness=dt;
width=dw;
legheight=thickness;
legwidth=width;
}
//function to print stars
void desk :: printstar()
{
cout<<"*";
}
//function to print space
void desk :: printspace()
{
cout<<" ";
}
//function to print new line
void desk :: printnewline()
{
cout<<" ";
}
//main function
int main()
{
int t,w;
int i,j;
//create object
desk d;
//read values from user
cout<<"Enter Desktop thickness ";
cin>>t;
cout<<" Enter Desk width ";
cin>>w;
//call function set data
d.setdata(t,w);
//check the condition and display the output
if((t>=1 && t<=5) && (w>=7 && w<=12) && (w>2*t))
{
for(i=1;i<=t;i++)
{
for(j=1;j<=w;j++)
d.printstar();
d.printnewline();
}
for(i=1;i<=t;i++)
{
for(j=1;j<=w;j++)
{
if(j<=t || j>(w-t))
d.printstar();
else
d.printspace();
}
d.printnewline();
}
cout<<" PROGRAM ENDS";
}
else
cout<<"Invalid data";
return 0;
}//end main
-------------------------------------------------------------------------------------------------
Output1:
Output 2:
Output 3:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.