#include <cstdlib> #include <iostream> #include <cmath> using namespace std; /*
ID: 3720838 • Letter: #
Question
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
/*
This program reads a width and height of a rectangle. It computes and
prints the area, perimeter and length of the diagonal of the rectangle.
Written by: Your name here
Date written: Current date here
*/
void compute_rectangle (double width, double height);
int main()
{
double width, // width of the rectangle entered by the user
height, // height of the rectangle entered by the user
area, // area of the given rectangle
perimeter, // perimeter of the given rectangle
diagonal; // diagonal of the given rectangle
cout << "Enter the width of the rectangle: ";
cin >> width;
cout << "Enter the height of the rectangle: ";
cin >> height;
compute_rectangle (width, height);
cout << " The area is: " << area;
cout << " The perimeter is: " << perimeter;
cout << " The diagonal is: " << diagonal << " ";
return 0;
}
void compute_rectangle (double width, double height)
{
double area, perimeter, diagonal;
area = width * height;
perimeter = width * 2 + height * 2;
diagonal = sqrt (width * width + height * height);
}
Then add call by reference parameters to send back the values of area, perimeter and diagonal to main. Change the prototype, function call and function definition to get the program to compile and run correctly.
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
/*
This program reads a width and height of a rectangle. It computes and
prints the area, perimeter and length of the diagonal of the rectangle.
Written by: Your name here
Date written: Current date here
*/
void compute_rectangle (double width, double height, double &area,double &perimeter, double &diagonal);
int main()
{
double width, // width of the rectangle entered by the user
height, // height of the rectangle entered by the user
area, // area of the given rectangle
perimeter, // perimeter of the given rectangle
diagonal; // diagonal of the given rectangle
cout << "Enter the width of the rectangle: ";
cin >> width;
cout << "Enter the height of the rectangle: ";
cin >> height;
compute_rectangle (width, height, area, perimeter, diagonal);
cout << " The area is: " << area;
cout << " The perimeter is: " << perimeter;
cout << " The diagonal is: " << diagonal << " ";
return 0;
}
void compute_rectangle (double width, double height, double &area,double &perimeter, double &diagonal)
{
area = width * height;
perimeter = width * 2 + height * 2;
diagonal = sqrt (width * width + height * height);
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.