Beruflich Dokumente
Kultur Dokumente
Problem Description:
Students at a local middle school that you have been tutoring are having some difficulty
with basic area formulas that they learn in math class. You decided to write a simple
computer program that will help them with a few of these concepts.
Program Directions:
Write a C++ program that uses the information found in the formula section to calculate
the area of a rectangle, parallelogram, or trapezoid.
Formulas
rectangle = ab
parallelogram = bh
Program Output
The program should display the following:
• The name of the user
• The area of the shape
Program Calculations
Failure to include the formulas in the program will result in a deduction of points on the
assignment.
Failure to include this information as comments at the beginning of your program will
result in a deduction of point.
Sample Run
Below is a sample run. DO NOT include additional input or output streams in your
program.
This program received 100/100 points from Dr. Cynthia
Lester.
/* Adonis Brown
Program #1
I worked on the assignment by myself and received no additional assistance.
Questions:
1. The purpose of this program is to find the area of a certain shape (using
user entered integers), then print it to the screen.
2. The inputs are the sides (and heights of certain shapes), as well as the
user's name.
3. The outputs are the area of the figure whose sides (and heights)were input,
as well as the user's name when asking for input.*/
#include <string>
#include <iostream.h>
#include <stdlib.h>
int main()
{
char shape;
string name;
double rs1 = 0, rs2 = 0, pb = 0, ph = 0, tb1 = 0, tb2 = 0, th = 0;
cout <<"Please enter your name:"<<endl;
cin>>name;
cout<<endl<<name<<", please choose one of the selections below:"<<endl
<<"------------------"<<endl;
cout<<"R = Rectangle |"<<endl<<"P = Parallellogram|"<<endl<<"T = Trapezoid
|"<<endl;
cout<<"------------------"<<endl;
cin>>shape;
if (shape == 'R'||shape == 'r')
{
cout<<"Please enter the lengths of the two sides of the rectangle: ";
cin>>rs1>>rs2;
if (rs1 <= 0 || rs2 <= 0)
cout<<"The lengths can not be negative."<<endl;
else
cout<<"The area of your rectangle is "<<(rs1*rs2)<<"."<<endl;
}
else if (shape == 'P'||shape == 'p')
{
cout<<"Please enter the length of the base of your parallellogram: ";
cin>>pb;
cout<<"Please enter the height of your parallellogram: ";
cin>>ph;
if (pb <= 0 || ph <= 0)
cout<<"The lengths can not be negative."<<endl;
else
cout<<"The area of your parallellogram is "<<(pb*ph)<<"."<<endl;
}
else if (shape == 'T'||shape == 't')
{
cout<<"Please enter the first base of your trapezoid: ";
cin>>tb1;
cout<<"And now the second base: ";
cin>>tb2;
cout<<"Finally, enter the height of your trapezoid: ";
cin>>th;
if (tb1 <= 0 || tb2 <= 0 || th <=0)
cout<<"The lengths can not be negative."<<endl;
else
cout<<"The area of your trapezoid is "<<(th/2*(tb1 + tb2))<<"."<<endl;
}
else
cout<<shape<<" is not a valid entry."<<endl;
system("PAUSE");
return 0;
}