Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Please Help ASAP Complete the code below Problem: Suppose we want to write a function that gets the side lengths of a triangle from

C++ Please Help ASAP Complete the code below

Problem:

Suppose we want to write a function that gets the side lengths of a triangle from the user

and puts the values entered in variables defined in the calling function.

The program must validate that the lengths entered are valid.

To be valid each length must be a positive value greater than 0

and the sum of any pair of side lengths must be greater than the third

To complete this exercise I want you to rewrite the code 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".

Don't forget the function header of your new function must be preceded by a comment that describes what the function does, the argument it takes, the inputs it receives from the user, and the value it returns.

Several calls to this function will replace much of the code we have in getTriangleSides currently.

Update getTriangleSides to use your function.

Name your file FL#Exercise3.cpp - where F is your first initial, L is your last initial and # is your gradebook #

Code:

/* 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

Communication; Reflective thinking)

Answered: 1 week ago

Question

Explain the chemical properties of acids with examples.

Answered: 1 week ago

Question

Write the properties of Group theory.

Answered: 1 week ago

Question

How is slaked lime powder prepared ?

Answered: 1 week ago

Question

Why does electric current flow through acid?

Answered: 1 week ago