C Program for calculating the hypotenuse

I am new to coding and am currently learning C. In class I was given the task of writing a program that calculates the hypotenuse of a triangle using our own functions. However, it looks like there is something wrong with the code I wrote.

#include <stdio.h>
#include <math.h>

double hypotenuse(double x, double y, double z);

int main(void) {
    double side1, side2, side3, counter;

    side3 = 1;

    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf_s("%d %d", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2, side3));
    }

    return 0;
}

double hypotenuse(double x, double y, double z) {
    x *= x;
    y *= y;
    z = sqrt(x + y);

    return z;
}

      

My instructor said that we are allowed to use the square root function of the math library. The main errors I encountered:

1) "side3" is undefined (which is why I just arbitrarily set it to 1, but is there any other way to prevent this error?)
2) If I, for example, entered 3 and 4 as side1 and side2, then side3 should be 5. However, the printed result is an absurdly long number.

Thanks for the help! Any words of advice are appreciated.

+3


source to share


5 answers


You don't need the side3 variable - it is not used in the calculation. And the function hypotenuse

returns the result, so you can output the result directly sqrt

.



+3


source


I am using Ubuntu Linux and write it like this. Please see if you like this.

#include <stdio.h>
#include <math.h>

double hypotenuse(double x, double y) {
    double z = sqrt(x * x + y * y);
    return z;
}

int main(void) {
    double b1, b2, counter;
    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &b1, &b2);
        printf("%.2f\n", hypotenuse(b1, b2));
    }
    return 0;
}

      



Test

$ ./a.out 
Enter values for two sides: 1 1.73
2.00

      

+1


source


As a reference for anyone considering this:

You don't need to write your own function. The C standard provides functions for calculating a hypothesis :

7.12.7.3 Functions hypot

Summary

#include <math.h>
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);

      

Note that you will most likely need to link to -lm

, although this is not explicitly stated in the C functions documentation, but the latest POSIX documentation . It may be documented in other standards.

(The reference to the C11 standard [draft] is likely to be much more durable.)

0


source


OP's code has some problems:

Main problem: the code should have generated a compiler warning because it is scanf()

directed to be treated &side1

as int *

. Include all compiler warnings to save time. The code used "%d"

instead of mapping "%lf"

to read a double

. You also need to check the return value to validate your input.

double side1, side2, side3, counter;
...
// scanf_s("%d %d", &side1, &side2);
if (scanf_s("%lf %lf", &side1, &side2) != 2) puts("Input error");

      


size3

not required. Call hypotenuse()

with two arguments. @ghostprgmr

// printf("%.2f\n", hypotenuse(side1, side2, side3));
printf("%.2f\n", hypotenuse(side1, side2));

// double hypotenuse(double x, double y, double z) {
double hypotenuse(double x, double y) {
   double z = ...

      


Minor: Used in code "%.2f"

to print the value of the hypotenuse. It might be OK with the choice of input values ​​for the OP's code, but in general it's a poor choice. If the input values ​​change small, such as 0.001 and 0.002, the output will print a rounded value of 0.00. For very large values, the result will show many unimportant numbers, like the OP found with 130899030500194208680850288727868915862901750748094271410143‌​232.00

.

For development and debugging, consider using "%e"

, "%g"

or "%a"

to see the appropriate double

.


Note that it is x * x + y * y

prone to overflow / underflow even if mathematically sqrt(x * x + y * y)

in range double

. This is one of the benefits of the standard function hypot(x,y)

as it usually handles these edge cases well.

0


source


Use correct format specifiers! The format specifier for double is not% d! Rest is fine.

#include <stdio.h>
#include <math.h>

double hypotenuse(double x, double y, double z);

int main(void) {
    double side1, side2, side3, counter;

    side3 = 1;

    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2, side3));
    }

    return 0;
}

double hypotenuse(double x, double y, double z) {
    x *= x;
    y *= y;
    z = sqrt(x + y);

    return z;
}

      

Also you can change it like this:

#include <stdio.h>
#include <math.h>

double hypotenuse(double x, double y);

int main(void) {
    double side1, side2, counter;



    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2));
    }

    return 0;
}

double hypotenuse(double x, double y) {
    x *= x;
    y *= y;
    return sqrt(x + y);


}

      

0


source







All Articles