Calculating Hypotenuse with C++ Code

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Code Triangle
Click For Summary

Discussion Overview

The discussion revolves around writing C++ code to calculate the hypotenuse of a triangle using the Pythagorean theorem. Participants explore issues related to runtime errors, memory management, and code efficiency.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports a runtime error when attempting to calculate the hypotenuse and seeks assistance.
  • Another participant notes that the main function should return a value and questions the nature of the runtime error.
  • A participant later resolves their issue by initializing a pointer to a new double.
  • Another participant suggests an alternative approach by using a local variable instead of dynamic memory allocation, which they claim works correctly.
  • One participant demonstrates a method that allocates memory for a double but acknowledges that deleting it is unnecessary since it is not an array.
  • Another participant points out that the deletion of the pointer is not needed and suggests a simpler approach without dynamic memory allocation.

Areas of Agreement / Disagreement

Participants present multiple approaches to solving the problem, with some favoring dynamic memory allocation while others advocate for simpler, stack-based solutions. No consensus is reached on the best method.

Contextual Notes

Participants discuss different memory management techniques and their implications, but there is no resolution on the necessity of dynamic allocation versus stack allocation.

Who May Find This Useful

Readers interested in C++ programming, particularly in memory management and mathematical computations, may find this discussion relevant.

magnifik
Messages
350
Reaction score
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
I don't see anything obvious, although your main should return a value. What's the run-time error?
 
nvm, figured it out. i just had to initialize p to new double.
 
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.
 
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
}
 
Well, that's one way. At the end you can just delete p since it's not an array.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K