Why isn't my code working when it's the exact same as my tutors?

Hello, I’m following a tutorial from my tutor and it’s just the start so he made a mistake on purpose for all of the ellipses to fall in to the top left (so that’s not the issue here) but whenever I try to do the exact same code it is saying that x and y can’t be resolved to a variable. His sketch shows up fine and he gets no errors. I’ve written it out twice now and checked it character by character and nothing is different to his. Am I going crazy or is there a reason for this? I’ve found that it happens quite often, I’m not sure if we’re using different versions or something? But I don’t really think that’s the issue.

Table data;
int cols = 40;
int rows = 25;
// 40 wide, 25 down
// determin our rows and columns

void setup(){
size(1000,1000);
background(255);
data = loadTable (“DrugData2.csv”, “header”);
noLoop();
}

void draw(){

// width divided by columns
float gridX = width/cols;
float gridY = height/rows;
noFill();
stroke(0,64);
// for int y = 0, y is less than rows
for (int y = 0; y < rows; y++); {
for (int x = 0; x < cols; x++); {
ellipse(x, y, gridX, gridX);

}

}
}

Thanks for any help! :slight_smile:

No ; before the { here please

for (int y = 0; y < rows; y++); {

:wink:

1 Like

Hey and welcome to the forum again!

Great to have you back!

1 Like

Thank you! Sorry but it’s still not working :joy:, any more ideas?

1 Like



Table data;
int cols = 40;
int rows = 25;
// 40 wide, 25 down
// determin our rows and columns

void setup() {
  size(1000, 1000);
  background(255);
  data = loadTable ("DrugData2.csv", "header");
  noLoop();
}

void draw() {

  // width divided by columns
  float gridX = width/cols;
  float gridY = height/rows;
  noFill();
  stroke(0, 64);
  // for int y = 0, y is less than rows
  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      ellipse(6+x*(gridX-4), 6+y*(gridY-4), 
        gridX, gridY);
    }
  }
}

Thank you so much! This one works :slight_smile: What do the numbers you added do? I’m still just confused why my tutors code works and mine doesn’t :frowning: (I’d upload a photo, but can’t share anything to do with the tutorial for legal reasons haha)

I have to say, it works here

I changed the two for-loops first:

for (int y = 0; y < rows; y++); {
for (int x = 0; x < cols; x++); {

BOTH “;” after the ) brackets (before the “{” brackets) were WRONG…!!! :wink:

Then it should run!!!

Explanation

The ; ends the for-loop command.

It says: executed everything between ) and the ; which is nothing.

Then the x and the y is not known within the { … } section because the x and y are only known within the for-loop section. So the ellipse command fails, x and y are not known here.

When we delete the ; the for-loop extends its area (scope) to the entire {…} section so the x and y are known inside the {…} section so the ellipse works again.

Remark

This would also work (no {…} here, but also no “;” signs). Here the for-loop extends implicitly from for-loop 1 to for-loop 2 to the ellipse command so the x and y are known. No “;” allowed at the end of the line.

(but it’s always better to work with { … } to see easily how far the for-loop extends in the code and to be able to insert a fill() command for example without destroying the code section)


Table data;
int cols = 40;
int rows = 25;
// 40 wide, 25 down
// determine our rows and columns

void setup() {
  size(1000, 1000);
  background(255);
  data = loadTable ("DrugData2.csv", "header");
  noLoop();
}

void draw() {

  // width divided by columns
  float gridX = width/cols;
  float gridY = height/rows;
  noFill();
  stroke(0, 64);
  // for int y = 0, y is less than rows
  for (int y = 0; y < rows; y++) 
    for (int x = 0; x < cols; x++) 
      ellipse(x*13, y*13, gridX, gridX);
}//func 

Remark

This error is also common when we write

if(.....) 
    {.......}

please no ; after “)” …


Problem

Then all ellipses overlap.

Solution

I changed the ellipse line then because of this:

      ellipse(6+x*(gridX-4), 6+y*(gridY-4), 
        gridX, gridY);

it boils down to :

  • add 6 to position left and top so we have a space between screen border and 1st ellipse
  • multiply the position x and y by (gridX-4) so they are spread over the screen (and slightly overlap because of the -4).

short form is

      ellipse(x*gridX, y*gridY, 
        gridX, gridY);

to spread the ellipses over the screen. We use x and y to calculate a screen position by multiplying with gridX and gridY.

I hope this helps!

Warm regards,

Chrisir

The two “;” were the problem.

1 Like

Amazing! Thank you so much! I can’t believe I missed that :joy: The ellipses were overlapping in the corner on purpose because it was an issue my tutor made on purpose so he could explain the next part and how to solve it, but thank you for doing it for me! I hope you have a great day :grin:

1 Like

I explained a bit more in the post above about the ) ; { mistake

Have a great day!

:wink: