Hello all!

I don't know what is wrong with my program. I know i'm missing something but still i can't figure it out.


#define TRUE 1
#define FALSE 0

int search2Darray(int x)
{
int i, j;

for (i =0; i< 2; i++)
for (j=0; j < 10; j++)
if (a [j] == x)
return (TRUE);
return(FALSE);
}


#include <stdio.h>

int a[2] [10] = {49,28,32,43,12,23,57,98,2,20,11,33,47,87,90,17,99,4,30,19};
int main ()
{
if (search2Darray(33))
printf("33 is found in the array.\n");
else
printf("33 is not found in the array.\n");
getchar();
}

I find that you get more responses when you use the code blocks.

Also try to indent your code.

#define TRUE 1
#define FALSE 0

int search2Darray(int x)
{
int i, j;

for (i =0; i< 2; i++)
for (j=0; j < 10; j++)
if (a[i] [j] == x) 
return (TRUE);
return(FALSE);
}


#include <stdio.h>

int a[2] [10] = {49,28,32,43,12,23,57,98,2,20,11,33,47,87,90,17,99,4,30,19};
int main ()
{
if (search2Darray(33))
printf("33 is found in the array.\n");
else
printf("33 is not found in the array.\n");
getchar();
}

Edited 5 Years Ago by coolbeanbob: n/a

You need to move line 18 to line 3.

As you have it above, the array is defined after the search2Darray function. This is not good since the function uses the array.

I would also move line 16 above line 4.

Edited 5 Years Ago by coolbeanbob: n/a

This is better.

#define TRUE 1
#define FALSE 0
#include <stdio.h>

int a[2] [10] = {{49,28,32,43,12,23,57,98,2,32},
                {49,28,32,43,12,23,57,98,2,33}};


int search2Darray(int x)
{
    int i, j;

    for (i = 0; i < 2; i++)
    {
        for (j=0; j < 10; j++)
        {
            if (a[i] [j] == x)
            {
                return (TRUE);
            }
        }
    }
}

int main ()
{

    if (search2Darray(33))
    {
        printf("33 is found in the array.\n");
    }
    else
    {
        printf("33 is not found in the array.\n");
        getchar();
    }
}

So I consider. Thanks a bunch. I really appreciate your immediate reply.

This article has been dead for over six months. Start a new discussion instead.