Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. int Perfect_Matrix(int **ar, int size); //Checks if the matrix is a perfect matrix.
  4. int main()
  5. {
  6.     int i, j, Redo = 0, Size, *elemnt, **ar; //Initialzing the variables.
  7.     while (Redo < 3) //Runs the main 3 times.
  8.     {
  9.         printf("\nEnter matrix size: ");
  10.         scanf("%d", &Size);
  11.         ar = (int **)malloc(Size * sizeof(int *)); //Matrix allocation.
  12.         elemnt = (int *)malloc(Size * Size * sizeof(int));
  13.         for (i = 0; i < Size; i++)
  14.             ar[i] = elemnt + (i * Size);
  15.         printf("Enter the numbers:\n");
  16.         for (i = 0; i < Size; i++)
  17.         for (j = 0; j < Size; j++)
  18.             scanf("%d", &ar[i][j]);
  19.         if (Perfect_Matrix(ar, Size))
  20.             printf("\nOK!\n");
  21.         else
  22.             printf("\nNo!\n");
  23.         free(ar[0]); //Free the memory that was allocated.
  24.         free(ar);
  25.         Redo++;
  26.     }
  27.     getch();
  28.     return 0;
  29. }
  30. int Perfect_Matrix(int **ar, int size) //Checks if the matrix is a perfect matrix.
  31. {
  32.     int i, j, count = 0;
  33.     for (i = size - 1; i >= 0; i--)
  34.     for (j = 1; j < size; j++)
  35.     {
  36.         if (ar[0][i] == ar[j][i])
  37.             return 0;
  38.         if (ar[size - i - 1][0] == ar[size - i - 1][j])
  39.             return 0;
  40.         if (ar[0][0] == ar[j][i] || ar[0][1] == ar[j][i] || ar[0][2] == ar[j][i] || ar[0][3]==ar[i][j]) //Counts the number of times each number in a Row appears.
  41.             count++;
  42.     }
  43.     if (count == (size-1)*size) //Checks if the number of values is valid for a Perfect Matrix.
  44.         return 1;
  45.     else
  46.         return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement