Home
Answered Questions
Computer Science
This Is For C++ Not Java Simplifying With Functions Modify The C++ Program You Created In Assignment

This is for C++ not Java

**Simplifying with functions**

Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions:

- A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter.
- A function to return the area of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the area.
- A function to print the characteristics of a rectangle; the length of the sides, the perimeter, and the area. This function shall be passed 4 parameters as doubles; the width, the length, the perimeter, and the area of the rectangle. Name this function and all parameters appropriately. This function shall return void. Remember to display these characteristics in a visually pleasant manner.

Create the same 3 rectangles you created for assignment 1, calling the new functions. Please note that you want perform the following 3 times in sequence:

Read in the width and length of the rectangle and store them in unique variables (for each rectangle)

call function to determine area and store in unique variables (for each rectangle)

call function to determine perimeter and store in unique variables (for each rectangle)

call function to print out the rectangles characteristics (width, length, area and perimeter)

Be sure to test to ensure your program functions correctly and don't forget to include the file header and function headers for all functions.

NOTE: Do not use features that we haven't yet discussed in class.

**Expected** Output:

Your output shall look like this given the inputs below:

Enter the length of the 1st rectangle: 3

Enter the width of the 1st rectangle: 5

You created a rectangle with the following characteristics:

width = 5

length = 3

perimeter = 16

area = 15

Enter the length of the 2nd rectangle: 4

Enter the width of the 2nd rectangle: 6

You created a rectangle with the following characteristics:

width = 6

length = 4

perimeter = 20

area = 24

Enter the length of the 3rd rectangle: 4.1

Enter the width of the 3rd rectangle: 6.2

You created a rectangle with the following characteristics:

width = 6.2000

length = 4.1000

perimeter = 20.6000

area = 25.4200

Code used

#include <iostream> #include <iomanip> using namespace std; int main() { /* Rectangles */ double length = 0; double width = 0; double area = 0; double perimeter = 0; /* Square */ double sideLength = 0; double area4 = 0; double perimeter4 = 0; /* Rectangle 1 */ cout << "Enter the height of the 1st rectangle: "; cin >> length; cout << "Enter the width of the 1st rectangle: "; cin >> width; cout << "You created the first rectangle with the following characteristics: " << endl; area = length * width; perimeter = (2 * length) + (2 * width); cout << "height = " << length << endl; cout << "width = " << width << endl; cout << "perimeter = " << perimeter << endl; cout << "area = " << area << endl; cout << endl; /* Rectangle 2 */ cout << "Enter the height of the 2nd rectangle: "; cin >> length; cout << "Enter the width of the 2nd rectangle: "; cin >> width; cout << "You created the 2nd rectangle with the following characteristics:" << endl; area = length * width; perimeter = (2 * length) + (2 * width); cout << "height = " << length << endl; cout << "width = " << width << endl; cout << "perimeter = " << perimeter << endl; cout << "area = " << area << endl; cout << endl; /* Rectangle 3 */ cout << "Enter the height of the 3rd rectangle: "; cin >> length; cout << "Enter the width of the 3rd rectangle: "; cin >> width; cout << "You created the 3rd rectangle with the following characteristics:" << endl; area = length * width; perimeter = (2 * length) + (2 * width); cout << fixed << setprecision(4); // decimal place goes to 4 digits cout << "height = " << length << endl; cout << "width = " << width << endl; cout << "perimeter = " << perimeter << endl; cout << "area = " << area << endl; cout << endl; /* Square */ cout << "Enter the side length of the square: "; cin >> sideLength; cout << "You created the square with the following characteristics:" << endl; area4 = sideLength * sideLength; perimeter4 = 4 * sideLength; cout << "side length = " << sideLength << endl; cout << "perimeter = " << perimeter4 << endl; cout << "area = " << area4 << endl; cout << endl; return 0; }

#include