I need help with arrays

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, 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))));
}
1 Like

Hi @JosepAlqueza,

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


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

3 Likes

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”.

1 Like

Yes.

So show your attempt please

Hi,

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 :slight_smile:

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

1 Like

Hello @JosepAlqueza,

Please take a look at the code you posted:

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

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

:)

2 Likes

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;

 }
}

}
}

  • 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

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

1 Like

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.

1 Like

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

  • 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

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

already posted a solution … just change the dist one with sqrt approach if you want.

1 Like

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

 }
}

}
}

Check out this correction;

distancesSG = calc_Distance(Pos_XS,Pos_YS ,Pos_GX,Pos_GY);

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

Hey Josep,
Did you have luck with this one?

1 Like

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.