Sean
Sean

Reputation: 3385

C Program to Calculate Hypotenuse

I'm fairly new to coding and am currently learning C. In class I was given an assignment to write a program that calculates the hypotenuse of the triangle by using our own functions. However, there seems to be something wrong with the code that I have written.

#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're allowed to use the square root function sqrt of the math library. The main errors that I'm facing are:

1) side3 is not defined (This is why I just arbitrarily set it to 1, but is there some other way to prevent this error from happening?)
2) If I, for example, inputted 3 and 4 as side1 and side2, then side3 should be 5. However, the printed result is an absurdly long number.

Thank you for the help! Any words of advice are appreciated.

Upvotes: 4

Views: 36095

Answers (5)

chux
chux

Reputation: 153417

OP's code has some problems:

Key problem: Code should have generated a compiler warning as scanf() is directed to treat &side1 as an int *. Turn on all compiler warnings to save you time. Code used "%d" rather than the matching "%lf" to read a double. Also the return value should be checked to validate input.

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

size3 is not needed. Call hypotenuse() with 2 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: Code used "%.2f" to print the value of the hypotenuse. This may be OK with select input values to OP's code, but is a poor choice, in general. If input values are vary small like 0.001 and 0.002, the output will print rounded value of 0.00. With very large values, the result will show many non-important digits as OP found with 130899030500194208680850288727868915862901750748094271410143‌​232.00.

For development and debugging, consider using "%e" ,"%g" or "%a" to see a relevant double.


Note that x * x + y * y is prone to over/under flow, even when mathematically sqrt(x * x + y * y) is in the double range. That is one advantage of the standard function hypot(x,y) as it usually handles those edge cases well.

Upvotes: 2

Pushan Gupta
Pushan Gupta

Reputation: 3825

Use correct format specifiers! 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 could modify it to 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);


}

Upvotes: 1

Andrew Henle
Andrew Henle

Reputation: 1

As a reference for anyone viewing this question:

You don't need to write your own function. Standard C provides functions to calculate the hypotnuse:

7.12.7.3 The hypot functions

Synopsis

#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 likely need to link with -lm, though that's not listed explicitly in the function documentation in the C standard nor the latest POSIX documentation. It might be documented elsewhere in the standards.

(Link to C11 [draft] standard - likely to be much longer-lived.)

Upvotes: 1

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

I use Ubuntu Linux and write it this way. Please look if you like it.

#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

Upvotes: 3

ghostprgmr
ghostprgmr

Reputation: 488

You don't need side3 variable - it is not used in calculation. And you function hypotenuse returns the result, so you can directly output the result of sqrt.

Upvotes: 3

Related Questions