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();
}

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.

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.

you don't need to define TRUE and FALSE
c++ already prepare for the built in true and false

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.