CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2005
    Posts
    13

    Cool Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    I hope your day is going well



    I have a question regarding finding similar 4 digit numbers in a pool of 120 numbers

    Below is an linear single dimension array of 4 digits

    There are 120, 4 digit numbers in total

    My question is this - How can I code in C - a function that looks through all the 120, 4 digits to find similar numbers

    Example - 2095 is similar or matches to - 0950, 5095, 5250, 5269 - i.e having 3 of the digits that are the same in the 4

    The code must print out 2095 + all the matched numbers

    If the Matched or Similar numbers are less then a certain number n - that number is discarded and the code should go onto the next number

    2095 0950 5374 1884 8640 3723 3464 8465 2376 8035
    4340 5208 6540 5306 7535 5322 6100 6107 5323 5081
    1160 3465 0840 9698 8091 7671 5645 7655 2067 6974
    2398 5095 2330 7591 8084 6421 5318 6491 4098 2136
    2388 0150 3021 7584 3110 6484 4010 5207 9634 5135
    5376 5651 5580 6436 0923 3036 6745 5369 6923 2311
    4351 5191 4931 0340 6209 1861 5194 3247 4465 5250
    5330 6340 5245 0976 5310 5388 2380 3180 6976 5623
    2186 6774 4021 2394 3674 6851 9636 6175 3655 6706
    5274 2395 4981 4534 0910 5206 0118 4995 2165 5275
    6351 0911 5394 4834 0035 4521 2360 0980 5395 5218
    4835 4060 6130 5276 7691 6188 5611 3767 7684 5269


    I have written some code below but it is not working ...

    Thank you




    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include <dos.h>
    
    
    FILE *fileptr;
    FILE *fileptr1;
    FILE *fileptr2;
    FILE *fileptr3;
    int **numberX1;
    int RowX, ColX;
    
    
    int intA, intB, intC, intD, intE, intF, intG, intH;
    int FourD_Array1[25][5];
    int FourD_Array2[25][5];
    void main(void)
    {
    
    
    	char buffx[20];
    
    	int x, y, z;
    	int a, b, c, d, e;
    	int charx;
    	
    	int dbVal1 ,dbVal2, dbVal3, dbVal4, dbVal5, dbVal6, dbVal7, dbVal8;
    	int dbValx[10];
    	int dbVala1[10];
    	int dbValx1[10];
    
    	int NumX[10];
    
    
    
    	fileptr1 = fopen("4D-Database-1.txt", "r");
    	fileptr3 = fopen("4D-Database-A.txt", "w");
    
    	fscanf(fileptr1, "%1d%1d%1d%1d	", &dbVal1, &dbVal2, &dbVal3, &dbVal4);
    	//fprintf(fileptr2, "[%d%d%d%d]", dbVal1, dbVal2, dbVal3, dbVal4);
    	
    
    	a = 0;
    	b = 0;
    	dbVal5 = dbVal1;
    	dbVal6 = dbVal2;
    	dbVal7 = dbVal3;
    	dbVal8 = dbVal4;
    	while (!feof(fileptr1))
    	{
    		fileptr2 = fopen("4D-Database-2.txt", "r");
    
    		while (!feof(fileptr2))
    		{
    
    			fscanf(fileptr2, "%1d%1d%1d%1d	", &dbVal1, &dbVal2, &dbVal3, &dbVal4);
    
    			if (dbVal1 == dbVal5)
    				a++;
    			if (dbVal1 == dbVal6)
    				a++;
    			if (dbVal1 == dbVal7)
    				a++;
    			if (dbVal1 == dbVal8)
    				a++;
    
    			if (dbVal2 == dbVal5)
    				a++;
    			if (dbVal2 == dbVal6)
    				a++;
    			if (dbVal2 == dbVal7)
    				a++;
    			if (dbVal2 == dbVal8)
    				a++;
    
    			if (dbVal3 == dbVal5)
    				a++;
    			if (dbVal3 == dbVal6)
    				a++;
    			if (dbVal3 == dbVal7)
    				a++;
    			if (dbVal3 == dbVal8)
    				a++;
    
    			if (dbVal4 == dbVal5)
    				a++;
    			if (dbVal4 == dbVal6)
    				a++;
    			if (dbVal4 == dbVal7)
    				a++;
    			if (dbVal4 == dbVal8)
    				a++;
    
    			if (a >= 3)
    			{
    				a = 0;
    				b++;
    			}
    			else
    			{
    				fprintf(fileptr3, "[%2d]\n", b);
    				fprintf(fileptr3, "[%1d%1d%1d%1d]", dbVal1, dbVal2, dbVal3, dbVal4);
    				dbVal5 = dbVal1;
    				dbVal6 = dbVal2;
    				dbVal7 = dbVal3;
    				dbVal8 = dbVal4;
    				a = 0;
    				b = 0;
    			}
    		}
    		fclose(fileptr2);
    	}
    	fprintf(fileptr2, "[%2d]\n", b);
    	//fprintf(fileptr2, "[%1d%1d%1d%1d]", dbVal1, dbVal2, dbVal3, dbVal4);
    	fclose(fileptr1);
    	
    }
    Last edited by VENDETTA; June 6th, 2014 at 04:54 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Please, edit your post adding Code tags. Otherwise your code is absolutely unreadable.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2005
    Posts
    13

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by VictorN View Post
    Please, edit your post adding Code tags. Otherwise your code is absolutely unreadable.
    Hello VictorN,

    So Sorry ...

    How can I do that ?

    I am a total Newbie


  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by VENDETTA View Post
    Hello VictorN,

    So Sorry ...

    How can I do that ?

    I am a total Newbie

    After nine years and ten post are you still "Newbie"?
    Well, then begin with the reading the Announcement: Before you post....
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    I have written some code below but it is not working ...
    Define 'not working'. What debugging of the code have you done? Where does the program execution deviate from your design?

    As Victor says, please use code tags. Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Aug 2013
    Posts
    55

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by VENDETTA View Post
    I have a question regarding finding similar 4 digit numbers in a pool of 120 numbers
    I hate reading other people's code so I just tell you how I would solve this.

    I would introduce an alternative representation of a number, lets call it a Digit Frequency Table, a DFT.

    A DFT basically would be a vector with one entry per digit and for each digit it would store how many times that digit appears in a number.

    So any number can be converted to its DFA in a function say toDFA.

    And DFA's can be compared for equality in a function called DFAcompare. If the result is 0 the numbers are equal. If the result is 1 they differ by 1 digit, if it's 2 they differ by 2, etcetera.

    On top of my head, as a first approach, I'd use an std::vector for the DFA and the number of digits is a constant like,

    const int DIGITS = 10;
    typedef vector<int> DFA;

    Then toDFA would look like this,

    Code:
    DFA toDFA(int x) {
        DFA dfa(DIGITS);
        for (int i=0; i<DIGITS; ++i) dfa[i] = 0; // all counts are initially 0
        while (x != 0) { // all digits
             int d = x % DIGITS; // get digit
    	 dfa[d] += 1; // increment count for this digit
    	 x = x / DIGITS; // remove digit
        }
        return dfa;
    }
    and DFAcompare would look like this,

    Code:
    int DFAcompare(const DFA& a, const DFA& b) {
        int c = 0;
        for (int i=0; i<DIGITS; ++i) {
           int d = a[i] - b[i];
           c += ((d>=0) ? d : -d);  // c += abs(d)
        }
        return c;
    }
    So turn numbers into their DFA representation and compare them using these functions. If DFAcompare is 0 they're equal, if it's 1 they differ by one digit, etcetera.

    As a sidenote, DFAcompare produces the most reasonable result of comparision cases you haven't even dreamed about yet.
    Last edited by zizz; June 6th, 2014 at 04:33 PM.

  7. #7
    Join Date
    Mar 2005
    Posts
    13

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by zizz View Post
    I hate reading other people's code so I just tell you how I would solve this.

    I would introduce an alternative representation of a number, lets call it a Digit Frequency Table, a DFT.

    A DFT basically would be a vector with one entry per digit and for each digit it would store how many times that digit appears in a number.

    So any number can be converted to its DFA in a function say toDFA.

    And DFA's can be compared for equality in a function called DFAcompare. If the result is 0 the numbers are equal. If the result is 1 they differ by 1 digit, if it's 2 they differ by 2, etcetera.

    On top of my head, as a first approach, I'd use an std::vector for the DFA and the number of digits is a constant like,

    const int DIGITS = 10;
    typedef vector<int> DFA;

    Then toDFA would look like this,

    Code:
    DFA toDFA(int x) {
        DFA dfa(DIGITS);
        for (int i=0; i<DIGITS; ++i) dfa[i] = 0; // all counts are initially 0
        while (x != 0) { // all digits
             int d = x % DIGITS; // get digit
    	 dfa[d] += 1; // increment count for this digit
    	 x = x / DIGITS; // remove digit
        }
        return dfa;
    }
    and DFAcompare would look like this,

    Code:
    int DFAcompare(const DFA& a, const DFA& b) {
        int c = 0;
        for (int i=0; i<DIGITS; ++i) {
           int d = a[i] - b[i];
           c += ((d>=0) ? d : -d);  // c += abs(d)
        }
        return c;
    }
    So turn numbers into their DFA representation and compare them using these functions. If DFAcompare is 0 they're equal, if it's 1 they differ by one digit, etcetera.

    As a sidenote, DFAcompare produces the most reasonable result of comparision cases you haven't even dreamed about yet.
    Dear Sir,

    THANK YOU Extremely much for your kind time and help to coding the function for my problem.

    I hope you and everyone in the forum have a great day ahead.

    All the Best !!!


  8. #8
    Join Date
    Mar 2005
    Posts
    13

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    I have done some re-working of my code.

    Please see below - some how it is still not working ... it does not handle duplicate numbers too well ...




    File Contents of - 4D-Database-1.txt

    2095 0950 5374 1884 8640 3723 3464 8465 2376 8035 4340 5208 6540 5306 7535 5322 6100 6107 5323 5081 1160 3465 0840 9698 8091 7671 5645 7655 2067 6974 2398 5095 2330 7591 8084 6421 5318 6491 4098 2136 2388 0150 3021 7584 3110 6484 4010 5207 9634 5135 5376 5651 5580 6436 0923 3036 6745 5369 6923 2311 4351 5191 4931 0340 6209 1861 5194 3247 4465 5250 5330 6340 5245 0976 5310 5388 2380 3180 6976 5623 2186 6774 4021 2394 3674 6851 9636 6175 3655 6706 5274 2395 4981 4534 0910 5206 0118 4995 2165 5275 6351 0911 5394 4834 0035 4521 2360 0980 5395 5218 4835 4060 6130 5276 7691 6188 5611 3767 7684 5269


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <memory.h>
    #include <dos.h>
    
    
    FILE *fileptr;
    FILE *fileptr1;
    FILE *fileptr2;
    FILE *fileptr3;
    int **numberX1;
    int RowX, ColX;
    
    
    int intA, intB, intC, intD, intE, intF, intG, intH;
    int FourD_Array1[25][5];
    int FourD_Array2[25][5];
    int FourD_Array3[128][8];
    
    
    void main(void)
    {
    	char buffx[20];
    
    	int x, y, z;
    	int a, b, c, d, e;
    	int charx;
    	
    	int dbVal1 ,dbVal2, dbVal3, dbVal4, dbVal5, dbVal6, dbVal7, dbVal8;
    	int dbValx[10];
    	int dbVala1[10];
    	int dbValx1[10];
    
    	int NumX[10];
    
    	// initialize the moses database array
    	for (x = 0; x != 128; x++)
    	for (y = 0; y != 8; y++)
    		FourD_Array3[x][y] = 0;
    
    	// open the moses database file for reading
    	fileptr = fopen("4D-Database-1.txt", "r");
    
    	y = 0;
    
    	// copy all the moses database into the 2D array moses_db[x][y]
    	for (x = 0; x != 120; x++)
    	{
    		fscanf(fileptr, "%1d%1d%1d%1d	", &dbVal1, &dbVal2, &dbVal3, &dbVal4);
    		FourD_Array3[x][0] = dbVal1;
    		FourD_Array3[x][1] = dbVal2;
    		FourD_Array3[x][2] = dbVal3;
    		FourD_Array3[x][3] = dbVal4;
    
    		//printf("%d%d%d%d\n",dbVal1,dbVal2,dbVal3,dbVal4);
    		/*
    		y++;
    		if(y<97)
    		{
    		charx=fgetc(fileptr);
    		y=0;
    		}
    		*/
    	}
    
    	fclose(fileptr);
    
    	//for (a = 0; a != 120;a++)
    	//		printf("%d%d%d%d\n", FourD_Array3[a][0], FourD_Array3[a][1], FourD_Array3[a][2], FourD_Array3[a][3]);
    	//exit(0);
    
    	fileptr1 = fopen("4D-Database-1.txt", "r");
    	fileptr2 = fopen("4D-Database-A.txt", "w");
    
    	//fscanf(fileptr1, "%1d%1d%1d%1d	", &dbVal1, &dbVal2, &dbVal3, &dbVal4);
    	
    	a = 0;
    	b = 0;
    	c = 0;
    	d = 0;
    	e = 0;
    
    	while (!feof(fileptr1))
    	{
    		fscanf(fileptr1, "%1d%1d%1d%1d	", &dbVal1, &dbVal2, &dbVal3, &dbVal4);
    
    		for (a = 0; a != 120; a++)
    		{
    			c = 0;
    			for (b = 0; b != 4; b++)
    			{
    				if (FourD_Array3[a][b] == dbVal1)
    					c++;
    				if (FourD_Array3[a][b] == dbVal2)
    					c++;
    				if (FourD_Array3[a][b] == dbVal3)
    					c++;
    				if (FourD_Array3[a][b] == dbVal4)
    					c++;
    			}
    
    			if (c >= 3)
    			{				
    				d++;
    				fprintf(fileptr2, "[[%1d%1d%1d%1d]", FourD_Array3[a][0], FourD_Array3[a][1], FourD_Array3[a][2], FourD_Array3[a][3] );
    				fprintf(fileptr2, "[%1d%1d%1d%1d]]  ", dbVal1, dbVal2, dbVal3, dbVal4 );
    			}
    
    		}
    
    		if (d > 0)
    		{
    			fprintf(fileptr2, "[%4d]\n", d);
    			d = 0;
    		}
    	}
    	
    	fclose(fileptr1);
    	fclose(fileptr2);
    }

  9. #9
    Join Date
    Aug 2013
    Posts
    55

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by VENDETTA View Post
    THANK YOU Extremely much for your kind time and help to coding the function for my problem.
    You're wellcome.

    I served the professional approach to your problem on a silver plate and I hope you have a look at it.

  10. #10
    Join Date
    Mar 2005
    Posts
    13

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by zizz View Post
    You're wellcome.

    I served the professional approach to your problem on a silver plate and I hope you have a look at it.
    Dear Sir,

    THANK YOU Again for your kind help ...

    I am backing it up to my 2 external hard disk right this moment just so I do not lose it ...


  11. #11
    Join Date
    Jul 2013
    Posts
    576

    Re: Game of Pick 4 - How to find similar 4 digit numbers in an array of 120 ?

    Quote Originally Posted by zizz View Post
    and DFAcompare would look like this,

    Code:
    int DFAcompare(const DFA& a, const DFA& b) {
        int c = 0;
        for (int i=0; i<DIGITS; ++i) {
           int d = a[i] - b[i];
           c += ((d>=0) ? d : -d);  // c += abs(d)
        }
        return c;
    }
    The approach is nice but there seems to be a small problem. It's easy to see with an example. Say you want to compare 1234 with 5678. If you subtract their DFAs you get

    0: 0
    1: 1
    2: 1
    3: 1
    4: 1
    5: -1
    6: -1
    7: -1
    8: -1
    9: 0

    If you add them using abs() you get 8 differing digits which is obviously wrong because 4 is the maximum digits that can differ. It seems the current implementation adds all digits that differ in BOTH numbers but that wasn't the problem. The problem was to find all digits that differ in the FIRST number compared with the second.

    I suppose the fix is to add positive numbers only. It's just to change one line,
    Code:
          c += ((d>=0) ? d : 0);  // add on d if positive
    Last edited by razzle; June 11th, 2014 at 02:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured