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

**URL:** <https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [October 22, 2020, 9:45am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813 "2020-10-22T09:45:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JennyMcCreadie](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@JennyMcCreadie](https://discourse.processing.org/u/JennyMcCreadie)\
**Post date:** [October 22, 2020, 9:45am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/1 "2020-10-22T09:45:00Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 9:49am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/2 "2020-10-22T09:49:04Z")

</div>

> [@JennyMcCreadie](#):
>
> code it is saying that x and y can’t be resolved to a variable.

No ; before the `{` here please

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

😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 9:49am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/3 "2020-10-22T09:49:47Z")

</div>

Hey and welcome to the forum again!

Great to have you back!

---

<div class="post-metadata">

**Author:** ![JennyMcCreadie](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@JennyMcCreadie](https://discourse.processing.org/u/JennyMcCreadie)\
**Post date:** [October 22, 2020, 9:53am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/4 "2020-10-22T09:53:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 9:53am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/5 "2020-10-22T09:53:40Z")

</div>

```auto

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);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![JennyMcCreadie](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@JennyMcCreadie](https://discourse.processing.org/u/JennyMcCreadie)\
**Post date:** [October 22, 2020, 9:55am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/6 "2020-10-22T09:55:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 10:00am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/7 "2020-10-22T10:00:19Z")

</div>

I have to say, it works here

I changed the two for-loops first:

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

```

BOTH “ **;** ” after the ) brackets (before the “{” brackets) were WRONG…!!! 😉

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)

```auto

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

```auto
if(.....) 
    {.......}

```

please no ; after “)” …

* * *

**Problem**

Then all ellipses overlap.

**Solution**

I changed the ellipse line then because of this:

```auto
      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

```auto
      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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 10:03am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/8 "2020-10-22T10:03:43Z")

</div>

> [@JennyMcCreadie](#):
>
> I’m still just confused why my tutors code works and mine doesn’t

The two “;” were the problem.

---

<div class="post-metadata">

**Author:** ![JennyMcCreadie](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@JennyMcCreadie](https://discourse.processing.org/u/JennyMcCreadie)\
**Post date:** [October 22, 2020, 10:07am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/9 "2020-10-22T10:07:01Z")

</div>

Amazing! Thank you so much! I can’t believe I missed that 😂 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 😁

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2020, 10:09am UTC](https://discourse.processing.org/t/why-isnt-my-code-working-when-its-the-exact-same-as-my-tutors/24813/10 "2020-10-22T10:09:05Z")

</div>

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

Have a great day!

😉
