NOTE: This code cannot use array indices [ ] [ ]. Only pointers can be used. [ ] [ ] are okay to use only for array declaration.
I am comparing an array of same size as of array image called copyArray and array image itself. First I initialize the copyArray with all 0s using pointers which I checked using printf and works correctly.
function colorPtr simply checks for all the 1s in array image and calls function distinctCountPointer. function distinctCountPointer checks for all 1s that are connected(from up, down, left, right or diagonal) to each other and groups them by giving a unique integer. (See output(that should be) for a clear idea).
I was able to do this code without using pointers, but I'm having trouble using pointers. Any help would be appreciated.
void colorPtr(int* image){
int copyArray[IMAGE_SIZE][IMAGE_SIZE];
int i, j;
for(i=0; i<IMAGE_SIZE; i++)
for(j=0; j<IMAGE_SIZE; j++){
*(*(copyArray + i) + j) = 0;
//printf("%d ", *(*(copyArray +i) + j));
}
// going to all adjacent positions and running distinctCount function
int counter = 1;
for(i=0;i<IMAGE_SIZE;i++){
for(j=0;j<IMAGE_SIZE;j++){
if(*(copyArray + i * IMAGE_SIZE + j)==0 && *(image + i * IMAGE_SIZE + j)!=0){ //error line
distinctCountPointer(image, i, j, copyArray, counter);
counter++;
}
}
}
}
function colorPtr then calls distinctCountPointer. This function simply gives 1s that are connected to each other from up, down, left, right and diagonals with a unique number.
void distinctCountPointer(int * image, int i, int j, int * copyArray, int counter){
*(copyArray + i*10 + j) = 1;
*(image + i*10 + j) = counter;
int new_i, new_j;
for(new_i =-1;new_i <2;new_i ++){
for(new_j = -1;new_j < 2; new_j++){
if(new_i != 0 || new_j != 0){
if(checkIndex(i+new_i, j+new_j)){
if(if(*(copyArray + i * IMAGE_SIZE + j)==0 && *(image + i * IMAGE_SIZE + j)!=0){
distinctCountPointer(image, i+new_i , j+new_j, copyArray, counter);
}
}
}
}
}
}
void printImgArray(int array[IMAGE_SIZE][IMAGE_SIZE])
{
printf("------ Image Contents -------\n");
int i, j;
for (i=0; i<IMAGE_SIZE; i++)
{
for (j=0; j<IMAGE_SIZE; j++)
printf("%02d, ",array[i][j]);
printf("\n");
}
printf("-----------------------------\n");
}
int checkIndex(int i, int j){
return i >= 0 && j>=0 && i< IMAGE_SIZE && j< IMAGE_SIZE;
}
Here is my main.
int main(){
int cellImg_[IMAGE_SIZE][IMAGE_SIZE]={{0,0,1,1,0,0,1,0,0,1},\
{1,0,1,1,0,0,1,1,0,1},\
{1,0,0,1,1,0,1,1,0,1},\
{1,1,0,0,0,0,0,0,0,0},\
{1,0,0,1,1,1,0,0,1,0},\
{0,0,0,0,1,0,0,1,1,0},\
{0,0,1,0,0,1,0,1,0,0},\
{0,0,1,1,0,0,1,0,0,0},\
{0,0,1,0,0,0,0,0,1,1},
{0,1,1,0,0,0,1,1,1,1}};
int* ptr = cellImg_;
printImgArray(ptr);
colorPtr(ptr);
printImgArray(ptr);
count=cellCountPtr(ptr);
printf("Total number of cells in the image: %d\n",count);
}
Sample Input:-
00, 00, 01, 01, 00, 00, 01, 00, 00, 01,
01, 00, 01, 01, 00, 00, 01, 01, 00, 01,
01, 00, 00, 01, 01, 00, 01, 01, 00, 01,
01, 01, 00, 00, 00, 00, 00, 00, 00, 00,
01, 00, 00, 01, 01, 01, 00, 00, 01, 00,
00, 00, 00, 00, 01, 00, 00, 01, 01, 00,
00, 00, 01, 00, 00, 01, 00, 01, 00, 00,
00, 00, 01, 01, 00, 00, 01, 00, 00, 00,
00, 00, 01, 00, 00, 00, 00, 00, 01, 01,
00, 01, 01, 00, 00, 00, 01, 01, 01, 01
Output (shown):-
00, 00, 01, 01, 00, 00, 01, 00, 00, 01,
01, 00, 01, 01, 00, 00, 01, 01, 00, 01,
01, 00, 00, 01, 01, 00, 01, 01, 00, 01,
01, 01, 00, 00, 00, 00, 00, 00, 00, 00,
01, 00, 00, 01, 01, 01, 00, 00, 01, 00,
00, 00, 00, 00, 01, 00, 00, 01, 01, 00,
00, 00, 01, 00, 00, 01, 00, 01, 00, 00,
00, 00, 01, 01, 00, 00, 01, 00, 00, 00,
00, 00, 01, 00, 00, 00, 00, 00, 01, 01,
00, 01, 01, 00, 00, 00, 01, 01, 01, 01
Output(that should be):-
00, 00, 01, 01, 00, 00, 02, 00, 00, 03,
04, 00, 01, 01, 00, 00, 02, 02, 00, 03,
04, 00, 00, 01, 01, 00, 02, 02, 00, 03,
04, 04, 00, 00, 00, 00, 00, 00, 00, 00,
04, 00, 00, 05, 05, 05, 00, 00, 05, 00,
00, 00, 00, 00, 05, 00, 00, 05, 05, 00,
00, 00, 06, 00, 00, 05, 00, 05, 00, 00,
00, 00, 06, 06, 00, 00, 05, 00, 00, 00,
00, 00, 06, 00, 00, 00, 00, 00, 07, 07,
00, 06, 06, 00, 00, 00, 07, 07, 07, 07