writing a function that gets the min value from an array and returns its index
in
Programming Questions
•
2 years ago
Hi, I'm trying to write a function that gets the min value from an array but returns its index. But I keep on getting some errors. What am I doing wrong?
Here is the beginnings of my function:
int getIndex (float[] a) {
for (int i=0; i < a.length; i++) {
if (a[i] == min(a)) {
return i;
}
}
}
Another attempt, basically creating a float[] sqDist that takes a function mouseDist (basically a function that calculates the distance between the x-y of an object and the mouse) as its values. I then used an if statement to perhaps obtain the minimum value associated with the index. But it's giving an error saying that the variables for the function and the return function are not the same! What am I missing here?
int Indexget() {
int gI;
float[] sqDist = new float[squares.length];
for (gI=0; gI < squares.length; gI++) {
sqDist[gI] = mouseDist(squares[gI]);
if (sqDist[gI] == min(sqDist)) {
return gI;
}
}
}
thanks in advance!
Here is the beginnings of my function:
int getIndex (float[] a) {
for (int i=0; i < a.length; i++) {
if (a[i] == min(a)) {
return i;
}
}
}
Another attempt, basically creating a float[] sqDist that takes a function mouseDist (basically a function that calculates the distance between the x-y of an object and the mouse) as its values. I then used an if statement to perhaps obtain the minimum value associated with the index. But it's giving an error saying that the variables for the function and the return function are not the same! What am I missing here?
int Indexget() {
int gI;
float[] sqDist = new float[squares.length];
for (gI=0; gI < squares.length; gI++) {
sqDist[gI] = mouseDist(squares[gI]);
if (sqDist[gI] == min(sqDist)) {
return gI;
}
}
}
thanks in advance!
1