How do I write a function to search for an element in two dimensional array: if exists returns 1, otherwise returns no?
#include <stdio.h>
int search(int a[3][3],int x);
int main ()
{
int Array[3][3]; // array of size 3*3
int i,j; //counters i,j
int result,number;
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" Array[%d][%d]= ",i,j);
scanf("%d", &Array[i][j]); //Fill The 3*3 array
}
}
printf("Enter The number you want:>");
scanf("%d",&number);
result=search(Array,number);
if(search(Array,number))
printf("Number exists\n");
else
printf("Number does not exists\n");
return 0;
}
int search(int a[3][3],int x){
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if (x==a[i][j])
return 1;
return 0;
}
}
}
is this right ??开发者_如何转开发
No, it's not. You need to move the return 0;
out of the two for
loops, and have it be the last statement in search
.
No. The return 0;
statement should be placed on the line before the very last closing brace.
Your program is not running because the search algorithm returns 1 if and only if the search element is in array[0][0], otherwise it returns zero and you think that the element doesn't exists. All you need to do is return 0 after traversing the full multi-dimensional array.
You can look into above answers. They have given good solutions.
No because your return 0;
is inside the for loops.
I think what you want is that :
#include <stdio.h>
int search(int a[3][3],int x);
int main ()
{
int Array[3][3]; // array of size 3*3
int i,j; //counters i,j
int result,number;
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
{
printf(" Array[%d][%d]= ",i,j);
scanf("%d", &Array[i][j]); //Fill The 3*3 array
}
}
printf("Enter The number you want:>");
scanf("%d",&number);
result=search(Array,number);
if(result)
printf("Number exists\n");
else
printf("Number does not exists\n");
return 0;
}
int search(int a[3][3],int x)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if (x==a[i][j])
return 1;
}
}
return 0;
}
Jon gave you the answer you need, but there are some details to be aware of.
C's treatment of arrays is such that the search
function doesn't receive a 3x3 array of int
; rather, it receives a pointer to a 3-element array of int
. From the C language standard, draft n1256:
6.3.2.1 Lvalues, arrays, and function designators
...
3 Except when it is the operand of thesizeof
operator or the unary&
operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
Thus, when you call result = search(Array, number);
, the type of the expression Array
is implicitly converted from a 3-element array of 3-element arrays of int
(int [3][3]
) to a pointer to a 3-element array of int
(int (*)[3]
). In the context of a function parameter declaration, T a[]
and T a[n]
are synonymous with T *a
. You could change the function prototype to
int search(int (*a)[3], int x)
and it would behave exactly the same.
One consequence of this is that search
can operate not just on 3x3 arrays, but on any Nx3 array. You've written your function to assume that a
is always 3x3; if you want to be able to handle arrays of different numbers of rows, you would need to pass in a separate parameter to specify the number of rows in the array:
int search(int (*a)[3], size_t rows, int x)
{
size_t i, j;
for (i = 0; i < rows; i++)
for (j = 0; j < 3; j++)
if (a[i][j] == x)
return 1;
return 0;
}
int main(void)
{
int fiveRowArray[5][3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9},
{10,11,12}, {13,14,15}};
int twoRowArray[2][3] = {{ 1, 2, 3}, { 4, 5, 6}};
int number;
printf("Gimme a number: ");
fflush(stdout);
scanf("%d", &number);
if (search(array, sizeof fiveRowArray / sizeof *fiveRowArray, number))
printf("Number exists in fiveRowArray\n");
else
printf("Number does not exist in fiveRowArray\n");
if (search(array, sizeof twoRowArray / sizeof *twoRowArray , number))
printf("Number exists in twoRowArray \n");
else
printf("Number does not exist in twoRowArray \n");
return 0;
}
The sizeof arr / sizeof *arr
expression calculates the number of elements in the array by getting the total array size in bytes (sizeof arr
) and dividing that by the number of bytes in an individual array element (sizeof *arr
or sizeof arr[0]
). Note that this only works for expressions of array type; it will not work for pointers that are being treated as arrays (such as the expression a
in the search
function).
If you want to handle different numbers of rows and columns, you'll have to take a different approach:
int search(int *a, size_t rows, size_t cols, int x)
{
size_t i, j;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
if (a[i * cols + j] == x)
return 1;
return 0;
}
int main(void)
{
int fiveByThree[5][3] = {...};
int twoByFour[2][4] = {...};
...
if (search(&fiveByThree[0][0],
sizeof fiveByThree / sizeof *fiveByThree,
sizeof fiveByThree[0] / sizeof *fiveByThree[0],
number))
...
if (search(&twoByFour[0][0],
sizeof twoByFour / sizeof *twoByFour,
sizeof twoByFour[0] / sizeof *twoByFour[0],
number))
...
}
In this case, we explicitly pass a pointer to the first element in each array, so instead of receiving a pointer to an array of int
, search
receives a simple pointer to int
, which we treat as a 1D array, and compute the offset manually as i * cols + j
. This code assumes that all elements in the 2D array are contiguous.
Edit
Note that in C99, you can have what are called Variable Length Arrays (VLAs), where the array dimension can be specified by a runtime variable rather than a constant expression; this allows us to write the prototype as
int search(size_t rows, size_t cols, int arr[rows][cols], int x)
and not mess with pointers.
精彩评论