Thread: Simple calculation not working, need help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    Simple calculation not working, need help

    I am trying to get a program to calculate the side of a triangle. I know this is a simple code, im a first year student. So bare with me

    http://rchevys.com/ffxi/underRATEDGames/test.txt

    For some reason, when it gets to the output for the calculation it just puts 0 instead of the value it should be desplaying. what am i doing wrong here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("C = %d", C);
    Use %f for floats and doubles, not %d
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    I changed that but the answer is still coming out to "0"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Betcha didn't change all your scanf() calls to use the correct conversion either...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    I changed it to :
    (still no good)

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <windows.h>
    
    double A,B,C,calculation;
    int selection;
    
    
    main()
    {
    	printf("Calcute the side of a traingle...a^2 + b^2 = c^2\n");
    	printf("Which sides do you have?\n");
    	printf("(1) a , b\n(2) b , c\n(3) a , c\n");
    	scanf("%d", &selection);
    		if (selection==1)
    		{
    			printf("Please enter the two sides:\n");
    			printf("A: ");
    			scanf("%f", &A);
    			printf("B: ");
    			scanf("%f", &B);
    			calculation = A*A-B*B;
    			C = sqrt(calculation);
    			printf("C = %f", C);
    		}
    		if (selection==2)
    		{
    			printf("Please enter the two sides:\n");
    			printf("B: ");
    			scanf("%f", &B);
    			printf("C: ");
    			scanf("%f", &C);
    			calculation = B*B-C*C;
    			A = sqrt(calculation);
    			printf("A = %f", A);
    		}
    		if (selection==3)
    		{
    			printf("Please enter the two sides:\n");
    			printf("A: ");
    			scanf("%f", &A);
    			printf("C: ");
    			scanf("%f", &C);
    			calculation = C*C-A*A;
    			B = sqrt(calculation);
    			printf("B = %f", B);
    		}
    	Sleep (50000);
    
    	return 0;
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Betcha didn't change all your scanf() calls to use the correct conversion either...
    Code:
    double A,B,C,calculation;
    /* ... */
    scanf("%f", &A);
    Nope. Use "%lf" for double with scanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    Code:
    printf("A: ");
    scanf("%1f", &A);
    printf("B: ");
    scanf("%1f", &B);
    calculation = A*A-B*B;
    C = sqrt(calculation);
    printf("C = %f", C);
    I tried that with still no luck. Still giving me 0.0000
    I also tried puting printf("C = %1f", C);
    Same result

  8. #8
    printf("Please enter the two sides:\n");
    printf("A: ");
    scanf("%f", &A);
    printf("B: ");
    scanf("%f", &B);
    calculation = A*A-B*B;
    C = sqrt(calculation);
    printf("C = %f", C);

    maybe its just me but the formula for finding sides on right
    triangles is :
    (Pathagerean Therom.. SP?)
    A*A + B*B = C*C

    (pay close attention to the + )

    EDIT : i was in a rush so i didnt check your other formula's
    on there, but i suggest you check them if there not giving you the
    correct answer

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Summonerur
    Code:
    printf("A: ");
    scanf("%lf", &A); /* 'ell' -- not 'one' */
    printf("B: ");
    scanf("%lf", &B); /* 'ell' -- not 'one' */
    calculation = A*A-B*B;
    C = sqrt(calculation);
    printf("C = %f", C);
    I also tried puting printf("C = %1f", C);
    No need.
    Code:
    printf("C = %f", C);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    Woops, that was normally a +. I accidently copied and pasted over it when i was trying something else. but thats not the problem.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It seems to be rounding it off at random places at times.
    Welcome to the world of floating point numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    Quote Originally Posted by quzah
    Welcome to the world of floating point numbers.

    Quzah.
    Wow! Thanks for the help, I know everything I need to know now.



    Do you actually enjoy sitting here bashing on people who are trying to learn things? I have been looking at previous threads to maybe learn some things off of other peoples questions and I noticed all you do is post sarcastic comments that have absolutely no educational value.

    I am sorry if we are not all as smart as you are or fortunate enough to have had or have professors who know what they are teaching. /bow

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Summonerur
    Wow! Thanks for the help, I know everything I need to know now.
    That is everything you need to know about floating point numbers. There is no rhyme or reason to why they round they do. They just do. If they run across something they can't represent, it rounds it. You as the programmer have absolutely no way of easily telling when it's going to round. It just does.

    As I stated: "Welcome to the world of floating point numbers." If you don't want rounding, don't use floating point numbers. Period.

    Trust me, you'll know when you're being "bashed" by me.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Summonerur
    Wow! Thanks for the help, I know everything I need to know now.



    Do you actually enjoy sitting here bashing on people who are trying to learn things? I have been looking at previous threads to maybe learn some things off of other peoples questions and I noticed all you do is post sarcastic comments that have absolutely no educational value.

    I am sorry if we are not all as smart as you are or fortunate enough to have had or have professors who know what they are teaching. /bow
    Well, I suppose from your viewpoint it might seem like Quzah was not trying to be helpful, but if you had taken the initiative and actually thought about the answer he gave you (as opposed to moaning about what a mean and nasty guy he is) you might have come up with the novel idea of looking up some floating point resources for yourself.

    From the viewpoint of the regulars on this board, you are just another student in a long list of students who come here for a short time and are gone for good. Every year a new batch pops up wanting to get all kinds of good information, in the easiest fashion possible. Well hey, this board is to help people get good info, and yeah it is free, so you can't really complain if you don't like what you get. But here is the kicker - how many times do you think some of the members on the board, like Quzah or Salem or any of the others with 3000+ posts have seen a question like, 'Gee, this floating point stuff is whacked!' ? Believe me, they have seen it many times. And as the answer is not generally a short one (as evidenced by some of the documentation referred to previously and in this post) it is somewhat easier to just give a bit of hint (like Quzah did) which ought to be enough to prompt someone like you to do some looking instead of sitting there like a baby bird with its neck stretched out and its beak open waiting for mommy bird to vomit up some worms or whatever.

    That said, here are a couple of other things with regards to your question, should you wish to take the time to look through the material.

    http://docs.sun.com/source/806-3568/ncgTOC.html

    http://www.eskimo.com/~scs/C-faq/s14.html

    This next one might be a little helpful too:

    http://stevehollasch.com/cgindex/coding/ieeefloat.html

    ~/
    Last edited by kermit; 09-24-2004 at 03:01 PM.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well if you really want everything, then read this
    http://cch.loria.fr/documentation/IE...M/goldberg.pdf
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifdef is harmful - but simple workaround is not working
    By amitbern in forum C Programming
    Replies: 15
    Last Post: 12-06-2008, 07:02 AM
  2. Replies: 10
    Last Post: 09-07-2008, 11:38 PM
  3. Simple program not working
    By oobootsy1 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2005, 02:20 AM
  4. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  5. simple program not working
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2002, 11:36 PM