Calculating Hypotenuse with C++ Code

In summary, a code is being written to calculate the hypotenuse of a triangle. However, when attempting to run the code, a run-time error occurs. The cause of the error is not clear, but it is suggested to initialize the variable p to a new double. Another solution is to simply use the variable answer in the main function instead of using a pointer.
  • #1
magnifik
360
0
i am trying to write a code that calculates the hypotenuse of a triangle... when i try to run it, i get a run time error. I'm not exactly sure what the problem is.

Code:
 #include <iostream>
    #include <cmath>
    using namespace std;

    void hypotenuse(double side1, double side2, double* result)
    {
        *result = sqrt(side1*side1 + side2*side2);
    }

    int main()
    {
        double* p;
        hypotenuse(1.5, 2.0, p);
        cout << "The hypotenuse is " << *p << endl;
    }
 
Physics news on Phys.org
  • #2
I don't see anything obvious, although your main should return a value. What's the run-time error?
 
  • #3
nvm, figured it out. i just had to initialize p to new double.
 
  • #4
Something like this?
Code:
int main()
{
    double answer;
    double* p = &answer;
    hypotenuse(1.5, 2.0, p);
    cout << "The hypotenuse is " << answer << endl;
}

This works for me, and produces the correct result.
 
  • #5
i just did

Code:
int main()
{
double* p;
p = new double;
hypotenuse(1.5, 2.0, p);
cout << "The hypotenuse is " << *p << endl;
delete [] p; // i know this part is unnecessary though
}
 
  • #6
Well, that's one way. At the end you can just delete p since it's not an array.
 
  • #7
A much simpler approach is this code in main - no changes needed in hypotenuse:
Code:
double answer; 
hypotenuse(1.5, 2.0, &answer);
cout << "Hypotenuse is " << answer << endl;
return 0;

You can initialize answer, but that's not really necessary, since it will be overwritten.
 

Related to Calculating Hypotenuse with C++ Code

1. What is the formula for finding the hypotenuse of a triangle using code?

The formula for finding the hypotenuse of a triangle using code is sqrt(a^2 + b^2), where a and b are the lengths of the other two sides of the triangle.

2. How do I implement the formula in code?

You can implement the formula in code by using the built-in sqrt() function and inserting the values of a and b into the formula. For example, in Python, it would look like math.sqrt(a**2 + b**2).

3. Can I find the hypotenuse using code in any programming language?

Yes, the formula for finding the hypotenuse of a triangle is a mathematical concept and can be implemented in any programming language that has a square root function. Some examples include Python, Java, C++, and JavaScript.

4. Are there any built-in functions or libraries for finding the hypotenuse of a triangle?

Yes, many programming languages have built-in functions or libraries for calculating the hypotenuse of a triangle, such as cmath.hypot() in C++, Math.hypot() in JavaScript, and Math.sqrt() in Python.

5. Can I use the formula for finding the hypotenuse to solve for other unknowns in a triangle?

Yes, the Pythagorean theorem, which is the basis for the formula for finding the hypotenuse, can be used to solve for any unknown side or angle in a right triangle. As long as you have two known values, you can use the formula to find the third unknown value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
770
  • Engineering and Comp Sci Homework Help
Replies
8
Views
872
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
964
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top