CS201 Assignment Solution 2016
solution
#include <iostream>
using namespace std;
struct Rectangle
{
float width,length,area;
float rectangleArea(float ,float)
{
area=width*length;
return area;
}
};
struct Trapezoid
{
float height,base1,base2,area;
float trapezoidArea(float ,float, float)
{
area=((base1+base2)/2)*height;
return area;
}
};
int main()
{
char repeat;
int choice;
do
cout<<"\n\nPress 1 to calculate the area of Rectangle"<<endl;
cout<<"Press 2 to calculate the area of Trapezoid\n"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
if(choice==1)
Rectangle object;
cout<<"\nEnter the width of rectangle: ";
cin>>object.width;
cout<<"Enter the length of rectangle: ";
cin>>object.length;
object.rectangleArea(object.width , object.length);
cout<<"The Area of Rectangle is: "<<object.area;
}
else if(choice==2)
{
Trapezoid object;
cout<<"\nEnter the base1 of trapezoid: ";
cin>>object.base1;
cout<<"Enter the base2 of trapezoid: ";
cin>>object.base2;
cout<<"Enter the height of trapezoid: ";
cin>>object.height;
object.trapezoidArea(object.base1 , object.base2 , object.height);
cout<<"The Area of Trapezoid is: "<<object.area;
}
else
{
cout<<"Invalid Choice."<<endl;
}
cout<<"\n\nDo you want to do another calculation? (y/n): ";
cin>>repeat;
}
while (repeat=='y'||repeat=='Y');
system("pause");
}

No comments:
Post a Comment