``````Hi everyone - I hope your day is going great.

Can someone please kindly help me with the coding of the following function -
I have absolutely no clue where to start -

Given the following sets of numbers -
1154    1179    2154    2554    2484    2144    4515   1144   1517  4815  1481

Given the Index number of 1154

I want to search the numbers for the Index number of 1154

The search will return a True if I can find 3 or 4 same digits between the Index number and the 8 numbers

The search also have the following criteria -

meaning that -

1154 when compared to 1154 == true
1154 when compared to 1179 == false
1154 when compared to 2154 == true
1154 when compared to 2554 == false
1154 when compared to 2484 == false
1154 when compared to 2144 == false
1154 when compared to 4515 == true
1154 when compared to 1144 == true
1154 when compared to 1517 == true
1154 when compared to 4815 == true
1154 when compared to 1481 == true

the index number can also be of type - 1234, 1123, 1112, 1111

:)

Thank You
``````

So create some function (fun(a, b)) such that,

1) 'a' creates a unique array of the input, bool[10], marking which digits exist
2) 'b' creates a unique array of the input, bool[10], which must have at least 3 elements in common
3) returns true if UNION(array_a, array_b) contains at least …

## All 4 Replies

So create some function (fun(a, b)) such that,

1) 'a' creates a unique array of the input, bool[10], marking which digits exist
2) 'b' creates a unique array of the input, bool[10], which must have at least 3 elements in common
3) returns true if UNION(array_a, array_b) contains at least 3 elements
4) returns false otherwise

There are plenty of other ways to do this, I'm not saying this is the only way.
If you are given integers instead of strings, you'll want to find each digit individually,
that means you'll want to take the original integer 'a' and a while loop, eg:
`while( tmp_a ) bool[ tmp_a % 10 ] = true; tmp_a /= 10;`
Or some such...

In the case that the uniqueness of the digit doesn't matter, you'll want to use int[10] instead of bool[10] and count the number of occurences.
`int[tmp_a%10]++;`

``````Sorry, Here is what I have done so far ...

Somehow the Strncmp() Function is not working ...

Any help will be well appreciated ...

Thank You ...

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
#include <string.h>

/*start bubbleSort*/
void bubbleSort(int *array, int length)
{
int i, j, temp;
int test; /*use this only if unsure whether the list is already sorted or not*/
for(i = length - 1; i > 0; i--)
{
test=0;
for(j = 0; j < i; j++)
{
if(array[j] > array[j+1]) /* compare neighboring elements */
{
temp = array[j]; /* swap array[j] and array[j+1] */
array[j] = array[j+1];
array[j+1] = temp;
test=1;
}
} /*end for j*/
if(test==0) break; /*will exit if the list is sorted!*/
} /*end for i*/
}
/*end bubbleSort*/

