在尝试在二维数组中搜索元素。

huangapple go评论53阅读模式
英文:

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 &lt;stdio.h&gt;
void main()
{
int arr[2][3]={{1,2,3},{4,5,6}};
printf(&quot;enter your number\n&quot;);
int n,row,column,i,j,flag=0;
scanf(&quot;%d&quot;,&amp;n);

row = sizeof(arr)/sizeof(arr[0]);
column = sizeof(arr[0])/row;

for(i=0;i&lt;row;i++)
{
    for(j=0;j&lt;column;j++)
    {
        if(n==arr[i][j])
        {
            printf(&quot;the number is at index\n %d %d&quot;,i,j);
            flag=1;
            break;
        }
   
        if(flag==0)
        printf(&quot;this is not in the program\n&quot;);
        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 ifs body will be executed and the output is always gonna be &quot;this is not in the program\n&quot;. In addition to that, I guess you want your break after that to be in the ifs 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 ifs body?

If so you need to exit the outer for loop aswell. Your code then could look something like this:

#include &lt;stdio.h&gt;

void main()
{
    int arr[2][3] = { {1,2,3},{4,5,6} };
    int n, row, column, i, j, flag = 0;

    printf(&quot;enter your number\n&quot;);
    scanf(&quot;%d&quot;, &amp;n);

    row = sizeof(arr) / sizeof(arr[0]);
    column = sizeof(arr[0]) / row;

    for (i = 0; i &lt; row; i++)
    {
        for (j = 0; j &lt; column; j++)
        {
            if (n == arr[i][j])
            {
                printf(&quot;the number is at index\n %d %d&quot;, i, j);
                flag = 1;
                break;
            }
        }

        if (flag != 0)
            break;
    }

    if (flag == 0)
        printf(&quot;this is not in the program\n&quot;);
}

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 ints 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;

huangapple
  • 本文由 发表于 2023年5月20日 22:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定