# I need help with arrays

**URL:** <https://discourse.processing.org/t/i-need-help-with-arrays/37381>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 7, 2022, 11:47am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381 "2022-06-07T11:47:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosepAlqueza](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josepalqueza/32/16390_2.png) [@JosepAlqueza](https://discourse.processing.org/u/JosepAlqueza)\
**Post date:** [June 7, 2022, 11:47am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/1 "2022-06-07T11:47:51Z")

</div>

Hello to all!

The program will have to calculate the Santiago de Compostela distance (x, y position 490,110) to all cities and save the coordinates and distance from the city further away in the Array distances. Likewise, the calc\_Distance() function, also created for this purpose, will be used to help calculate the distances.

```auto

// Multidimensional Array
// Position X of every city
// Position Y
// Population of the city
int cities = {
{930, 350, 800000},
{1063, 215, 1660000},
{900, 190, 675000},
{865, 115, 209000},
{825, 138, 151000},
{813, 111, 254000},
{750, 70, 172000},
{640, 70, 218000},
{490, 110, 98000},
{690, 190, 298000},
{755, 278, 3300000},
{738, 317, 85000},
{613, 378, 59000},
{633, 485, 684000},
{895, 445, 461000},
{1093, 335, 419000},
{667, 591, 83500},
{794, 630, 86000},
};

// Cities names
String names = {“Valencia”, “Barcelona”, “Zaragoza”, “Pamplona”, “Logroño”, “Vitoria”, “Santander”, “Oviedo”, “Santiago de C.”, “Valladolid”, “Madrid”, “Toledo”, “Mérida”, “Sevilla”, “Murcia”, “Palma de M.”, “Ceuta”, “Melilla”};
// number of the cities
int total_Cities = 18;
// Variable to save the distance between cities
int distance;
// Array to save the position on X, Y and the distance between Santiago and the other cities
int distances = new int[3];

void setup(){

size(1227, 639);
// Load Image
SpainMap = loadImage(“MapaEsp.png”);
// Reajust the image
SpainMap.resize(1227, 639);

}

void draw(){

int calc_Distance(int posX_1, int posY_1, int posX_2, int posY_2){
// Apliquem Pitàgores i tornem el valor de la distància entre ciutats
return(int(sqrt(pow(posX_2-posX_1,2)+pow(posY_2-posY_1,2))));
}

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 7, 2022, 1:30pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/2 "2022-06-07T13:30:04Z")

</div>

Hi @JosepAlqueza,

Consider the following just as an example of how you can (maybe) do better …

```auto

public class City {
  PVector pos;
  String name;
  int population;
  int size;
  boolean selected;
  
  public City(String n, float x, float y, int pp) {
    pos = new PVector(x,y);
    name = n;
    population = pp;
    size = floor(map(population,59000,3300000,10,40));
    selected=false;
  }
  
  public void hover(float mx,float my) {
    selected = PVector.dist(new PVector(mx,my),pos) < size/2;    
  }
  
  public float distanceTo(City b) {
     return PVector.dist(pos,b.pos);
  }

  // to do it simple just return Strings
  public ArrayList<String> calcAndShowDistanceTo(City[] cities) {
    ArrayList<String> ret = new ArrayList<String>(); 
    pushStyle();
    textAlign(CENTER,CENTER);
    for (City c: cities) {
      if (c != this) {       
        stroke(255,255,0);
        line(pos.x,pos.y,c.pos.x,c.pos.y);
        fill(255,255,0);
        int d = floor(PVector.dist(pos,c.pos));
        text(d,(pos.x+c.pos.x)/2,(pos.y+c.pos.y)/2);
        ret.add(String.format("%-20s -> %-20s = %d",name,c.name, d));
      }      
    }
    popStyle();
    return ret;
  }
  
  public void show() {
    pushStyle();
    fill(255,0,0);
    noStroke();
    if (selected)
      stroke(0,255,0);
    ellipse(pos.x,pos.y,size,size);    
    text(name,pos.x+size/2,pos.y);    
    text(pos.x + "/" + pos.y ,pos.x+size/2,pos.y+10);
    popStyle();
  }
}