void main(void)
{
FILE *fileptr;

int FourDDatabaseX[120][4];
int FourDRows, FourDCols;

int char1, char2, char3, charx;

int counter1, counter2, counter3;

int s, t, u, v, w, x, y, z;
int a, b, c, d, e, f, g, h, i, j, k, l, m;

int FourDVal1 ,FourDVal2, FourDVal3, FourDVal4;
int FourDValX[4];
int FourD1[5];
int FourD2[5];
int FourD1X;
int FourD2X;

char FourD1char[8];
char FourD2char[8];
char Tempbuff[8];

FourDRows=120;
FourDCols=4;

//FourD1char = (char*)malloc(sizeof(char)*8);
//FourD2char = (char*)malloc(sizeof(char)*8);

for(x=0;x!=120;x++)
for(y=0;y!=4;y++)
FourDDatabaseX[x][y]=0;

fileptr=fopen("4D Database-X.txt", "r");

x=0;
y=0;
// copy all the moses database into the 2D array moses_db[x][y]
while(!feof(fileptr))
{
fscanf(fileptr,"%1d%1d%1d%1d    ",&FourDVal1,&FourDVal2,&FourDVal3,&FourDVal4);

FourDDatabaseX[x][0]=FourDVal1;
FourDDatabaseX[x][1]=FourDVal2;
FourDDatabaseX[x][2]=FourDVal3;
FourDDatabaseX[x][3]=FourDVal4;
x++;
y++;
if(y==10)
{
fscanf(fileptr,"\n");
y=0;
}
}

fclose(fileptr);

//for(x=0;x!=120;x++) printf("[%d%d%d%d]\n",FourDDatabaseX[x][0],FourDDatabaseX[x][1],FourDDatabaseX[x][2],FourDDatabaseX[x][3]);
//exit(0);

for(s=0;s!=120;s++)
{

FourD1[0]=FourDDatabaseX[s][0];
FourD1[1]=FourDDatabaseX[s][1];
FourD1[2]=FourDDatabaseX[s][2];
FourD1[3]=FourDDatabaseX[s][3];

bubbleSort(FourD1,4);

//printf("%d%d%d%d", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );

for(t=0;t!=120;t++)
{
FourD2[0]=FourDDatabaseX[t][0];
FourD2[1]=FourDDatabaseX[t][1];
FourD2[2]=FourDDatabaseX[t][2];
FourD2[3]=FourDDatabaseX[t][3];

bubbleSort(FourD2,4);

//printf("D1 %d%d%d%d ", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );
//printf("D2 %d%d%d%d\n", FourD2[0], FourD2[1], FourD2[2], FourD2[3] );

FourD1X = (FourD1[0]*1000) + (FourD1[1]*100) + (FourD1[2]*10) + (FourD1[3]);
FourD2X = (FourD2[0]*1000) + (FourD2[1]*100) + (FourD2[2]*10) + (FourD2[3]);

//printf("FourD2X %d\n",FourD2X);

sprintf(FourD1char,"%d",FourD1X);
sprintf(FourD2char,"%d",FourD2X);

if(strlen(FourD2char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

if(strlen(FourD2char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}
if(strlen(FourD2char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

//printf("[%s]",FourD2char);

//if((FourD1[0]==FourD2[0])&&(FourD1[1]==FourD2[1])&&(FourD1[2]==FourD2[2])&&(FourD1[3]==FourD2[3]))
//  printf("FOUND[%d%d%d%d]", FourDDatabaseX[s][0], FourDDatabaseX[s][1], FourDDatabaseX[s][2], FourDDatabaseX[s][3]);

//if(strncmp(FourD1char,FourD2char,4)==0)
printf("[strncmp %d]",strncmp(FourD1char,FourD2char,4));
{
printf("[%s][%s]\n", FourD1char, FourD2char);

}

}
printf("\n");
}

}
``````

Geez it really seems like you're overcomplicating the task, would you mind showing me the true file format of "4D database.txt"?

I'm not really a fan of your general approach to the problem, but I also don't want to tell you how to think.

To start with, can you demonstrate that the strings "FourD1char" and "FourD2char" are exactly what you expect? I see that you have print functions, but you didn't include any sample output here. I'm just some random stranger, so I need to have a little context in order to help you. =)

``````I finally got it working now

Thank You very much for your time and help !!!

:D

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
#include <string.h>

int count_chars(const char* string, char ch)
{
int count = 0;
int i;

// We are computing the length once at this point
// because it is a relatively lengthy operation,
// and we don't want to have to compute it anew
// every time the i < length condition is checked.
int length = strlen(string);

for (i = 0; i < length; i++)
{
if (string[i] == ch)
{
count++;
}
}

return count;
}

void main(void)
{
FILE *fileptr;

int FourDDatabaseX[120][4];
int FourDRows, FourDCols;

int char1, char2, char3, charx;

int counter1, counter2, counter3, counter4, counter5, counter6, counter7, counter8;

int counterX[8],counterY[8];

int s, t, u, v, w, x, y, z;
int a, b, c, d, e, f, g, h, i, j, k, l, m;

int FourDVal1 ,FourDVal2, FourDVal3, FourDVal4;
int FourDValX[4];
int FourD1[5];
int FourD2[5];
int FourD1X;
int FourD2X;

char FourD1char[8];
char FourD2char[8];
char Tempbuff[8];

FourDRows=120;
FourDCols=4;

//FourD1char = (char*)malloc(sizeof(char)*8);
//FourD2char = (char*)malloc(sizeof(char)*8);

for(x=0;x!=120;x++)
for(y=0;y!=4;y++)
FourDDatabaseX[x][y]=0;

fileptr=fopen("4D Database-X.txt", "r");

x=0;
y=0;
// copy all the moses database into the 2D array moses_db[x][y]
while(!feof(fileptr))
{
fscanf(fileptr,"%1d%1d%1d%1d    ",&FourDVal1,&FourDVal2,&FourDVal3,&FourDVal4);

FourDDatabaseX[x][0]=FourDVal1;
FourDDatabaseX[x][1]=FourDVal2;
FourDDatabaseX[x][2]=FourDVal3;
FourDDatabaseX[x][3]=FourDVal4;
x++;
y++;
if(y==10)
{
fscanf(fileptr,"\n");
y=0;
}
}

fclose(fileptr);

//for(x=0;x!=120;x++) printf("[%d%d%d%d]\n",FourDDatabaseX[x][0],FourDDatabaseX[x][1],FourDDatabaseX[x][2],FourDDatabaseX[x][3]);
//exit(0);

for(s=0;s!=120;s++)
{

FourD1[0]=FourDDatabaseX[s][0];
FourD1[1]=FourDDatabaseX[s][1];
FourD1[2]=FourDDatabaseX[s][2];
FourD1[3]=FourDDatabaseX[s][3];

//printf("%d%d%d%d", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );

for(t=0;t!=120;t++)
{
FourD2[0]=FourDDatabaseX[t][0];
FourD2[1]=FourDDatabaseX[t][1];
FourD2[2]=FourDDatabaseX[t][2];
FourD2[3]=FourDDatabaseX[t][3];

//printf("D1 %d%d%d%d ", FourD1[0], FourD1[1], FourD1[2], FourD1[3] );
//printf("D2 %d%d%d%d\n", FourD2[0], FourD2[1], FourD2[2], FourD2[3] );

FourD1X = (FourD1[0]*1000) + (FourD1[1]*100) + (FourD1[2]*10) + (FourD1[3]);
FourD2X = (FourD2[0]*1000) + (FourD2[1]*100) + (FourD2[2]*10) + (FourD2[3]);

//printf("FourD2X %d\n",FourD2X);

sprintf(FourD1char,"%d",FourD1X);
sprintf(FourD2char,"%d",FourD2X);

if(strlen(FourD1char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD1char);
strcpy(FourD1char,Tempbuff);
}

if(strlen(FourD1char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD1char);
strcpy(FourD1char,Tempbuff);
}
if(strlen(FourD1char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD1char,Tempbuff);
}

if(strlen(FourD2char)==3)
{
strcpy(Tempbuff,"0");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

if(strlen(FourD2char)==2)
{
strcpy(Tempbuff,"00");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}
if(strlen(FourD2char)==1)
{
strcpy(Tempbuff,"000");
strcat(Tempbuff,FourD2char);
strcpy(FourD2char,Tempbuff);
}

//int count_chars(const char* string, char ch)

//printf("FourD1X %d\n",FourD1X);
//printf("FourD2X %d\n",FourD2X);

counter1=count_chars(FourD1char,FourD1char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD1char,FourD1char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD1char,FourD1char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD1char,FourD1char[3]);
//printf("Counter4 %d\n",counter4);

counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;

counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==1)
counter6++;
}

if(counter6==4)
{
counter1=count_chars(FourD2char,FourD2char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD2char,FourD2char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD2char,FourD2char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD2char,FourD2char[3]);
//printf("Counter4 %d\n",counter4);

counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;

counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==1)
counter6++;
}

if(counter6==4)
{
counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);

counter5=counter1+counter2+counter3+counter4;

if(counter5 >= 3)
{
//printf("c %d ",counter5);
printf("[%s][%s] ",FourD1char,FourD2char);
}

}

}
else if((counter1==3)||(counter2==3)||(counter3==3)||(counter4==3))
{
if((counter1+counter2+counter3+counter4) >= 7)
printf("[%s][%s] ",FourD1char,FourD2char);
}
else
{

//printf("[%s] ",FourD2char);
/*
counter1=count_chars(FourD2char,FourD2char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD2char,FourD2char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD2char,FourD2char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD2char,FourD2char[3]);
//printf("Counter4 %d\n",counter4);
*/

counter1=count_chars(FourD1char,FourD1char[0]);
//printf("Counter1 %d ",counter1);
counter2=count_chars(FourD1char,FourD1char[1]);
//printf("Counter2 %d ",counter2);
counter3=count_chars(FourD1char,FourD1char[2]);
//printf("Counter3 %d ",counter3);
counter4=count_chars(FourD1char,FourD1char[3]);
//printf("Counter4 %d\n",counter4);

counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;

counter6=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
counter6++;
}

if(counter6==2)
{
counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);

counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;

counter6=0;
counter7=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
{
counter6++;
}
else if(counterX[counter5]==1)
{
counter7++;
}
}

if(counter6 >=2)
{
if(counter7 >=2)
{
printf("[%s][%s] ",FourD1char,FourD2char);
//printf("Counter %d ",counter6);
}
}

}
else if(counter6==4)
{

counter1=count_chars(FourD2char,FourD1char[0]);
//printf("Counter1 %d",counter1);
counter2=count_chars(FourD2char,FourD1char[1]);
//printf("Counter2 %d",counter1);
counter3=count_chars(FourD2char,FourD1char[2]);
//printf("Counter3 %d",counter1);
counter4=count_chars(FourD2char,FourD1char[3]);
//printf("Counter4 %d\n",counter1);

counterX[0]=counter1;
counterX[1]=counter2;
counterX[2]=counter3;
counterX[3]=counter4;

counter6=0;
counter7=0;
for(counter5=0;counter5!=4;counter5++)
{
if(counterX[counter5]==2)
{
counter6++;
}
/*
else if(counterX[counter5]==1)
{
counter7++;
}*/
}

if(counter6 >=4)
{
//if(counter7 >=2)
{
printf("[%s][%s] ",FourD1char,FourD2char);
//printf("Counter %d ",counter6);
}
}

}

}
}
printf("\n");
}

}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.