I want to create a program that draws a diamond or a triangle with a size that t
ID: 3621647 • Letter: I
Question
I want to create a program that draws a diamond or a triangle with a size that the user selects using a specific character that will be entered at the keyboard. Here are two examples:A triangle of size 4, using *:
*
***
*****
*******
A diamond of size 4, using *:
*
***
*****
*******
*****
***
*
Inputs:
1) a choice to draw one of the two shapes or to quit the program,
2) a character choice which will be used for drawing the selected shape, and
3) the size of the shape
Output:
One of these will be the output:
1) a triangle,
2) a diamond,
3) quit
Explanation / Answer
please rate - thanks
let me know if you haven't learned functions
#include<iostream>
using namespace std;
void drawtriangle(int);
void drawbottom(int);
int main()
{
int max,i,j;
int shape=0;
while(shape!=3)
{
cout<<"Enter shape to draw: ";
cout<<"1-triangle 2-diamond 3-quit ";
cin>>shape;
if(shape==3)
return 0;
cout<<"Enter the size of the shape: ";
cin>>max;
if(shape==1)
drawtriangle(max);
else if(shape==2)
{drawtriangle(max);
drawbottom(max);
}
else
cout<<"invalid shape ";
}
}
void drawtriangle(int max)
{int i,j;
for(i=1;i<=max;i++)
{for(j=0;j<i*2-1;j++)
cout<<"*";
cout<<endl;
}
}
void drawbottom(int max)
{int i,j;
for(i=max;i>1;i--)
{for(j=(i-1)*2-1;j>0;j--)
cout<<"*";
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.