CIS22A Homework 8
Spring 2022

Topic:
A function with parameters passed by value and passed by reference (Chapter 6.13, Lecture 6/6/22)
Do not use anything beyond Chapter 6 materials.
Do not use C++ pointers (this is not a topic of this course).

Write a program to read the base and the hypotenuse of an isosceles triangle and calculate the height and the area of the triangle.

In the picture above a is the base, b is the hypotenuse, and h is the height.

The formulas are:




where A is the area.
https://mathworld.wolfram.com/IsoscelesTriangle.html

Write a single function isoscelesTriangleHeightAndArea (base, hypotenuse, height, area) that calculates both the height and area of an isosceles triangle given its base length and hypotenuse length.

There must be no other function in the program beside isoscelesTriangleHeightAndArea() and main()


The function isoscelesTriangleHeightAndArea (base, hypotenuse, height, area) has EXACTLY FOUR PARAMETERS.
-Input parameters to the functions are the base and hypotenuse of the triangle. Function cannot modify the input parameters.
-Output parameters of the function are the area and height of the triangle. Function must calculate and modify these 2 parameters.
-Function must have a boolean return (success or failure) to indicate whether the calculation is successful or not.
-This function returns true if both base and hypotenuse are not negative (0 base is valid).
There is also a validation rule that the following expression cannot be negative to avoid calculation of square root of a negative number.

If validation fails then the function returns false and skips all calculations.

-The function isoscelesTriangleHeightAndArea() should Not use any "cin" or "cout". Only the main() function will contain "cin" and "cout".
-The main() function calls the function in a loop until the user enters 0 0. These are the "Sentinel" values that stop the loop (chapter 5). If only one input is 0 and the other input is positive, the loop will keep going.
-Use variable name base and hypotenus, not a and b.

The following is a basic simple code without loop to test your function:
#include
#include
// Function prototype
// —-----------
int main()
{
double base = 2.5, hypotenuse = 3, height, area;
bool success = isoscelesTriangleHeightAndArea(base, hypotenuse, height, area);
if (success)
{
cout << "Triangle height is " << height << endl;
cout << "Triangle area is " << area << endl;
}
else
cout << "Invalid Input!" << endl;

// Expected output is:
// Triangle height is 2.72718
// Triangle area is 3.40897
}

Output is:
Triangle height is 2.72718
Triangle area is 3.40897

You can use this web calculator here to verify results:
https://keisan.casio.com/exec/system/1273850202


It is not required that you have to copy and paste the above code to your code for submission. But the code gives you an excellent guide of how to write the function prototype so that the test code above works correctly.

If your function code does not work with the above given main() code, your score will be very low because the function is incorrect.

The result of the function call is used in main() to decide what to display for the output: if the return value is true then it means the calculation result is stored in area and height, and if false it means that the input parameters base and hypotenuse are invalid.

After using the simple test code above, write code in main() to accept multiple base and hypotenuse inputs and display the results by writing a Sentinel loop (chapter 5, lecture 5/23/22) that runs until the user enters 0 and 0 for the 2 inputs.

Note:
Do not use an infinite loop as a sentinel loop.
Do not use "break" statements.
A good sentinel loop does not use an extra "if" statement inside loop to check for the sentinel value 0 and 0.
If the sentinel loop is correct, the function is NOT called when input is 0 and 0 because these values will make the loop stop.
There is no need to control the number of decimal digits in the results
The output text and results should be aligned nicely as seen in the following samples.

Write the function prototype before main() and write the full function definition after main().

Output samples

Respuesta :

The program is an illustration of C++ functions;

Functions are program statements that are executed when evoked

The C++ program

The program in C++, where comments are used to explain each action is as follows:

#include <iostream>

#include <cmath>

using namespace std;

//This defines the boolean method

bool isoscelesTriangleHeightAndArea(double base, double hypotenuse, double height, double area) {

   //This checks for invalid base and height

   if(base < 0 || hypotenuse < 0){

       return false;

   }

   //This returns true if base and height are valid

   return true;

}

//The main begins here

int main(){

   //This initializes the variables

   double base, hypotenuse, height, area;

   //This gets input for base and hypotenuse

   cout<<"Base: ";    cin>>base;

   cout<<"Hypotenuse: "; cin>>hypotenuse;

   //The calls the boolean method

   bool success = isoscelesTriangleHeightAndArea(base, hypotenuse, height, area);

   //If success is True

   while (success){

       //This calculates height

       height = sqrt(hypotenuse*hypotenuse - (base/2) * (base/2));

       //This calculates area

       area = 0.5 * base * height;

       //This prints height

       cout << "Triangle height is " << height << endl;

       //This prints area

       cout << "Triangle area is " << area << endl;

       //This gets input for base and hypotenuse

       cout<<"Base: ";    cin>>base;

       cout<<"Hypotenuse: "; cin>>hypotenuse;

       bool success = isoscelesTriangleHeightAndArea(base, hypotenuse, height, area);

   }

   cout << "Invalid Input!" << endl;

}

Read more about C++ programs at:

https://brainly.com/question/24833629

#SPJ1