www.vustudents.ning.com

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

# CS201 Assignment No 2 Due Date 28-06-2016

Assignment 2 of CS201 is continuity of assignment 1. You have to do the same task as you did in assignment 1 but this time you have to use structure variables. You have to write C++ program in which;

• You are required to define 2 structures named as Rectangle and Trapezoid.
• Rectangle has 3 data members, width, length and area
• Trapezoid has 4 data members named as height, base1, base2 and area.
• Provide 2 options to the user as below:
 Enter 1 to calculate the area of Rectangle
 Enter 2 to calculate the area of a Trapezoid

• If the user enters 1, then
 Object (variable) of Rectangle structure is created
 The program should ask the user to enter the width and length of the rectangle
 Then show the result after calculation. Formula for calculating Area of rectangle is
Area = width x Length.

• If the user enters 2, then
 Object (variable) of Trapezoid structure is created
 The program should ask the user to enter the base1, base2, and height of the trapezoid
 Then shows the result after calculation. Formula for calculating Area of trapezoid is
Area =
• You must implement two different functions for calculating areas of rectangle and trapezoid.
• After taking the choice from the user in form of 1 or 2 and assigning entered values to respective structure data members, the relevant function should be called to calculate area.
• Function for calculating area should return a value that would be stored in data member of structure to store respective area.
• After storing area, it should be displayed on screen.
• After showing the output to the user, you need to ask the user if he/she wants to do another calculation. If the user presses y or Y, then the program should ask the user again to enter the choice of shape otherwise the program should be terminated.

Screenshot of the program execution is given below for both shapes:

Views: 2767

### Replies to This Discussion

there is an error

# CS201 Assignment 2 only correct solution

CS201 assignment2 Correct solution, remove my comments add your's, change variable names, enjoy!! By Aamir

Attachments:

By Nazish

#include <iostream>
using namespace std;
double AreaofRectangle ();
double AreaofTrapezoid ();
double rectangleArea(double w,double l){
double rectArea;
rectArea = w* 1;
return rectArea;
}
double trapezoidArea(double b1,double b2,double h){
double trapeArea;
trapeArea + ((b1/b2)/2)*h;
return trapeArea;
}
int main(){
int choice1;
char choice2;
double width,length,rectArea,base1,base2,height,trapeArea;
do{
cout"\n\nEnter 1 to calculate the area of Rectangle"endl;
cout"Enter 2 to calculate the area of trapezoid\n\n";
cin>>choice1;
if(choice1==1){
cout"Enter the width of rectangle : ";
cin>>width;
cout"Enter the length of rectangle : ";
cin>>length;
rectArea = rectangleArea(width,length);
cout"The Area of Rectangle is : "rectAreaendl;
}
else if (choice1==2){
cout"Enter base1 of trapezoid : ";
cin>>base1;
cout"Enter base2 of trapezoid : ";
cin>>base2;
cout"Enter the height of trapezoid : ";
cin>>height;
trapeArea = trapezoidArea(base1,base2,height);
cout"The Area of trapezoid is : "trapeAreaendl;
}
else{
cout"Wrong Input";
}
cout"\nDo you want to do another calculation? : ";
cin>>choice2;
}while(choice2=='y' || choice2=='Y');
system("pause");
}

It has been observed that in assignment 2, some students are confused regarding passing the user’s entered values to the functions. Mostly students have confusion whether to pass the values through the ordinary variables or through the structure variables.

You are required to pass the arguments to the function in form of Structure Variables otherwise there will be deduction of marks while grading your assignment solution.

#include
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"<"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: "<"\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: "<"invalid="" choice."endl;="" cout"\n\ndo="" you="" want="" to="" do="" another="" calculation?="" (y="" n):="" ";="" cin="">>repeat;
}
while (repeat=='y'||repeat=='Y');
system("pause");
}

cout, cin and endl are defined in the namespace std (using namespace is a command while std is a library)

Structure and its sample programs have been defined in Lecture no. 20.

Here is also Solution of Cs201 Assignment 2

It is far different code from the above solution.

Attachments:

another codding

#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;
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");
}

hi

Kindly ap mjhe bta skty hain kaisay assignment bnani hai please meine abi open kiya or aaj last date hai please ap maray name or roll nmbr add kar dijye and mjhe file send kar dijiye please

1

2

3

4

5

## VIP Member Badge & Others

------------------------------------

## Latest Activity

3 hours ago

### ENG101 Assignment 2 Solution Spring 2021 || ENG101 Assignment No 2 || 100% Correct Solution

3 hours ago
+ ! ! ! ! ! !★ "Mannat"★✓ liked Ziddi Queen's discussion غور طلب بات
3 hours ago
3 hours ago
Ibn Azeem liked Mani Siddiqui's discussion Android hacking / Mobile Hacking
5 hours ago
Ibn Azeem liked Meera ch's discussion hint
5 hours ago
Ibn Azeem liked ツβµŕɨeď€ʍʍÖツ's discussion اختیار کے استعمال کا حق تصرّف۔۔۔
5 hours ago
5 hours ago