e.g. K = { 4, 8 , 5}
5 has index = 2 in K
4 has index = 0 in K
9 is not in K
int [] k = {
4, 8, 5
};
void setup () {
size (130, 100);
background (255, 255, 0);
fill (0);
Find_Index (5, 40);
Find_Index (4, 60);
Find_Index (9, 80);
}
void draw () {
}
int Find_Index (int value, int pointY) {
boolean stop = false;
for (int i = 0; i<k.length; i++) {
if (k[i] == value) {
text(+value+" has index = "+i+" in K", 10, pointY);
stop = true;
}
else if (i == (k.length-1) && stop == false) {
text (+value+ " is not in K", 30, pointY);
}
}
return value;
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น