Cmpsc201 Lab Assignment #6 Write a program to ask the user to enter the width an
ID: 3540204 • Letter: C
Question
Cmpsc201 Lab Assignment #6
Write a program to ask the user to enter the width and length of a rectangle, calculate the area
and perimeter of the rectangle, and then display the results. The main function of this
program should call the following functions:
getLength %u2013 this function should ask the user to enter the rectangle%u2019s length, and return that
values as a double. This function should also implement input validation that only accepts
positive length value from the user.
getWidth %u2013 this function should ask the user to enter the rectangle%u2019s width, and return that
values as a double. This function should also implement input validation that only accepts
positive width value from the user.
getArea %u2013 this function should accept the rectangle%u2019s length and width as parameters, and
return the rectangle%u2019s area.
getPerimeter %u2013 this function should accept the rectangle%u2019s length and width as parameters,
and return the rectangle%u2019s perimeter.
displayData %u2013 this function should accept the rectangle%u2019s length, width, area, and perimeter as
parameters, and then display them in an appropriate message on the screen.
Explanation / Answer
#include<stdio.h>
double getLength();
double getWidth();
double Area(double,double);
double Perimeter(double,double);
void Display(double,double,double,double);
int main()
{
double l=getLength();
double w=getWidth();
double a=Area(l,w);
double p=Perimeter(l,w);
Display(l,w,a,p);
return 0;
}
double getLength()
{
double len;
printf("Enter Length of Rectangle ");
scanf("%lf",&len);
if(len<0)
{printf("Enter Positive Length of Rectangle ");
scanf("%lf",&len);
}
return len;
}
double getWidth()
{
double wid;
printf("Enter Width of Rectangle ");
scanf("%lf",&wid);
if(wid<0)
{printf("Enter Positive Width of Rectangle ");
scanf("%lf",&wid);
}
return wid;
}
double Area(double l,double w)
{
double area=l*w;
return area;
}
double Perimeter(double l,double w)
{
double p=2*(l+w);
return p;
}
void Display(double l,double w,double a,double p)
{
printf(" Length of the Rectangle:%lf ",l);
printf("Width of the Rectangle:%lf ",w);
printf("Area of the Rectangle:%lf ",a);
printf("Perimeter of the Rectangle:%lf ",p);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.