1
I need to calculate the distance between two points, but when I call the function that does this (calculates the distance), it gives the error: "subscripted value is neither array nor Pointer vector".
I did the same thing in another function. I needed to calculate the distance between two points and it worked. I don’t know why it doesn’t work now.
Distance function:
float distancia(int ax, int ay, int bx, int by)
{
return (sqrt((pow((ax - bx), 2)) + (pow((ay - by), 2))));
}
Function where it worked when I called 'distance'':
void mDistancia(char cidadeXY[][150], int coordenadasX[], int coordenadasY[], float matrizDistancia[][30])
{
int c, i, contadorC, contadorI;
float dij;
for(i = 0, contadorI = 0; i < 30; i++, contadorI += 2)
{
for(c = 0, contadorC = 0; c < 30; c++, contadorC += 2)
{
dij = distancia(coordenadasX[contadorI], coordenadasY[contadorI], coordenadasX[contadorC], coordenadasY[contadorC]);
matrizDistancia[i][c] = dij;
}
}
}
Function where it did not work:
void problema5(char cidadeXY[][150], int coordenadasX[], int coordenadasY[])
{
int i, j;
float distanciaX, distanciaY;
float dis, aux;
int cX, cY;
char cid[150];
for(j = 0; j < 30; j++)
{
aux = distancia(coordenadasX[j], cX[j], coordenadasY[j], cY[j]);
if(aux < dis)
{
dis = aux;
strcpy(cid, cidadeXY[i]);
}
}
}
Notice that I did the same thing. I kept the value returned in the variable float aux, just as I did with the variable float dij. But only gives this error in function problem5. The error happens twice, more specifically in the j in coordinates Y[j] and Cx[j]. At least that’s what it says in the terminal.
In fact, and in addition, the order of the parameters does not make any sense in the code.
– Bacco
Thank you! I didn’t realize it in the middle of that pile of code.
– user3741052