Thread: Double Hops but why ???

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    Double Hops but why ???

    This code is pretty much all working but for some reason. It calculates the root twice before prompting the user if he wants to continue, Very strange I have looked at it a few times can't figure out why its doing this. Can someone help me w/ this problem. Maybe even show how to get it to stop doing this. Anyhow here is the code:





    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #define STOP 0.001

    double func1 (double x);
    double func2 (double x);

    char q, blah,f;
    double Xro, Xrn, EPS, fXro, fprimeXro;
    int Iter;


    void main(void)
    {
    FILE *outptr;

    outptr=fopen("output.txt","wt");


    label3: printf("\nPLease enter a value for Xro");
    scanf("%lf",&Xro);

    printf("\nYou entered Xro= %lf", Xro);
    fprintf(outptr, "\nThe intial guess is Xro= %lf ", Xro);
    Iter=1;
    fprintf(outptr,"\nIter Xro Xrn error");
    label1: fXro = func1(Xro);
    fprimeXro = func2(Xro);
    if (fprimeXro == 0.0) {
    printf ("\nThe slope of a function at iteratuin number =%4d\
    \nis equal to zero. Newton-Raphson Method fails. Program is\
    \ngoing to prompt you to start a new initial guess!",Iter);
    goto label3;
    }
    Xrn = Xro -fXro/fprimeXro;
    printf("\nThe new estimate of the root is = %lf", Xrn);
    EPS = fabs((( Xrn - Xro)/Xro)*100.);
    fprintf (outptr,"\n%4d %12.9f %12.9f %12.9f\
    ",Iter,Xro,Xrn,EPS);
    Iter = Iter + 1;
    printf("Do you wish to exit, if so enter Y");
    scanf("%c", &q);
    if (q=='y' || q=='Y') {
    return;
    }
    if ( EPS <= STOP ) {
    printf ("\nRoot = %lf",Xrn);
    fprintf (outptr,"\nRoot is = %lf", Xrn);
    goto label2;
    }
    else {
    Xro = Xrn;
    goto label1;
    }
    label2:
    fclose(outptr);

    printf ("\nAre you finished using the Newton Raphson's Root finder ?");
    scanf("%c", &f);
    if (f=='y' || f=='Y')
    return;

    if (f=='n' || f=='N')
    goto label3;


    getch();
    }
    double func1(double x)
    {
    return( pow(x,2.0) - x -12. );
    }
    double func2(double x)
    {
    return( 2.0*x - 1.0);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I refuse to read code filled with "goto"s.

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

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    Dude I am a newb trying to learn C Cut me some slack plz...

    Also whats so bad about goto ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Dude I am a newb trying to learn C Cut me some slack plz...
    >
    > Also whats so bad about goto ?

    Well you've just learned your first lesson. goto sucks. Additionally, anything you can do with a goto, you can do without.

    Here is your second lesson.

    In a nutshell, goto makes your program very hard to follow. You want your programs to have a nice logical flow so that they are very easily red by other people. If you design with this in mind, your projects tend to turn out much better.

    Making nice, clean, readable code is one of the best things you can learn.


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

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Lesson #3: void main is wrong. For more information pick a thread that I've replied to at random.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    Flow Ehhhh ? Anyways how could Void Main be wrong thats what it says in my book.
    Last edited by Halo; 04-03-2002 at 09:11 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Cut me some slack plz...
    What makes you think we aren't? We're teaching you the correct way to program as early as possible, if you stick around for a while then you'll learn a great deal simply by not being offended at our replies and absorbing the information.

    >Also whats so bad about goto ?
    If used correctly, nothing technically. But local jumping in your code defeats the purpose of procedural programming. There are some that argue in favor of goto because loops are local jumps as well, but goto tends to confuse people enough so that they use it improperly. Loops are difficult to mess up once you understand the basic concept, but goto's just have too many uses for the intention to be easily understood in code.

    My recommendation is to learn all you can about goto, then learn how to deal without it. You'll probably come to the same conclusion that the rest of us did, goto really isn't needed.

    >Anyways how could Void Main be wrong thats what it says in my book.
    Probably because the author of your book was stupid.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    lol.... On thing I must say is damn you got some quick response times :-)

    Do you know of a link that has all the C commands or allot of the ones used in r00kie programing ?

  9. #9

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is a list of functions and their correct usage, and all the parameters they take. Some of these are non-standard (meaning they are system specific), however, a great number of them are ANSI compliant, which is ideally what you should aim for.

    That isn't to say there is no reason to not write non-ANSI code; there are perfectly valid reasons why you'd need non-ANSI code. However, for most of the learning projects you'd encounter, it's wise to be ANSI compliant.

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

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Here is a list of functions and their correct usage
    I actually haven't been to rt.com/man since I ported the pages to my computer and wrote a program to access them like Linux. I was truly bored one night.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    Okay thanks for the linkage... I looked around a bit but still can't find exaclty how double works. Would you mind posting how that works ? Plz :-)

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I looked around a bit but still can't find exaclty how double works
    [oversimplification]
    What do you mean by how it works? A double is a data type, similar to int except it can hold considerably larger values with greater precision than int. The usage of double is more or less the same as the other types in how you declare it, assign to it, etc.. The only real difference is what kind of values it holds and what you can do with those values.
    [/oversimplification]

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    Arggggggggg.... Okay last post for the day this program is making me mad. For somereason my

    printf ("\nAre you finished using the Newton Raphson's Root finder ?");
    scanf("%c", &f);
    if (f=='y' || f=='Y')
    return;

    if (f=='n' || f=='N')
    goto label3;

    will not work. This is supposed to take place at the end when it finds the pure root. Ne1 know why this is not working. I have a feeling its in the wrong spot, but I have tried it in like 6 other places and now I am turning to you.... again

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Almost every problem in the programs that are posted is caused by programmers using scanf, while having a \n still in the buffer.

    A simple solution to this would be to simply use fgets.

    fgets(buffer, max_input_size, stdin).

    Code:
    buffer[1024] //or any integer that you want for a max input.
    
    fgets(buffer, 1024, stdin);
    This will insert the characters typed including the \0 into buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM