Question

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

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

1. 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.

2. 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.

3. 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

call function to determine area

call function to determine perimeter

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

I do not have the rectangles I used for my first assignment, I just used "cin >>" to allow the user to input different lengths and widths each time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<iostream>

using namespace std;

double perimeter(double width, double length){

return 2*(width+length);

}

double area(double width, double length){

return width*length;

}

void print(double width, double length, double area, double perimeter){

cout<<"Width: "<<width<<endl;

cout<<"Length: "<<length<<endl;

cout<<"Area: "<<area<<endl;

cout<<"Perimeter: "<<perimeter<<endl;

}

int main(){

double length, width, a, p;

for(int i=0; i<3; i++){

cout<<"Enter width and length: ";

cin>>width>>length;

a = area(width, length);

p = perimeter(width, length);

print(width, length, a, p);

cout<<endl;

}

return 0;

}

nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/october/10102018$ g++ rectangle.cpp nagarajuanagaraju-Vostro-3550:~/Desktop/CHEGG/october/10102018$./a.out Enter width and length: 5.0 3.0 Width: 5 Length: 3 Area: 15 Perimeter: 16 Enter width and length: 4.2 2.5 Width: 4.2 Length: 2.5 Area: 10.5 Perimeter: 13.4 Enter width and length: 5.8 2.5 Width: 5.8 Length: 2.5 Area: 14.5 Perimeter: 16.6

Add a comment
Know the answer?
Add Answer to:
Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • this assignment should be in C++ In this assignment, you will implement two classes. Point: A...

    this assignment should be in C++ In this assignment, you will implement two classes. Point: A Point in a two dimensional plane has an integer x coordinate value and an integer y coordinate value. Rectangle: A Rectangle is a class that has three attributes: 1) Point type data that represent the top-left point of the rectangle, 2) integer length and 3) integer width. Write the appropriate class definition for Point class and Rectangle class with necessary constructors, mutator and accessor...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Complete the Rectangle Program Using the code given in rectangle.cpp, implement the functions that are called...

    Complete the Rectangle Program Using the code given in rectangle.cpp, implement the functions that are called in the main function to complete the program. You must not change any of the existing code in the file. You can only add functions and comments to the code. Here’s a sample run of how the program should behave: Enter rectangle length: -1 ← invalid value, ask user to re-enter Enter rectangle length: -2 ← invalid value Enter rectangle length: 0 ← invalid...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT