2d array filling problem

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

2d array filling problem

Hello everybody,

i wrote a program which find every pixel and check their around. I want to write every checked pixel to 2d array. For this reason i initialized struct and with help of struct 2d array. But when i want to fill 2d array with checked pixels after x=167, the program can not terminate its operation. What is problem?
In advance thank you very much!

typedef unsigned char buftype;
#define NBUF 4104
#define MAXCLU 10000
#define MAXPIX 6000

struct tpix {
unsigned char x,y;
unsigned short z;
};

tpix cluster[MAXCLU][MAXPIX];

void fillcluster(int x, int y) {
const int ncluster=34;
int np[ncluster];
int nn=np[ncluster];

cluster[ncluster][nn].x=x;
cluster[ncluster][nn].y=y;
cluster[ncluster][nn].z=frame[x][y];
printf("%d %d %d \n", cluster[ncluster][nn].x, cluster[ncluster][nn].y, cluster[ncluster][nn].z);
frame[x][y]=0;
np[ncluster]++;
}

void clust(int x, int y) {
int i,j,m;

for (i=-1;i<2;i++) {
for (j=-1;j<2;j++) {
if (frame[x+i][y+j]>0) {
fillcluster(x+i,y+j);
clust(x+i,y+j);
}

}
}

}

analyze_clusters() {

int i,j;
int k,l;

for (i=0;i<258;i++) {
for (j=0;j<258;j++) {
if (frame[i][j]>0) {
clust(i,j);
}
}
}
}

Posts: 2
Joined: Mon Jun 04, 2012 2:10 am

Re: 2d array filling problem

--Please use the code Tags to make the code more readable in the forum.
I didn't check all of your code, but I noticed these two lines:
Code: Select all
`int np[ncluster];int nn=np[ncluster];`

The first line creates an array of size ncluster. But the second line is wrong for two reasons:
1. index ncluster is out of bounds. as ncluster is the size of the array, the highest valid index is (ncluster-1).
2. The array np is not initialized in any way. Taking a value from this array is a pretty much random value.
This said it doesn't seem to be a very good idea to use the value of nn as array index either. Make sure you use valid and useful array indexes everywhere.
Who needs a signature anyway.

exomo

Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm

Re: 2d array filling problem

Hi exomo,

I have 2d array (frame[i][j]). I want to check around every pixel in frame[i][j] and i do this in the function clust(int x, int y). After this i want to fill 2d array (cluster[MAXCLU][MAXPIX]) with these checked pixels. But i do not know how i can fill cluster[MAXCLU][MAXPIX] exactly. Can you help me?Can you advice me other thing?

Posts: 2
Joined: Mon Jun 04, 2012 2:10 am

Re: 2d array filling problem

Sorry, I don't get what exactly you want to do.
What do the clusters represent and how do you check the pixels?
Can you perhaps paint the problem or at least make an example.
Who needs a signature anyway.

exomo

Posts: 894
Joined: Fri Sep 26, 2003 12:30 pm