我正在研究我教科书中关于二维数组的样本。它具有以下示例,允许用户输入一个值,然后它搜索数组并返回包含该值的元素的位置,或者如果该值不存在则提醒他们。
我想知道,如果多个元素包含用户的价值怎么办?在代码中,我添加了一些循环来初始化二维数组,并且多个元素包含相同的值。我将如何设置搜索以返回多个包含搜索值的元素?
#include <stdio.h>
int main() {
int iTwoD[3][3];
int iFoundAt[2] = {0};
int x, y;
int iFound, iValue = 0;
//initialize the 2-d array
for ( x = 0; x<=2; x++) {
for (y=0;y<=2;y++)
iTwoD[x][y] = (x+y);
} //end outer loop
//print the 2-d array
for (x=0; x<=2; x++){
for (y=0;y<=2;y++)
printf("iTwoD[%d][%d] = %d\n", x, y, iTwoD[x][y]);
}//end outer loop
//Get the user's value to search for
printf("\nEnter your search value: ");
scanf("%d", &iValue);
//Search the 2-d array for user's value
for (x = 0; x<=2; x++) {
for (y = 0; y<=2; y++) {
if ( iTwoD[x][y] == iValue) {
iFound = 1;
iFoundAt[0] = x;
iFoundAt[1] = y;
break;
} //end if
} //end inner loop
} //end outer loop
if (iFound ==1)
printf("\nFound value in iTwoD[%d][%d]\n", iFoundAt[0], iFoundAt[1]);
else
printf("\nValue not found\n");
return 0;
} //end main