The variable does not exist.

edited September 2017 in Programming Questions

Hi, I´m working on my first sketch, and I have to do some extensive calculation in it. So I wrote only the calculation with a clock to know how long this will take. The error I get for all the variables is that they do not exist. What am I doing wrong?

void setup() {
   widht = 80;
   heght = 40;
   zoomx1 = int(widht / 4);
   zoomx2 = 2;
   zoomy1 = int(heght / 4);
   zoomy2 = 2;
noLoop();
int m = millis();
}

void draw() {
  for (int i = 0; i < widht; i = i+1) {
    x =  i / zoomx1 - zoomx2;
      for (int j = 0; j < heght; j = j+1) {
        y = zoomy2 - j / zoomy1;
        zr = 0;
        zi = 0;
        zr2 = 0; 
        zi2 = 0; 
        cr = x;   
        ci = y;  
        n = 1;
        int i = 0;
          while (n < 200 && (zr2 + zi2) < 4) {
            zr2 = zr * zr;
            zi2 = zi * zi;
            zi = 2 * zi * zr + ci;
            zr = zr2 - zi2 + cr;
            n++;
          }
      }
   }
 int t = millis() - m;
 print(t);  
}

Thanks in advance,
Noel

Tagged:

Answers

  • Ohh, I see, processing wants you to declare all variables There isn´t a default, like you had in VB6, the "variant". It´s a pity that they didn´t use a default like int. So I made all vars global, and don´t get any error but when I run the sketch it doesn't print anything to the console. Why?

        int x, y, zr, zi, zr2, zi2, cr, ci, n, m, t;
        int wdht, heght, zoomx1, zoomx2, zoomy1, zoomy2;
    
        void setup() {
         wdht = 80;
         heght = 40;
         zoomx1 = (wdht / 4);
         zoomx2 = 2;
         zoomy1 = int(heght / 4);
         zoomy2 = 2;
         noLoop();
         m = millis();
        }
    
        void draw() {
    
          for (int i = 0; i < wdht; i = i+1) {
            x =  i / zoomx1 - zoomx2;
              for (int j = 0; j < heght; j = j+1) {
                y = zoomy2 - j / zoomy1;
                zr = 0;
                zi = 0;
                zr2 = 0; 
                zi2 = 0; 
                cr = x;   
                ci = y;  
                n = 1;
                i = 0;
                  while (n < 200 && (zr2 + zi2) < 4) {
                    zr2 = zr * zr;
                    zi2 = zi * zi;
                    zi = 2 * zi * zr + ci;
                    zr = zr2 - zi2 + cr;
                    n++;
                  }
              }
           }
         t = millis() - m;
         print(t);  
        }
    
  • edited September 2017 Answer ✓

    ... it doesn't print anything to the console. Why?

    @ line #28, i = 0; makes for (int i = 0; i < wdht; i = i+1) { infinite! :-&

  • edited September 2017 Answer ✓

    So I made all vars global, ...

    • Don't declare everything as global (a.K.a. fields in Java)! It's bad programming! [-X
    • Before deciding whether a variable should be declared globally rather than locally scoped, ask yourself:
    1. Does more than 1 function needs to access it as well?
    2. Can't we rather pass it to those functions instead if they need that variable?
    3. Does that variable needs to remember its previous value between function calls?
    • If 3rd question is affirmative, that variable needs to be global regardless.
    • If 1st question is affirmative, also check whether the 2nd is viable.
    • If it isn't or you prefer not to, that variable should be global.
  • Thank you for all your kind answers!

Sign In or Register to comment.