Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

how can the code be rewritten given so that it uses a called function that takes one argument, the number of the side you are

how can the code be rewritten given so that it uses a called function that takes one argument, the number of the side you are asking the user to enter, the function will prompt for the side length (display the side number in your prompt), read the side length, let them re-enter if the side length is not greater than zero, and return a valid side length to the calling code using the "return mechanism".

/* A program that gets the lengths of three sides of a triangle from

the user and calculates and displays the area of the triangle along

with its side lengths.

*/

#include

#include

using namespace std;

void getSideLengths(double& side1, double& side2, double & side3);

double findTriArea(double side1, double side2, double side3);

int main()

{

double triSide1, triSide2, triSide3, area;

// Get valid side lengths

getSideLengths(triSide1, triSide2, triSide3);

// Calculate the area from the side lengths

area = findTriArea(triSide1, triSide2, triSide3);

cout << " A triangle with side lengths of " << triSide1 << ", " << triSide2 << " and "

<< triSide3 << " units \thas an area of " << area << " square units. ";

return 0;

}

/* A function that gets valid side lengths for a triangle and places them in the arguments.

Params:

side1 - A reference to the double used to store a side length

side2 - A reference to the double used to store a side length

side3 - A reference to the double used to store a side length

Returns: See reference parameters.

*/

void getSideLengths(double& side1, double& side2, double & side3)

{

cout << "Enter the length of side 1: ";

cin >> side1;

while(side1 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 1: ";

cin >> side1;

}

cout << "Enter the length of side 2: ";

cin >> side2;

while(side2 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 2: ";

cin >> side2;

}

cout << "Enter the length of side 3: ";

cin >> side3;

while(side3 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 3: ";

cin >> side3;

}

while (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1)

{

cout << " Error, the lengths entered do not form a triangle. ";

cout << "Enter the length of side 1: ";

cin >> side1;

while(side1 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 1: ";

cin >> side1;

}

cout << "Enter the length of side 2: ";

cin >> side2;

while(side2 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 2: ";

cin >> side2;

}

cout << "Enter the length of side 3: ";

cin >> side3;

while(side3 <= 0)

{

cout << " Error, the length of the side must be greater than 0 ";

cout << "Enter the length of side 3: ";

cin >> side3;

}

}

return;

}

/* A function that calculates the area of a circle given the lengths of

all of the sides.

Params:

side1 - A double that is the length of side 1

side2 - A double that is the length of side 2

side3 - A double that is the length of side 3

Returns: A double that is the area

*/

double findTriArea(double side1, double side2, double side3)

{

// Calculate the half permimeter

double halfPerimeter = (side1 + side2 + side3) / 2.0;

//cout << "***The calculated half perimeter is " << halfPerimeter << endl; // Debug statement - take out

// Using Heron's Formula to calculate area

double area = sqrt(halfPerimeter * (halfPerimeter - side1) * (halfPerimeter - side2) *

(halfPerimeter - side3));

//cout << "***The calculated area is " << area << endl; // Debug statement - take out

return area;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

,nglish Law of Contract (.JUS5260 / JUR126

Answered: 1 week ago