// Array of Cities
// City contains
// Name
// Position X
// Position Y
// Population
City[] cities = {
  new City("Valencia",930, 350, 800000),
  new City("Barcelona",1063, 215, 1660000),
  new City("Zaragoza",900, 190, 675000),
  new City("Pamplona",865, 115, 209000),
  new City("Logroño",825, 138, 151000),
  new City("Vitoria",813, 111, 254000),
  new City("Santander",750, 70, 172000),
  new City("Oviedo",640, 70, 218000),
  new City("Santiago de C",490, 110, 98000),
  new City("Valladolid",690, 190, 298000),
  new City("Madrid",755, 278, 3300000),
  new City("Toledo",738, 317, 85000),
  new City("Mérida",613, 378, 59000),
  new City("Sevilla",633, 485, 684000),
  new City("Murcia",895, 445, 461000),
  new City("Palma de M.",1093, 335, 419000),
  new City("Ceuta",667, 591, 83500),
  new City("Melilla",794, 630, 86000)
};

void setup(){
  size(1227, 639);
// Load Image
// SpainMap = loadImage(“MapaEsp.png”);
// Reajust the image
// SpainMap.resize(1227, 639);
}

void mouseMoved() {
  for (City c : cities) {    
    c.hover(mouseX,mouseY);
  }
}

void draw() {
  background(0); 
  ArrayList<String> ret = null;
  for (City c:cities) {
    if (c.selected) {
      ret = c.calcAndShowDistanceTo(cities);
    }
  }

  for (City c:cities) {
    c.show();
  }
  
  if (ret != null) {
    fill(255);
    for (int i = 0; i < ret.size(); i++) {
      text(ret.get(i),20,20+i*20);
    }    
  }  
}

```

Cheers  
— mnse

 ![example](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/6/06ac952050e79d2133ba2fee1f002a562b28e290.png)

---

<div class="post-metadata">

**Author:** ![JosepAlqueza](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josepalqueza/32/16390_2.png) [@JosepAlqueza](https://discourse.processing.org/u/JosepAlqueza)\
**Post date:** [June 7, 2022, 3:25pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/3 "2022-06-07T15:25:35Z")

</div>

Hi @mnse !

First of all, I want to thank you for your help. But I think my homework is not as complex as what you propose.

I need to calculate, from the variable that I have “calc\_Distances” the distance between “Santiago de Compostela” and the rest of the cities but only the farthest (position X, Y and distance) is kept in the array “distances”.

---

<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:** [June 7, 2022, 3:42pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/4 "2022-06-07T15:42:20Z")

</div>

Yes.

So show your attempt please

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 7, 2022, 3:59pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/5 "2022-06-07T15:59:56Z")

</div>

Hi,

```auto
int[] distances={0,0,0};

void doHomework() {
   int[] sdc = cities[8]; // Santiago de Compostela.

   for(int i=0;i<cities.length;i++) {
     if (i==8) continue; // not really required but ... :)
     int[] other=cities[i];
     int d = floor(dist(sdc[0], sdc[1],other[0], other[1]));
     if(d > distances[2]) { // check if currently stored is nearer
       distances[0] = other[0]; // store x of farest
       distances[1] = other[1]; // store y of farest
       distances[2] = d; // store farest distance
     }
   }
   // After finished distances contains the farest
}

```

Cheers  
— mnse

PS: syntax not testes … Typed offline on mobile 🙂

PPS: and please check your inital posted code … at least the syntax …

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 7, 2022, 4:19pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/6 "2022-06-07T16:19:18Z")

</div>

Hello @JosepAlqueza,

Please take a look at the code you posted:

> [@I need help with arrays](https://discourse.processing.org/t/i-need-help-with-arrays/37381):
>
> Hello to all! The program will have to calculate the Santiago de Compostela distance (x, y position 490,110) to all cities and save the coordinates and distance from the city further away in the Array distances. Likewise, the calc\_Distance() function, also created for this purpose, will be used to help calculate the distances. // Multidimensional Array // Position X of every city // Position Y // Population of the city int cities = { {930, 350, 800000}, {1063, 215, 1660000}, {900, 190, 6750…

I was not able to copy it, paste it and run it.

It should run in your Processing PDE (or equivalent) first.  
Then cut, paste and post as per instructions here:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

After you post it in the topic give it a test run to see if is posted as intended.

Properly formatted and posted code helps us help you.

Hint:

A function should not be inside another function such as draw().

Example:  
[https://processing.org/examples/functions.html](https://processing.org/examples/functions.html)

`:)`

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 8, 2022, 12:28am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/7 "2022-06-08T00:28:11Z")

</div>

This is what I could come up with, you or someone from the community should check for Syntax errors because I am not very good with Processing. Cheers

PImage SpainMap;  
// Multidimensional Array  
// Position X of every city  
// Position Y  
// Population of the city  
int cities = {  
{930, 350, 800000},  
{1063, 215, 1660000},  
{900, 190, 675000},  
{865, 115, 209000},  
{825, 138, 151000},  
{813, 111, 254000},  
{750, 70, 172000},  
{640, 70, 218000},  
{490, 110, 98000},  
{690, 190, 298000},  
{755, 278, 3300000},  
{738, 317, 85000},  
{613, 378, 59000},  
{633, 485, 684000},  
{895, 445, 461000},  
{1093, 335, 419000},  
{667, 591, 83500},  
{794, 630, 86000},  
};

//Color variable  
color blau = color (1,60,213); //Blue  
color verd = color (0,255,0); //Green  
color vermell = color (255,0,0); //Red  
color negre = color (0,0,0); //Black  
color blanc = color (255,255,255); //Whit

// Cities names  
String names = {“Valencia”,“Barcelona”,“Zaragoza”,“Pamplona”, “Logroño”, “Vitoria”,“Santander”, “Oviedo”,“SantiagodC.”,“Valladolid”, “Madrid”, “Toledo”, “Mérida”, “Sevilla”, “Murcia”, “PalmadeM.”, “Ceuta”, “Melilla”};  
// number of the cities  
int total\_Cities = 18;  
// Variable to save the distance between cities  
int distance;  
// Array to save the position on X, Y and the distance between Santiago and the other cities  
int distances = new int[3];

int MaxC;

int MaxD;  
void setup(){

size(1227, 639);  
// Load Image  
SpainMap = loadImage(“MapaEsp.png”);  
// Reajust the image  
SpainMap.resize(1227, 639);

}

void draw(){

calc\_Distance();  
}

void calc\_Distance(){

for (int i=0; i\<cities.length; ; i++){  
MaxD = sqrt((cities[8][0]), (cities[8][1]),(cities[cities.length][0]), (cities[cities.length][1]));  
MaxC = (sqrt((cities[8][0]), (cities[8][1]),(cities[i][j]), (cities[i][j])));  
for (j = 0; j \< cities[i].length-1; j++){  
if(MaxC\>=MaxD){  
MaxD = MaxC;  
distance = MaxD;

```
 }
}

```

}  
}

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 8, 2022, 5:41am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/8 "2022-06-08T05:41:56Z")

</div>

- Have you ever tried this yourself wrt syntax?
- Have you put this code into PDE and pressed the start button ?
- Why you ignoring the hints of other and not posting your code surrounded by code tag ? [Format your code](https://discourse.processing.org/faq#format-your-code)

> you or someone from the community should check for Syntax errors because I am not very good with Processing

If you had at least done the two first points from above, you surley know that your code is far from runable and full of errors…

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 8, 2022, 7:27am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/9 "2022-06-08T07:27:21Z")

</div>

I typed the code on Processing IDE, I’m not using the image file and some other variables, that is why it’s not running, it only showed 1 error when I ran it.

Josep, from my solution you now have a clue to the solution of your project. If I find time, I will fix those syntax errors.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 8, 2022, 8:31am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/10 "2022-06-08T08:31:10Z")

</div>

Maybe It shows only one error because it can’t parse more … 🙂

- int cities = { // wrong declaration
- int distances = new int[3]; // wrong declaration
- function calc\_Distance(){ // wrong js not java
- MaxD = sqrt((cities[8][0]), (cities[8][1]),(cities[cities.length][0]), (cities[cities.length][1])); // useless at all
- MaxC = (sqrt((cities[8][0]), (cities[8][1]),(cities[i][j]), (cities[i][j]))); // wrong at all and j is not defined anywhere, at wrong place and wrong input for sqrt
- for (j = 0; j \< cities[i].length-1; j++){ // wrong and useless
- overall the complete distance checking implemented not makes any sense at all

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 8, 2022, 8:37am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/11 "2022-06-08T08:37:00Z")

</div>

I am gradually getting a hang of Processing Syntax, I’ll post another solution soon.

My solution was for the maximum distance.

I’m also working with P5.js

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 8, 2022, 8:56am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/12 "2022-06-08T08:56:40Z")

</div>

[already posted a solution … just change the **dist** one with **sqrt approach** if you want.](https://discourse.processing.org/t/i-need-help-with-arrays/37381/5)

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 8, 2022, 10:48am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/13 "2022-06-08T10:48:30Z")

</div>

2D Arrays are so difficult to figure out, this is what I came up with again, I think this is what you have been looking for. Please check it out for Syntax errors etc. I’m so stressed and my health doesn’t support rigorous work.

//Don’t forget to declare these variables;  
int Pos\_XS;  
int Pos\_YS;  
int Pos\_GX;  
int Pos\_GY  
int distancesSG

This solution is the distances from"Santiago d C" to every city. The clue for the farthest distance is in this thread where I used variables MaxC and MaxD. Cheers

void draw(){

for (int i=0; i\<cities.length; i++){  
for (j = 0; j \< cities[i].length-1; j++){

//“Santiago d C” is at position cities[8][0]cities [8][1]

Pos\_XS = cities[8][0];  
Pos\_YS = cities[8][1];

//General position of any city cities[i][j], cities [i][j+1];

Pos\_GX = cities[i][j]  
Pos\_GY = cities[i][j+1]

distancesSG = calc\_Distance(Pos\_XS,Pos\_YS ,Pos\_GX,Pos\_GY);

distances = (cities[i][j],cities[i][j+1], distancesSG);

```
    }
 }

```

}

void calc\_Distance(int X1,int Y1, int X2, int Y2){

for (int i=0; i\<cities.length; ; i++){  
for (j = 0; j \< cities[i].length-1; j++){

//Get the coordinates of any city and the city next to it  
X1 = cities[i][j];  
Y1 = cities[i][j+1];  
X2 = cities[i+1][j];  
Y2= cities[i+1][j+1];

distance = dist(X1,Y1, X2,Y2);

```
 }
}

```

}  
}

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 8, 2022, 11:21am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/14 "2022-06-08T11:21:17Z")

</div>

Check out this correction;

distancesSG = calc\_Distance(Pos\_XS,Pos\_YS ,Pos\_GX,Pos\_GY);

distances = (cities[i][j],cities[i][j+1], distancesSG);

---

<div class="post-metadata">

**Author:** ![Kerbig](https://avatars.discourse-cdn.com/v4/letter/k/b38774/32.png) [@Kerbig](https://discourse.processing.org/u/Kerbig)\
**Post date:** [June 11, 2022, 8:01pm UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/15 "2022-06-11T20:01:35Z")

</div>

Hey Josep,  
Did you have luck with this one?

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 14, 2022, 11:46am UTC](https://discourse.processing.org/t/i-need-help-with-arrays/37381/20 "2022-06-14T11:46:11Z")

</div>

I’ve always wondered how you can use a single “for loop” to access a 2D array. Your solution is not perfect because some of the answers were hard code, don’t worry though. Did you run your results on a Processing IDE?..Good luck with your homework.
