โปรแกรมหา Index ของค่าที่ต้องการใน Array
void setup(){
int[] K = {15,5,6,7,8,9,4,1,2,11,20,3};
FindIndexValue(K,5);
FindIndexValue(K,2);
FindIndexValue(K,8);
FindIndexValue(K,18);
FindIndexValue(K,20);
FindIndexValue(K,0);
FindIndexValue(K,1);
}
void FindIndexValue(int[] a,int x){
int Index=-1;
boolean Found=false;
for(int i=0;i<a.length&&(!Found);i++){
if(x==a[i]){
Index = i;
Found = true;
}
}
if(Found){
println(x+" has index = "+Index+" in K");
}
else{
println(x+" is not in K");
}
}