program calculates hypotenuse. float value always returns 0.

This is a discussion on program calculates hypotenuse. float value always returns 0. within the C Programming forums, part of the General Programming Boards category; I cant get this program to return the hypotenuse value. Can someone please check this out? I'm sure it's a ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    program calculates hypotenuse. float value always returns 0.

    I cant get this program to return the hypotenuse value. Can someone please check this out? I'm sure it's a little mistake somewhere.

    PHP Code:

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

    void main(void)
    {
        
    double side1side2sqrs1s2hypoto;
        
        
    printf("\nThis program will calculate the hypotenuse of a right triangle.\n\n");
        
    printf("Enter side one of the triangle:\n");
        
    scanf("%d",&side1);  ////gets side1
        
        
    printf("Enter side two of the triangle:\n");
        
    scanf("%d",&side2);  ////gets side2
        
        
    sqrs1s2 = (side1*side1 side2*side2);
        
    hypoto sqrt(sqrs1s2); ////uses squareroot to calculate hypotenuse
        
        
        
    printf("The lenth of the hypotenuse is: %d \n"hypoto);


  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) void main is bad.... mmkay? read the FAQ

    2) you are working with doubles and telling scanf to read integers ("%d") instead of "%lf"

    3) don't double post
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,698
    Check your format specifiers for scanf() and printf() Hint: "%d" is not double.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Quote Originally Posted by claudiu View Post
    1) void main is bad.... mmkay? read the FAQ

    2) you are working with doubles and telling scanf to read integers ("%d") instead of "%lf"

    3) don't double post
    sorry for the double post. It took forever to post so i pressed refreshed and i guess that what caused it. srry again.
    Also, not to sure which FAQ you are talking about. Can you provide a url?
    Last edited by team23; 10-02-2010 at 08:40 AM.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    ok so I changed the %d 's to %f. Value still comes to 0.000000

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    ok i got it now! I just had to change to double to float at the top where I declare the variables. haha silly me. kk thanks

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    A couple of things that might be useful to know:
    There's a function called _hypot available in the C Runtime Library which calculates the hypotenuse. It also works correctly even in the case where an intermediate result of the squaring of one of the input values might overflow a double.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by team23 View Post
    Also, not to sure which FAQ you are talking about. Can you provide a url?
    Scroll this screen up and look at the blue bar...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Please someone help
    By jsking09 in forum C Programming
    Replies: 6
    Last Post: 02-14-2010, 10:29 PM
  3. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-26-2008, 11:24 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21