This dude need someones help, bad????

This is a discussion on This dude need someones help, bad???? within the C Programming forums, part of the General Programming Boards category; Hello again, I cant seem to get the forth root to execute properly. Eg; at int 3 the square should ...

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question This dude need someones help, bad????

    Hello again,

    I cant seem to get the forth root to execute properly. Eg; at int 3 the square should read 1.732050808 and the forth root should read 1.316074013. Now my squares are fine but the forth roots aren't. What is wrong in my program and how can i fix it quickly.
    Thanx,

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

    void prn_heading (void);
    void prn_and_read_data (void);

    int main()
    {
    prn_heading();
    prn_and_read_data();
    return 0;
    }

    void prn_heading(void)
    {
    printf("%3s%28s%28s%28s",
    "Integer", "Square Root", "Fourth Root\n",
    "--------------------------------------------------------------\n\n");
    return;
    }

    void prn_and_read_data (void)
    {
    int x;
    double square_root, fourth_root;

    for (x = 1; x <= 100; ++x)
    printf("%5d:%28.9e %28.9e\n", x = x, sqrt(x), square_root = pow(x, x));
    fourth_root = square_root * square_root;
    printf("%50.9e", fourth_root);
    ;
    return;
    }
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Unregistered
    Guest
    I haven't done any math since school, but i believe that the problem is in how you create your fourth root. Multiplying the square root by itself is giving you the first number again, not the fourth root. For example- the square root of 4 is 2. 2*2 equals 4, not 1.7 something. Try something else. I believe that dividing the square root by the first number will work.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question math student...

    Hello,
    when i started the program i too could not believe it but somewhere in the code maybe...i donno.
    anyway another question i had is when i ran it after compiling all the forth root numbers were exactly the same... sounds wierd huh?
    can you please explain why?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    did it do that with or without the changes? Just to let you know, you shouldn't need to write x=x, you should be able to just use x in the display argument.

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Talking draco

    If i axe x = x then it prints it onto the scree haphazardly. I tried what you said by placing it in the argument stream but on the visual C++ software that what i got.

    Now my fourth root does not print with any changes, its something like 2.65 all the way down. weird huh?
    I am sure my math is correct but like the old west says " i aint no math want to be player!"
    just kidding... no sense to be made there!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    x^(1/4) = x^(1/2 * 1/2) = (x^(1/2))^(1/2)

    fourth_root = sqrt( sqrt(x) );

  7. #7
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thumbs down hello there pimppy!

    Hello,
    your close but no dice. The program when you run it should read:

    integer sq rt fth rt
    1 1.00000000 1.00000000
    2 1.414213562 1.189207115
    3 1.732050808 1.316074013
    4 2.00000000 1.414213562

    Thanks for trying to help me out. If you can still try i would appreciate it.
    Keep pimpin'
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    this works

    printf("%.9lf\n", sqrt(sqrt(2.)));

    just modify this to suit your needs

  9. #9
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question like this?

    Do you mean like this?
    Because its still not working, Mr. Pimp.
    Thanks anyways but if you can get it to work, please let me know, sir.
    It does complie and run but the figures are way off and i am starting to wonder whoever wrote this damn book was probably way off to start with.
    Thanks!

    void prn_and_read_data (void)
    {
    int x;
    double square_root, fourth_root;

    for (x = 1; x <= 100; ++x)
    printf("%5d:%28.9e %28.9e\n", x = x, sqrt(x), square_root = pow(x, x));
    fourth_root = sqrt( sqrt(x) );
    printf("%50.9lf\n", sqrt(sqrt(2.))); /*inserted here*/
    ;
    return;
    }

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    why do you have that extra printf at the end? You need to take out 'square_root = pow(x, x)' and replace that with 'square_root = sqrt( sqrt (x) )'.

    Also, your control string (or whatever its called) is screwed up. What I meant for you to do is change the control string in the printf thats inside the for-loop:

    "%5d:%28.9e %28.9e\n", --> "%d %.9lf %.9lf\n"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad coding habits
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-12-2005, 06:44 PM
  2. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  3. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM
  4. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 08:31 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 10:08 PM

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