Question
CIS22A Homework 11 Fall 2021 Topic: A function with parameters passed by value and passed by reference (Chapter 6.13, Lecture 11/10/21 ) Do not use
CIS22A Homework 11
Fall 2021
Topic: A function with parameters passed by value and passed by reference (Chapter 6.13,Lecture 11/10/21)
Do not use Chapter 7 materials.
Write a single functionrectangleAreaPerim(width, height, area, perimeter) that calculates both the area and the perimeter of a rectangle given its width and height.
-Input parameters to the functions are the width and height of the rectangle. Function cannot modify the input parameters.
-Output parameters of the functions are the area, perimeter, and a boolean return (success or failure).
-This function returns true if both width and height are not negative, and thus calculation is required. Otherwise the function returns false and skips all calculations.
-The function rectangleAreaPerim(width, height, area, perimeter) shouldNot use any "cin" or "cout". Only the main() function will contain "cin" and "cout".
-The main() function calls the function rectangleAreaPerim() in a loop until the user enters 0 0. These are "Sentinel" values that stop the loop (chapter 5). If only one input is 0, the loop keeps going.
This is a basic simple code without loop to test your function:
int main()
{
double width = 1.2, height = 2.3, area, perimeter;
bool success = rectangleAreaPerim(width, height, area, perimeter);
if (success)
cout << "Area is " << area << ". Perimeter is " << perimeter;
else
cout << "Invalid input";
}
This does NOT mean you have to copy and paste the above code to your code for submission. It just gives you a guide of how to write thefunction prototype so that the test code above works correctly..
The result of the function is used in main() to display the correct output: true means the calculation result is stored in area and perimeter, and false means that the input parameters width, height are invalid.
After using the simple test code above, write code in main() to accept multiple width, height inputs and print results by writing aSentinel loop (chapter 5) that runs until the user enters 0 and 0 for the width and height.
Write the function prototype before main() and write the full function definition after main().
Output sample
This program calculates area and perimeter or rectangle.
Enter width and height separated by spaces, or enter 0 0 to stop: 1.2 3.4
Rectangle area is 4.08
Rectangle perimeter is 9.2
Enter width and height separated by spaces, or enter 0 0 to stop: 0 3.4
Rectangle area is 0
Rectangle perimeter is 6.8
Enter width and height separated by spaces, or enter 0 0 to stop: -1.2 3.4
Invalid input
Enter width and height separated by spaces, or enter 0 0 to stop: 1.2 -3.4
Invalid input
Enter width and height separated by spaces, or enter 0 0 to stop: -1.2 -3.4
Invalid input
Enter width and height separated by spaces, or enter 0 0 to stop: 0 0
Goodbye
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started