0

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

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by stereomatching
0

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 by coolbeanbob: n/a

0

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 by coolbeanbob: n/a

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.