# GP2X[c Coding] How Many Of The Same Value Are

#### Pekele

##### Still Fresh
hi there, it's been a long time !

After some stuff under GP32, i'm coming back for the GP2X

i'm looking of a way of returning the number of the same value joined in an array.
I don't want to use a recursive fonction because of the stack overheap.
Here is my C code trying to do that with loop but something is missing.
Any ideas ?

Code:
``````#include <stdio.h>
#include <math.h>

int  mur[14][14],i,j;
int somme=0;
int x=0; int y=0;

int main (int argc, const char * argv[]) {
// insert code here...
printf("retourne le nombre de valeur adjacente dans un tableau\n");
int ligne, colonne;
//remplissage du tableau
for ( i=0;i<15;i++)
for (j=0;j<15;j++)
mur[i][j]=rand()%5;
//affichage du tableau
for ( i=0;i<15;i++)
for (j=0;j<15;j++)
printf("%i\t",mur[i][j]);

while(ligne !=16)
{
printf("\nligne:");
scanf("%i",&ligne);
printf("\ncolone:");
scanf("%i",&colonne);

printf("%d",mur[ligne][colonne],"\n");
printf("\n");

test(ligne,colonne,mur[ligne][colonne]);
printf("\nest présent :%i",somme);
}
}

test ( int ligne, int colonne, int valeur )
{
for ( i=0;i<15;i++){
for (j=0;j<15;j++){
if (mur[ligne+x][colonne]==valeur){x++;somme++;}
if (mur[ligne-x][colonne]==valeur){x++;somme++;}
if (mur[ligne][colonne+y]==valeur){y++;somme++;}
if (mur[ligne][colonne-y]==valeur){y++;somme++;}
}
}
return somme;
}``````

here the result. As you can see it's not working.

#### mrsnature

##### Member
can you be more specific as to what your program is supposed to do? i don't quite get it...

#### mrsnature

##### Member
but firstly your array is initialised to hold only 14x14 elements and you are storing 15x15:

Pekele posted on Oct 30 2005 at 10:41 AM said:
int  mur[14][14],i,j;

for ( i=0;i<15;i++)
for (j=0;j<15;j++)
mur[j]=rand()%5;
//affichage du tableau
for ( i=0;i<15;i++)
for (j=0;j<15;j++)
printf("%i\t",mur[j]);

Last edited by a moderator:

#### Feeblez

##### Member
Trying to work out what that code is supposed to be doing is quitre frankly frying my brain!

But i can spot a potential mistake:

if (mur[ligne-x][colonne]==valeur){x++;somme++;}

If you do this when ligne == 0 and x > 0 you are going to go out of bounds of the array. That is just 1 example, whatever you are doing looks very risky!

Also it makes more sense if that line is written:

if (mur[ligne][colonne-x]==valeur){x++;somme++;}

maybe look at this:

http://en.wikipedia.org/wiki/Flood_fill

#### fishybawb

##### Hired Geek
Also the "ligne" variable is used without being initialised.

#### Pekele

##### Still Fresh
I think the flood fill algorithme is what i was looking for. I'm going to work around this. I should have thought about this....

My program just attemp to return the number of equal value connect to each other. I dunno how to be more clear, sorry.

I know this code is ugly i wrote it too fastly. It is not an good excuse, i know...
of course the array must be of 15 elements. I'm a silly head !

thanks again

#### Pekele

##### Still Fresh
ok guys, the flood fill algorithme seems to work. Some minor change to do and it should be ok. Thanks for the help.

Replies
3
Views
1K
Replies
58
Views
33K
Replies
1
Views
2K
Reesy
R
Replies
8
Views
1K
Replies
1
Views
1K