-2

I'm having some problems with this loop. It's infinite but I dont know what is causing it. This is part of a long code so I'm just posting part of it I'm supposed to use Machin's approximation series for this and it's supposed to have just a .001 error margin (hence the operation on the while loop)

int main(void){

    int opcion=0, contLeib, banderaLeib, contWallis, bandWallis;

    float piLeib=0.0, sumaLeib=0.0, piLeibT=3.1415, piWallis=1.0, sumaWallis=0.0, nom, numWallis, denWallis, doble, sumaMachin=0.0, restaMachin=0.0;

    bandWallis=1;
    doble=1.0;
    piWallis=1.0;
    sumaMachin=0.0;
    restaMachin=0.0;
    contWallis=1;

    while(sqrt(pow(piLeibT-(4.0*piWallis), 2))>.001){
        if((contWallis%2)!=0){
            if(bandWallis==1){

                doble=contWallis*1.0;

                sumaMachin= sumaMachin + 1/(doble*pow(5,doble));
                restaMachin= restaMachin + 1/(doble*pow(239,doble));
                bandWallis=0;

            } else {
                sumaMachin= sumaMachin - 1/(doble*pow(5,doble));
                restaMachin= restaMachin - 1/(doble*pow(239,doble));
                bandWallis=1;
            }

            contWallis++;
            piWallis= 4.0 * (sumaMachin-restaMachin);
        }

       else{

         contWallis++;
       }
    }

    nom = piWallis*4.0;
    printf("\n%f", nom);
    return 0;
}
  • I guess `sqrt(pow(piLeibT-(4.0*piWallis), 2))>.001` is always true. Have you tried debugging? Do you know how to do so? – David Heffernan Oct 03 '14 at 22:35
  • I don't really know how to do so. I've been using a similar condition to other parts of my code and this is the only time it's not working – kittyPancake Oct 03 '14 at 22:38
  • You should learn how to debug. Inspect the values of the variables through each iteration. Watch the flow of code through the conditions. Compare with your own paper calcs of how it should go. If you don't learn to debug you will be doomed to ask questions like this until the end of time. – David Heffernan Oct 03 '14 at 22:40
  • Do you have a link on a tutorial or something? thank you – kittyPancake Oct 03 '14 at 22:41
  • You could type the obvious keywords into a web search. Interestingly there seem to be millions of books on programming languages. But where's the good book teaching you how to debug. I do feel your pain. Even without a debugger, you can debug by putting printf statements into the code and observing where it goes, and what the values are. Anyway, learning to debug is your number one priority right now. Good luck! :-) – David Heffernan Oct 03 '14 at 22:43
  • I will, thank you. I'm trying with prints right now – kittyPancake Oct 03 '14 at 22:56
  • Today is a good day to learn [how to debug programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Dour High Arch Oct 03 '14 at 23:18
  • Thank you! I already fixed the problem but I know this will come handy in my near future – kittyPancake Oct 03 '14 at 23:34

1 Answers1

0

You use contWallis before is has been initialized.

Presuming that you set it to 1 as you suggest in the comments, on the second iteration of the while loop, the value of if ((contWallis%2)!=0) will be false, so you won't ever enter the body of that conditional, and then you will never change the value of any variables on that iteration of the loop, and on any following ones.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173