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

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("Toledo",738, 317, 85000),
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);
// 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.

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

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.

Hint:

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

:)

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
// 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);
// 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 â€¦

• 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

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.