Thread: Loops

  1. #1
    * S T U D E N T *
    Join Date
    Oct 2003
    Posts
    30

    Question Loops

    hello. i have written some of the code of the start of a new lab on the mandelbrot sets!

    this part is on getting the iterations right to get the correct complex numbers. here is the code i have done so far (ps i have not optimized or tidied up and is without comments so i hope u understand the logic behind it etc):

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      
      int testOne, testTwo, testThree, testFour;
      double c_reOne = 2.0, c_reTwo = 0.0, c_reThree = -1.0, c_reFour = 1.0;
      double c_imOne = 2.0, c_imTwo = 0.0, c_imThree = -1.0, c_imFour = 1.0;
      
      testOne = mSetTest(c_reOne, c_imOne);
      printf("Test One\n");
      printf("Z = %lf + %lfi\n", c_reOne, c_imOne);
      printf("The real part is\t %lf\n", c_reOne);
      printf("The imaginary part is\t %lf\n", c_imOne);
      printf("The result is\t\t %d\n\n", testOne);
      
      testTwo = mSetTest(c_reTwo, c_imTwo);
      printf("Test Two\n");
      printf("Z = %lf + %lfi\n", c_reTwo, c_imTwo);
      printf("The real part is\t %lf\n", c_reTwo);
      printf("The imaginary part is\t %lf\n", c_imTwo);
      printf("The result is\t\t %d\n\n", testTwo);
      
      testThree = mSetTest(c_reThree, c_imThree);
      printf("Test Three\n");
      printf("Z = %lf + %lfi\n", c_reThree, c_imThree);
      printf("The real part is\t %lf\n", c_reThree);
      printf("The imaginary part is\t %lf\n", c_imThree);
      printf("The result is\t\t %d\n\n", testThree);
      
      testFour = mSetTest(c_reFour, c_imFour);
      printf("Test Four\n");
      printf("Z = %lf + %lfi\n", c_reFour, c_imFour);
      printf("The real part is\t %lf\n", c_reFour);
      printf("The imaginary part is\t %lf\n", c_imFour);
      printf("The result is\t\t %d\n", testFour);
      
      return(0);
    }
    
    int mSetTest(double c_re, double c_im)
    {
      
      double new_re = 0.0, new_im = 0.0, mod_z = 0.0;
      int loop = 0.0;
      
      while (loop < 200 && mod_z < 4)
      {
    
        new_re = ((new_re * new_re) - (new_im * new_im)) + c_re;
        new_im = ((2 * new_re * new_im) + c_im);
        mod_z = ((new_re * new_re) + (new_im * new_im));
        loop++;
        
        if (loop >= 200 && mod_z >=4)
        {
          loop = 0.0;
          return(0);
        }
        
      }
    
      printf("%lf %lf %d\n", new_re, new_im, loop);
    }
    the problem i have you see, is that when i check the last printf statement comes out fine for the real and imaginary parts and for 3 out of the cases the value 'loop' (the number of iterations until |Z| goes above 4.
    in the 4 tests cases, the result should be: 1, 0, 3, 2.
    but i get 1, 200, 3, 2. -- so there is something wrong with the loop and my faint error of solving it has failed!
    is there a really simple explanation for this?!
    any suggestions welcome....

    thankyou in anticipation,

    Chris

    --- goo$ematt ---

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >any suggestions welcome....
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int testOne, testTwo, testThree, testFour;
      double c_reOne = 2.0, c_reTwo = 0.0, c_reThree = -1.0, c_reFour = 1.0;
      double c_imOne = 2.0, c_imTwo = 0.0, c_imThree = -1.0, c_imFour = 1.0;
    
      testOne = mSetTest(c_reOne, c_imOne);
    /*
    [Info 718] Symbol 'mSetTest' undeclared, assumed to return int
    [Info 746] call to function 'mSetTest()' not made in the presence of a prototype
    Warning W8065: Call to function 'mSetTest' with no prototype in function main
    */
      printf("Test One\n");
      printf("Z = %lf + %lfi\n", c_reOne, c_imOne);
      printf("The real part is\t %lf\n", c_reOne);
      printf("The imaginary part is\t %lf\n", c_imOne);
      printf("The result is\t\t %d\n\n", testOne);
    
      testTwo = mSetTest(c_reTwo, c_imTwo);
    /*
    Warning W8065: Call to function 'mSetTest' with no prototype in function main
    */
      printf("Test Two\n");
      printf("Z = %lf + %lfi\n", c_reTwo, c_imTwo);
      printf("The real part is\t %lf\n", c_reTwo);
      printf("The imaginary part is\t %lf\n", c_imTwo);
      printf("The result is\t\t %d\n\n", testTwo);
    
      testThree = mSetTest(c_reThree, c_imThree);
    /*
    Warning W8065: Call to function 'mSetTest' with no prototype in function main
    */
      printf("Test Three\n");
      printf("Z = %lf + %lfi\n", c_reThree, c_imThree);
      printf("The real part is\t %lf\n", c_reThree);
      printf("The imaginary part is\t %lf\n", c_imThree);
      printf("The result is\t\t %d\n\n", testThree);
    
      testFour = mSetTest(c_reFour, c_imFour);
    /*
    Warning W8065: Call to function 'mSetTest' with no prototype in function main
    */
      printf("Test Four\n");
      printf("Z = %lf + %lfi\n", c_reFour, c_imFour);
      printf("The real part is\t %lf\n", c_reFour);
      printf("The imaginary part is\t %lf\n", c_imFour);
      printf("The result is\t\t %d\n", testFour);
    
      return(0);
    }
    
    int mSetTest(double c_re, double c_im)
    {
      double new_re = 0.0, new_im = 0.0, mod_z = 0.0;
      int loop = 0.0;
    /*
    [Warning 524] Loss of precision (initialization) (double to int)
    */
      while (loop < 200 && mod_z < 4)
      {
    
        new_re = ((new_re * new_re) - (new_im * new_im)) + c_re;
        new_im = ((2 * new_re * new_im) + c_im);
        mod_z = ((new_re * new_re) + (new_im * new_im));
        loop++;
    
        if (loop >= 200 && mod_z >=4)
        {
          loop = 0.0;
    /*
    [Warning 524] Loss of precision (assignment) (double to int)
    Warning W8004: 'loop' is assigned a value that is never used in function mSetTest
    */
          return(0);
        }
    
      }
    
      printf("%lf %lf %d\n", new_re, new_im, loop);
    }
    /*
    [Warning 533] function 'mSetTest(double, double)' should return a value
    Warning W8070: Function should return a value in function mSetTest
    */
    
    /*
    [Info 766] Header file 'math.h' not used
    */
    Use %f with printf to display a double.
    Last edited by Dave_Sinkula; 10-20-2003 at 07:04 PM.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM