Hi, i have to create a algorithm called find2D, to find an element x in an n × n array A. The find2D function iterates over the rows of A, and it calls the algorithm findArray shown below, on each one, until x is found or it has searched all rows of A.

int findArray(int A[], int n, int x)
{
for(int i = 0; i < n; i++)
if(A[i] == x)
return i;
else
return -1;
}

This function has to be called in find2D i am not sure how to pass this function to find2D to check each row

5
Contributors
12
Replies
13
Views
8 Years
Discussion Span
Last Post by 3bdelhakeem

Hi gamerchick.

So, what is the problem?

In general, you'd call it in a loop over each "row". You'd pass the parameters: pointer to the row array, its size, and the value you're looking for.

Your finder function needs fixing, though, so it goes through more than one iteration.

And then there is the issue of what you want your 2D function to do. Just return a boolean if the value is found? "Return" the coordinates?

You mean something like this :

bool find2D(int Array[][COL],const int key, const int MaxRow){
bool isFound = false;
for(int row = 0; row != MaxRow; ++row){
if( findArray(Array[row], COL, key){
isFound = true;
break;
}
}
return isFound;
}

ya something like this looks right, im just wondering where u linked the function findArray() in here where does it call it

ya something like this looks right, im just wondering where u linked the function findArray() in here where does it call it

It should be something like this :

bool findKey(const int* Array, const int KeyToFind, const int MaxSize){
size_t pos = 0;
assert( MaxSize >= 0 ); //better be positive or 0
for(; pos != MaxSize; ++pos){
if(Array[pos] == KeyToFind) break;
}
return !(pos == MaxSize); //if found pos will not equal MaxSize
}

Edited by firstPerson: n/a

That function is your findArray. I just didn't want to rewrite your code.

how is writing findArray going to help the situation i need tips on how to write find2D

how is writing findArray going to help the situation i need tips on how to write find2D

I told you , in fact I showed you. Use find1D inside find2D for every
row. If found return true. If end of loop, return false;

int arrayFind(int A[], int n, int x)
{
for(int i = 0; i < n; i++)
if(A == x)
return i;
else
return -1;
}

This function has to be called in find2D i am not sure how to pass this function to find2D to check each row

First:
your arrayFind function is wrong because for e.g:
if you have an array of values {1, 2, 3} and you wanted to search for the number 2 which is the second element of the array, when the function begin with the first element, if it's not the wanted one the function will return -1 ignoring the other elements so your arrayFind function should look like this:

int arrayFind(int A[], int n, int x)
{
for(int i = 0; i < n; i++)
{
if(A[i] == x)
return i;
}

return -1; /* When the loop ends it means that no element matches the wanted one so the function will return -1*/
}

Second:
you have to specify the size of the second dimension of the 2D-Array in the find2D function's argument here's what you probably need:

int find2D(int Arr[][5], int rows, int ToFind)
{
int retval;
for (int r=0 ; r<rows ; ++r)
{
retval = arrayFind(Arr[r], 5, ToFind);
if (retval>=0) return retval;
}

return -1;
}

If you want it to return the number of rows and the number of columns then you have to use pairs or a structure for e.g:

struct Search2D {
int row;
int col;
};

Search2D find2D(int Arr[][5], int rows, int ToFind)
{
Search2D retval;
for (int r=0 ; r<rows ; ++r)
{
retval.col = arrayFind(Arr[r], 5, ToFind);
retval.row = r;
if (retval.col>=0) return retval;
}

retval.row = -1;
retval.col = -1;
return retval;
}
int find2D(int Arr[][5], int rows, int ToFind)
{
int retval;
for (int r=0 ; r<rows ; ++r)
{
retval = arrayFind(Arr[r], 5, ToFind);
if (retval>=0) return retval;
}

return -1;
}

hey this looks, like what i need just wondering if you could tell me what exactly the ToFind variable is though?

Your "efforts" thus far really make you look like a leech. Have you actually written a line of code in all of this?

In general, you'd call it in a loop over each "row". You'd pass the parameters: pointer to the row array, its size, and the value you're looking for.

Your finder function needs fixing, though, so it goes through more than one iteration.

And then there is the issue of what you want your 2D function to do. Just return a boolean if the value is found? "Return" the coordinates?

i dont think you saw my original post

Edited by gamerchick: n/a

In general, you'd call it in a loop over each "row". You'd pass the parameters: pointer to the row array, its size, and the value you're looking for.

Your finder function needs fixing, though, so it goes through more than one iteration.

And then there is the issue of what you want your 2D function to do. Just return a boolean if the value is found? "Return" the coordinates?