英文:
trying to search element in 2d array
问题
我是C语言的初学者,正在尝试编写一个在二维数组中搜索元素的代码,但它不起作用。如果您发现问题,请帮忙。
#include <stdio.h>
void main()
{
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("请输入您要查找的数字\n");
int n, row, column, i, j, flag = 0;
scanf("%d", &n);
row = sizeof(arr) / sizeof(arr[0]);
column = sizeof(arr[0]) / sizeof(arr[0][0]);
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (n == arr[i][j])
{
printf("该数字位于索引 %d %d", i, j);
flag = 1;
break;
}
}
}
if (flag == 0)
printf("这个数字不在数组中");
}
英文:
i am a beginner in c language and am trying to make a code which searchs an element in 2d array and its not working .if you see the problem please help out.
#include <stdio.h>
void main()
{
int arr[2][3]={{1,2,3},{4,5,6}};
printf("enter your number\n");
int n,row,column,i,j,flag=0;
scanf("%d",&n);
row = sizeof(arr)/sizeof(arr[0]);
column = sizeof(arr[0])/row;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(n==arr[i][j])
{
printf("the number is at index\n %d %d",i,j);
flag=1;
break;
}
if(flag==0)
printf("this is not in the program\n");
break;
}
}
}
答案1
得分: 1
If you are not searching for the first element (value at arr[0][0]
), the first if
in the first iteration will not evaluate to true
and flag
won't be set to 1
. Therefore the second if
's body will be executed, and the output will always be "this is not in the program." In addition, you may want your break
after that to be within the if
's scope.
So, in fact, you need to move that second if
and the code belonging to it to the end of your code to get your desired results.
I'm not sure if you want only the indices of the first found element to be printed or not, but I guess you want only one because of your break
in the first if
's body.
If so, you need to exit the outer for
loop as well. Your code could then look something like this:
#include <stdio.h>
void main()
{
int arr[2][3] = { {1,2,3},{4,5,6} };
int n, row, column, i, j, flag = 0;
printf("enter your number\n");
scanf("%d", &n);
row = sizeof(arr) / sizeof(arr[0]);
column = sizeof(arr[0]) / sizeof(arr[0][0]);
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (n == arr[i][j])
{
printf("the number is at index\n %d %d", i, j);
flag = 1;
break;
}
}
if (flag != 0)
break;
}
if (flag == 0)
printf("this is not in the program\n");
}
And by the way, what is the point of calculating row
and column
if both of these values are already hardcoded? I would suggest you could change them to be const int
and use them to create your array. You could have your variable definitions then like the following:
const int row = 2;
const int column = 3;
int arr[row][column] = { {1,2,3},{4,5,6} };
int n, i, j, flag = 0;
英文:
If you are not searching for the first element (value at arr[0][0]
), the first if
in the first iteration will not evaluate to true
and flag
won't be set to 1
.
Therefore the second if
s body will be executed and the output is always gonna be "this is not in the program\n"
. In addition to that, I guess you want your break
after that to be in the if
s scope.
So in fact you need to move that second if
and the code belonging to it to the end of your code to get your desired results.
I am not sure if you want only the indices of the first found element to be printed or not. But I guess you want only one because of your break
in the first if
s body?
If so you need to exit the outer for
loop aswell. Your code then could look something like this:
#include <stdio.h>
void main()
{
int arr[2][3] = { {1,2,3},{4,5,6} };
int n, row, column, i, j, flag = 0;
printf("enter your number\n");
scanf("%d", &n);
row = sizeof(arr) / sizeof(arr[0]);
column = sizeof(arr[0]) / row;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (n == arr[i][j])
{
printf("the number is at index\n %d %d", i, j);
flag = 1;
break;
}
}
if (flag != 0)
break;
}
if (flag == 0)
printf("this is not in the program\n");
}
And by the way what is the point of calculating row
and column
if both of these values are already hardcoded? I would suggest you could change them to be const int
s and use them to create your array. You could have your variable definitions then like the following:
const int row = 2;
const int column = 3;
int arr[row][column] = { {1,2,3},{4,5,6} };
int n, i, j, flag = 0;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论