Can someone add SWITCH STATEMENT to the code below? Please, it has to be Switch
ID: 3591217 • Letter: C
Question
Can someone add SWITCH STATEMENT to the code below? Please, it has to be Switch Statement only! Prompt menu should look like this;
Press 1 for Rectangle:
Press 2 for Triangle (North to West):
Press 3 for Triangle (South to East):
Press 4 for Circle:
Press 5 to end this program:
// CODE STARTS HERE
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
// Task 1 : Draw a w-wide h-high rectangular frame, using asterisks.
int width, height, length;
cout<< "Task 1: Rectangular Frame"<<endl;
cout<< "Enter width: ";
cin>>width;
cout<< "Enter height: "; cin>>height;
for(int i = 0; i < height; ++i)
{
for(int j = 0; j < width; ++j)
{
if(i == 0 || j == 0 || i == height - 1 || j == width - 1)
{
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
// Task 2: Draw the triangle constituting the northwestern half of a square, given the side length.
cout<< "Task 2 North to West"<<endl;
cout<< "Enter length: ";
cin>>length;
for(int i=length; i >= 1; i--)
{
for(int j=1; j<=i; j++)
{
cout<< "*";
}
cout<< " ";
}
cout<<endl;
// Task 3: Draw the triangle constituting the southeastern half of a square, given the side length.
cout<< "Task 3: South to East"<<endl;
cout<< "Enter length: ";
cin>>length;
for(int i=length; i >= 1; i--)
{
for(int j=1; j <=i; j++)
{
cout<< " ";
}
for(int k=length;k>=i;k--)
{
cout << "*";
}
cout<<" ";
}
cout<<endl;
// Task: 4 Given a radius, draw a circle with that radius.
cout << "Task 4: Circle"<<endl;
float r;
cout<<"Enter radius: " ;
cin>>r;
float pr = 2; // pixel ratio
for (int i = -r; i <= r; i++)
{
for (int j = -r; j <= r; j++)
{
float d = ((i*pr)/r)*((i*pr)/r) + (j/r)*(j/r);
if (d >0.95 && d<1.08)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
} return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main()
{
// Task 1 : Draw a w-wide h-high rectangular frame, using asterisks.
int width, height, length;
int flag=0;
int ch;
do
{
// PROMPT MENU WHICH IS EXIT WHEN CHOICE=5
cout<<"Press 1 for Rectangle:"<<endl;
cout<<"Press 2 for Triangle (North to West):"<<endl;
cout<<"Press 3 for Triangle (South to East):"<<endl;
cout<<"Press 4 for Circle:"<<endl;
cout<<"Press 5 to end this program:"<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<< "Task 1: Rectangular Frame"<<endl;
cout<< "Enter width: ";
cin>>width;
cout<< "Enter height: "; cin>>height;
for(int i = 0; i < height; ++i)
{
for(int j = 0; j < width; ++j)
{
if(i == 0 || j == 0 || i == height - 1 || j == width - 1)
{
cout << "*";
}
else{
cout << " ";
}
}
cout << endl;
}
cout << endl;
break;
case 2:
// Task 2: Draw the triangle constituting the northwestern half of a square, given the side length.
cout<< "Task 2 North to West"<<endl;
cout<< "Enter length: ";
cin>>length;
for(int i=length; i >= 1; i--)
{
for(int j=1; j<=i; j++)
{
cout<< "*";
}
cout<< " ";
}
cout<<endl;
break;
case 3:
// Task 3: Draw the triangle constituting the southeastern half of a square, given the side length.
cout<< "Task 3: South to East"<<endl;
cout<< "Enter length: ";
cin>>length;
for(int i=length; i >= 1; i--)
{
for(int j=1; j <=i; j++)
{
cout<< " ";
}
for(int k=length;k>=i;k--)
{
cout << "*";
}
cout<<" ";
}
cout<<endl;
break;
case 4:
{
// Task: 4 Given a radius, draw a circle with that radius.
cout << "Task 4: Circle"<<endl;
float r;
cout<<"Enter radius: " ;
cin>>r;
float pr = 2; // pixel ratio
for (int i = -r; i <= r; i++)
{
for (int j = -r; j <= r; j++)
{
float d = ((i*pr)/r)*((i*pr)/r) + (j/r)*(j/r);
if (d >0.95 && d<1.08)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
} break;
}
//return 0; break;
//default: cout<<"You Enter Wrong Choice.."<<endl;
case 5:
exit(0); break;
default: cout<<"You Enter Wrong Choice! Please Enter(1,2,3,4,5) only.."<<endl;
}
} while(flag==0);
return 0;
}