Thread: Compare Arrays

  1. #16
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    No i sort of generate them from a couple of other matrices. Some source code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    int smallsquarefinal[9][9][9];
    int x, y, z;
    int a, b;
    int possarray[9][9];
    int i;
    int size_of_disjoint_subset, count_disjoint_subset_row;
    int location[9] = {0};
    int row = 0;
    int col = 0;
    int secondary_x, secondary_y, location_count;
    
    FILE *disjointsubsetoutput;
    FILE *possarrayoutput;
    
    if ((disjointsubsetoutput = fopen("disjointsubsetoutput", "w")) == NULL)
               fprintf(stderr, "Cannot open %s\n", "disjointsubsetoutput");
    	   
    for (x = 0; x < 9; x++)
    {
    	for (y = 0; y < 9; y++)
    	{
    	fprintf(disjointsubsetoutput, "\n[%d][%d] ", x, y);
    		for (z = 0; z < 9; z++)
    		{
    		smallsquarefinal[x][y][z] = z + 1;
    		fprintf(disjointsubsetoutput, "%d", smallsquarefinal[x][y][z]);
    		}
    	}
    }
    
    if ((possarrayoutput = fopen("possarrayoutput", "r")) == NULL)
               fprintf(stderr, "Cannot open %s\n", "possarrayoutput");
    
    for (x = 0; x <= 8; x++)
    {
    	char buf[10];
    	fscanf(possarrayoutput, "%s", buf);
    	for (y = 0; y <= 8; y++)
    	{
    	possarray[x][y] = buf[y]-'0';
    	printf("%d ", possarray[x][y]);
    	}
    printf("\n");
    }
    fclose(possarrayoutput);
    
    fprintf(disjointsubsetoutput, "\nThese are the arrays to be compared (seperated by newlines):");
    for (size_of_disjoint_subset = 2; size_of_disjoint_subset < 9; size_of_disjoint_subset++)
    {
    	for (row = 0; row < 9; row++)
    	{
    	count_disjoint_subset_row = 0;
    		for (col = 0; col < 9; col++)
    		{
    		location[col] = 0;
    			if (possarray[row][col] != 0)
    			{
    				if(possarray[row][col] == size_of_disjoint_subset)
    				{
    				count_disjoint_subset_row++;
    				location[col] = col + 1;
    				}
    			}
    		}
    		
    		if (count_disjoint_subset_row != 0)
    		{
    		printf("\nsubset = %d, count = %d, row = %d, location = ", size_of_disjoint_subset, count_disjoint_subset_row, row);
    			for (secondary_y = 0; secondary_y < 9; secondary_y++)
    			{
    				if(location[secondary_y] != 0)
    				{
    				printf("%d", location[secondary_y]);
    				}
    			}
    		
    			if (count_disjoint_subset_row >= size_of_disjoint_subset)
    			{
    			int disjoint_subset_row_array[count_disjoint_subset_row][9];
    			int no_of_rows_count = 0;
    			printf(" *");
    			// At this point we have the following information:
    			// The size of the subset "size_of_disjoint_subset"
    			// How many occurences of the subset there are in said row "coint_disjoint_subset_row"
    				// (This is greater than or equal to the subset size)
    			// The corresponding row "row"
    			// The locations of each of the possibility sets equal to the subset size "location[]"
    				//fprintf(disjointsubsetoutput, "\n");
    				for (y = 0; y < 9; y++)
    				{
    					for (location_count = 0; location_count < 9; location_count++)
    					{
    						if(location[location_count] != 0 && location[location_count] - 1 == y)
    						{
    						printf("%d", no_of_rows_count);
    						printf("[%d][%d] = ", row, y);
    							for (z = 0; z < 9; z++)
    							{
    							disjoint_subset_row_array[no_of_rows_count][z] = smallsquarefinal[row][y][z];
    							}
    						no_of_rows_count++;
    						}
    					}
    				}
    				
    				for (a = 0; a < count_disjoint_subset_row; a++)
    				{
    				fprintf(disjointsubsetoutput, "\n");
    					for (b = 0; b < 9; b++)
    					{
    					fprintf(disjointsubsetoutput, "%d", disjoint_subset_row_array[a][b]);
    					}
    				}
    			}
    		}
    	fprintf(disjointsubsetoutput, "\n");
    	}
    }
    
    
    printf("\n");
    fclose(disjointsubsetoutput);
    }
    That file is self sufficient and is just to test my code. Part of the out put from the program looks like this:

    Code:
    123456789
    123456789
    
    
    123456789
    123456789
    123456789
    123456789
    
    123456789
    123456789
    123456789
    
    123456789
    123456789
    123456789
    
    
    
    123456789
    123456789
    123456789
    
    123456789
    123456789
    123456789
    123456789
    
    
    123456789
    123456789
    123456789
    123456789
    And its these arrays which need to be compared. (Obviously they are all equal as i set them all for testing purposes)

  2. #17
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Oh sorry, except that it reads this file in called "possarrayoutput"
    Code:
    430430544
    003343346
    200503303
    540302330
    534000424
    044200033
    403304003
    343030300
    444020032

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  4. How do I compare two arrays?
    By lime in forum C Programming
    Replies: 6
    Last Post: 07-13-2003, 09:55 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM