4

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.

Sean
  • 2,890
  • 8
  • 36
  • 78

5 Answers5

3

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.

ghostprgmr
  • 488
  • 2
  • 11
3

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
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
2

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.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
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.)

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • Thank you for the comment! I am aware that there are library functions built into almost every programming language. However, I'm still a beginner so the professor wanted us to try and write our own functions to get a grasp of what it's like. Thank you again! – Sean Apr 08 '17 at 11:41
  • 2
    @Sean I added it so anyone looking to calculate the value of a hypotnuse - but who doesn't care if it's done with a library function - can see that there's a standard function to do so. Someone who doesn't know that there are functions to do this might not even know where to begin looking. There's way too much documentation written that pretty much requires almost knowing the answer before you can find it. – Andrew Henle Apr 08 '17 at 11:49
1

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);


}
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39