用户输入10个数据,此后,用户输入1个数字。然后程序会打印出数字存储在数组中的位置。
int main() {
int arr[10];
int data,num;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",&arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < arr[data]; j++) {
if (data==arr[j]) {
printf("Data %d found in array %d\n",data,arr[j] );
break;
}
else {
printf("Data not found in array\n");
break;
}
}
return 0;
}
答案 0 :(得分:1)
int main() {
int arr[10];
int data, flag = 0;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < 10; j++) {
if (data==arr[j]) {
printf("Data %d found in array at %dth position\n",data,j);
flag = 1;
break;
}
}
if(!flag){
printf("data not found in the array\n");
}
return 0;
}