Gerald Lee
Gerald Lee

Reputation: 21

Finding Duplicate value in 2d array 3x3 magic Square

Im trying to create a 2D array 3x3 square.

Calculation and checking for it is done. Right now, am trying to prevent repeated numbers from happening in the 2d array.

I tried using a for loop to brute force but cygwin compiles gives me error

Below is my code. How do i go about checking 2d array?

    #include <stdio.h> /* printf */

    /*Preprocessor*/
    /*set N constant 3*/
    #define N 3
    typedef enum {false,true}
    bool;

int main()
{
   /*initialize size to 3*/
   int size =3;
   int sum, sum1, sum2,array[N][N];
   int row, col = 0;

   int i, j, duplicate =0;


   /* variable to 
   indicate process reached*/
   int checkf =0;



   /*input into the array of an array*/
   for(row =0; row <size; row++)
   {
     for(col =0; col < size; col++)
     {
       scanf("%d", &array[row][col]);
     }
   }

   **/*check for repeated values*/
   for(row =0; row<9; row++)
  {
    for(col=0; col <9; col++)
    {
      if(array==array[row][col])
    }      
   }**







 /*Diagonal*/
 /*sum of diagonal from the left 
 equals to sum of diagonal 
 from the right */
  /*initialize sum2 to 0*/
   sum2 =0;
   /*check if row less than 3*/
   for(row=0; row< size; row++)
   {
     /*check if col less than 3*/
    for(col=0;col<size; col++)
     {
       /*number of row 
       equals number of col*/
       if(row == col)
       {
       /*addition*/
       sum2 = sum2 + array[row][col]; 
       }
     }
   }

 /*row*/
   /*check if row less than 3*/
   for(row=0; row < size; row++)
   {
     /*initialize sum */
     sum = 0;
     /*check if col less than 3*/
      for(col=0; col <size; col++)
      {
       /*addition of numbers*/
       sum = sum + array[row][col];
      }
     /*check if all additions
     adds up to same sum*/
     if(sum2 == sum)
     {
      /*a flag or check to print*/
      checkf =1;
     }
     else
     {
      /*a flag or check to print*/
      checkf =0;
      break;
     }  
   }

   /*Columns*/

   /*check if row less than 3*/
   for(row = 0; row < size; row++)
   {
     /*initialize sum */
     sum1 =0;
     /*check if col less than 3*/
     for(col = 0; col < size; col++)
     {
       /*addition*/
       sum1 = sum1 + array[col][row];
     }
     /*sum of diagonal equals
     sum of columns*/
     if(sum == sum1)
     {
       /*a flag or check to print*/
       checkf =1;
     }
     else
     {
       /*a flag or check to print*/
       checkf =0;
       break;
     }
   }


   /*if statement is true
   prints and display*/
   if(checkf ==1)
   {
     printf("This is a magic square.\n");
   }
   else
   {
     /*print and display*/
     printf("This is not a magic square.\n");
   }
   return 0;

}

Upvotes: 0

Views: 2569

Answers (1)

M Oehm
M Oehm

Reputation: 29116

If you had a flat, one-dimensional array (of fixed size N, say), you would check all elements against every element to its left:

int unique1d(int array[N])
{
    int i, j;

    for(i = 1; i < N*N; i++) {
        for(j = 0; j < i; j++) {
            if (array[i] == array[j]) return 0;
        }
    }

    return 1;    
}

You can do the same for a two-dimensional array, when you "flatten" the indices first. Say you enumerate the cells of your 3×3 grid with "flat" indices like so:

0   1   2
3   4   5
6   7   8

Then you get:

flat = row * N + col;

and, conversely:

row = flat / N;      // truncating int division gives row
col = flat % N;      // remainder gives column

So a function that tests whether there are duplicates in your N×N grid would look like this:

int unique(int array[N][N])
{
    int i, j;

    for(i = 1; i < N*N; i++) {
        for(j = 0; j < i; j++) {
            if (array[i / 3][i % 3] == array[j / 3][j % 3]) return 0;
        }
    }

    return 1;    
}

I've made that into a separate function, because it makes the code clearer. Not how you don't need a separate flag and a break. You can return the result explicitly as soon as you have found it. (Also, the break would have to break out of a nested loop, but the break keyword in C just jumps out of the inner loop.)

(The rest of your code that checks whether the square is magic or not looks a bit suspicious, too. For example, you should start with the assumption that the square is magic and then set your flag to false if one of the conditions for a magic square doesn't hold. You never have to set that flag back to true again. But that's a topic for another question.)

Edit: In order to explain the function better, here's example client code. This is essentially your program without the magic check. N must be a defined constant, as it is in your program.

int main()
{
    int array[N][N];
    int row, col;

    for(row =0; row < N; row++) {
        for(col =0; col < N; col++) {
            scanf("%d", &array[row][col]);
        }
    }

    if (unique(array)) {
        puts("Values are unique");
    } else {
        puts("There are duplicate values.");
    }

    return 0;
}

Upvotes: 1

Related Questions