Using functions for calculations

I have an assignment that is written below. I'm going to put the code I currently wrote in this post so it's like a reference point. I have also written some comments in my code at points where I wasn't sure what to do. I'll put my questions in the next post.

The area of an arbitrary triangle can be computed using the formula

area =√(s(s - a)(s - b)(s - c))

where a, b, and c are the lengths of the sides, and s is the semiperimeter.

s = (a + b + c)/2

Write a void function that computes the area and perimeter (not the semiperimeter) of a triangle based on the length of the sides. The function should use five parameters--three value parameters that provide the lengths of the edges and two reference parameters that store the computed area and perimeter.

Write a second bool function that checks to see if a, b, and c corresponds to a correctly formed triangle. Note that not all combinations of a, b, and c produce a triangle. After the user enters a, b, and c, call the bool function. If the bool function returns true, then and only then should the void function above be called to calculate the area. If the bool function returns false, then your program should output an error message about an incorrectly formed triangle.

Write a third void function that does the user input for a, b, and c. This function should robustly confirm that a, b, and c are positive. Leave the checking as to whether a, b, and c correspond to a correctly-formed triangle for the bool function above.

Your program should produce correct results for legal data and reasonable results for illegal combinations.


EXTRA CREDIT - Write a fourth function to calculate the semiperimeter, which will be called by the function that calculates the area.

Here is my current code with some questions as comments thrown in. Sorry for formatting issues in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>		// Used for cin and cout
#include <cmath>		// Used for square root function
using namespace std;

void description();
//Postcondition: Description of the program is printed to the screen.

void get_input(double side_a, double side_b, double side_c);
//Precondition: User enters correct values.
//Postcondition: The length of sides a, b, and c of a triangle
//are assigned to variables side_a, side_b, and side_c

bool check_triangle(double side_a, double side_b, double side_c);
//Precondition: Side lengths a, b, and c are positive nonzero numbers.
//Postcondition: If the side lengths a, b, c can correctly form a triangle
//a function to calculate the semipeerimieter, perimeter, and area will be called.

double semiperimter(double side_a, double side_b, double side_c);
//Precondition: Side lengths a, b, and c are positive nonzero numbers. 
//Postcondition: Returns the value of the semiperimeter of the triangle.

void area_perimeter(double side_a, double side_b, double side_c, double perimeter, double area);
//Precondition: Side lengths a, b, and c are positive nonzero numbers.  
//Postcondition: Returns the perimeter and area of a triangle. 

void show_output()	//Write this function later on


	int main ()
{
	char answer;

	do
	{
		description();
		get_input(side_a, side_b, side_c);
		check_triangle(side_a, side_b, side_c);
		//I want the area_perimeter function to be called here is check_triangle returns true
		//I assume I should should just write an output function that prints the perimeter and area to the screen
	}
	while (answer == 'y' || answer == 'Y');

	return 0;
}

void description()	
{
  cout << "This program calculates the perimeter and area of a triangle";
       << "based on inputed values of three side lengths.\n";
}

void get_input(double side_a, double side_b, double side_c)		//Should I ask the user to input values in 
{																//ascending order and put the hypotenuse as the 3rd value?
  cout << "Input the lengths of three sides of a triangle: ";
  cin >> side_a;
  while (side_a <= 0)
  {
	  cout << "Enter a positive nonzero number for the length of side a";
	  cin >> side_a;
  }
  cout << ",";
  cin >> side_b;
  while (side_b <= 0)
  {
	  cout << "Enter a positive nonzero number for the length of side b";
	  cin >> side_b;
  }
  cout << ",";
  cin side_c;
  while (side_c <= 0)
  {
	  cout << "Enter a positive nonzero number for the length of side c";
	  cin >> side_c;
  }
  cout << endl;
}
  
bool check_triangle(double side_a, double side_b, double side_c)
{
	if (side_a + side_b >= side_c)	//I would like a way to take any sides lengths and reorder them
		return true;				//so that I can always check if the sum of two sides are greater  
	else							//or equal to the longest side
		return false;

		cout << "Those side lengths cannot produce a triangle.\n";
		cout << "Make sure 
	//If the boolean function is true I want a function to calculate area to be called.
	//If the boolean function is false I want an error message to be printed to the screen.
	//How do I do that?

double semiperimter(double side_a, double side_b, double side_c)
		{
			double s;

			s = (side_a + side_b + side_c)/2.0	//Used 2.0 just in case all side lengths are integers.
			return s;							//If pass by reference is used does that also pass the data type?
		}

void area_perimeter(double side_a, double side_b, double side_c, double& perimeter, double& area);
{
	double perimeter, area;

	perimeter = side_a + side_b + side_c
	area = sqrt ( semiperimter(side_a, side_b, side_c) * (semiperimter(side_a, side_b, side_c) - side_a) 
	* (semiperimter(side_a, side_b, side_c) - side_b) * (semiperimter(side_a, side_b, side_c) -side_c) ) 
}
//The calculation above is too long. I'm not sure what the professor wants. Would it be best to define 
// the semiperimeter function as double s(double a, doubleb, double c) so its s(a,b,c) when called?
// I've looked online and read a little about pointers but the professor doesn't want us to use anything we 
//haven't learned in class. 
Last edited on
I think I would have wrote the ode a little differently but I have to stick to the parameters the professor outlined.

My questions are basically comments in the code. First issue is that I want to reorder the side lengths in the bool function so that the sum of the two shortest lengths are compared to the value of the longest side. Should I use the squares of the numbers instead or will both methods work every time? I think the other option would be to ask the user to put the numbers in ascending order with the hypotenuse as the last value (Like I mentioned in the code).

The next issue is how do I call the area_perimeter function if the boolean function is true? Do I put the boolean function in an if statement?

The last issue is the calculation of the area. I want to call the semiperimeter function to calculate the area. Since I have to use a void function would I just write the cout statements in the void function? For calculations such as (s-a), should I assign the value to a character and use the character in the equation so that the equation in shorter? Also I'm not sure what is meant by "two reference parameters that store the computed area and perimeter" in the project description.
for the last part since it was the easiest fix
the calculation is too long because you are calling smeiperimiter way more times then needed. if you did something like
double semiPer=semiperimiter(side_a, side_b, side_c);
area = sqrt(semiPer* (semiPer - side_a)*(semiPer - side_b)*(semiPer- side_c));

i think your professor has a problem with you calling the same function 4 times to get the same info back.
ill check the other two issues and get back to you in a min

just to clarify does c have to be the hypotenuse? is it required that the numbers be entered in that order? because you could always use swap logic to sort them and put them in the order you wanted.

and as for the area function being void you can pass it a reference to a variable in main like sumNum& for example and it can change the value of that variable in main so you wouldn't have to return anything.

for your reordering of the nums
note i just took this from one of my prog and quickly modified it so it might need a bit of tweeking for your use but it will put the array in ascending order then assign them to side a,b and c

else{
int ssf;
double array[3] = {side_a,side_b,side_c};
double holder;
for(int top = 0; top< 3; top++){
ssf = top;
for(int ptr = top; ptr < 3; ptr++){
if(array[ptr] < array[ssf]){
ssf = ptr;
}
}
holder = array[ssf];
array[ssf] = array[top];
array[top] = holder;
}
side_a=array[0];
side_b=array[1];
side_c=array[2];
}
Last edited on
Sorry I didn't get back to you earlier. I was reading some mechanical properties stuff.

for the last part since it was the easiest fix
the calculation is too long because you are calling smeiperimiter way more times then needed. if you did something like
double semiPer=semiperimiter(side_a, side_b, side_c);
area = sqrt(semiPer* (semiPer - side_a)*(semiPer - side_b)*(semiPer- side_c));


Thanks. I don't know why I didn't think of that. I just did an assignment last week week where I did just want you said. I guess I gotta think of all the possibilities I know next time.

i think your professor has a problem with you calling the same function 4 times to get the same info back.


So putting the semiperimter calculation in a, I guess you can say, "holding variable" prevents that?

just to clarify does c have to be the hypotenuse? is it required that the numbers be entered in that order? because you could always use swap logic to sort them and put them in the order you wanted.


side c doesn't have to be the hypotenuse. Thinking about it, saying a side has to be an hypotenuse restricts the triangles to right triangles so that's no good. I never heard of swap logic. The professor doesn't want us to use things we haven't covered in class. I'd rather just learn an efficient way to do something though. College is all about minutia not too much about learning.

and as for the area function being void you can pass it a reference to a variable in main like sumNum& for example and it can change the value of that variable in main so you wouldn't have to return anything.


So with the pass by reference variables declared in main I could use them with function that just prints those values to the screen?

Is using an array the best method for reordering the numbers? My goal is to make it so numbers can be typed in any order and a bool function can check if it is possible to make a triangle. That would return a true value and then the area function would be called.
the pass by reference variable would be similar to this

in prototyping the function
void someFunction(int &);

in main
int someNum = 12;
someFunction(someNum); <--- this passes a reference to the someNum variable to the someFunction function. That means someFunction can directly manipulate the value of someNum in main.

so using our eample above and assume someFunction adds 3 to the number passed to it.

int someNum= 12;
someFunction(someNum); <--- once this is called it alters someNum
cout << someNum; <--- prints out 15

instead of passing it a value or a copy of a value you are passing it a pointer to a position in memory that it can change. its like passing an array
Last edited on
so in my case it could be

1
2
3
4
5
6
7
void area_perimeter(double side_a, double side_b, double side_c, double& perimeter_calc, double& area_calc);
show_output (double perimeter, double area)
int main ()
{
    double perimeter, area;
    area_perimeter(side_a, side_b, side_c, perimeter, area)
    show_output (perimeter, area)


I'm thinking the perimeter_calc and area_calc variables store the calculations of each quantity in the function definition and then pass the memory location to the variables in main. The variables in main then pass the values to the value parameters of the output function and that function prints the comments and the values to the screen. Is that what would happen?


I learned to place the ampersand on the data type. Is there a difference between putting it there or at the beginning or end of the identifier?
Last edited on
yea how you have it there is right and that should work just fine i think.
My only problems that seem to be left are how to have a bool function control when another function executes and how to to reorder a set of type double numbers.

I'm thinking an if statement within the bool function that calls the other function if true and prints an error if false.

For reordering the numbers so I can check if a triangle is possible to construct I think a sorting algorithm is the best option. In my textbook I was looking at the selection sort algorithm. My professor doesn't want us to use things we haven't covered in class but I'm not sure what else I could use. I think I could use if statements but that seems tedious and inefficient.
yes you should be able to have your bool function call in the if statement.

selection sort is the code i posted earlier about sorting the data. As long as you know arrays and loops its nothing you haven't already learned its just using them in a certain way to achieve your goal.

k0t4, I thought the code you wrote above was a selection sort algorithm but I wasn't sure.

This assignment was for an intro to programming class for engineers. I asked my professor about my code and she said she doesn't know why I'm using preconditions and postconditions. She said she never taught them in class. They are wrong and the department has specific conditions and syntax for writing them. Also they are only taught to computer science majors.

She also said a I don't need a to use arrays or a sorting function. I just need if statements for the three conditions in which a triangle can be correctly made.

I'm not sure what the purpose of this class is. It seems much easier and more efficient to just learn to program outside of the class. I think this is just another class thrown into an engineering curriculum to artificially say the college provides a breadth and depth of education. All college is about is exams and deadlines. Learning is secondary and often neglected. Sorry if this became a rant. I've had a bad day so far and few exams over the next several days.
Deleted post. Found the answer.
Last edited on
Topic archived. No new replies allowed.