Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- int Perfect_Matrix(int **ar, int size); //Checks if the matrix is a perfect matrix.
- int main()
- {
- int i, j, Redo = 0, Size, *elemnt, **ar; //Initialzing the variables.
- while (Redo < 3) //Runs the main 3 times.
- {
- printf("\nEnter matrix size: ");
- scanf("%d", &Size);
- ar = (int **)malloc(Size * sizeof(int *)); //Matrix allocation.
- elemnt = (int *)malloc(Size * Size * sizeof(int));
- for (i = 0; i < Size; i++)
- ar[i] = elemnt + (i * Size);
- printf("Enter the numbers:\n");
- for (i = 0; i < Size; i++)
- for (j = 0; j < Size; j++)
- scanf("%d", &ar[i][j]);
- if (Perfect_Matrix(ar, Size))
- printf("\nOK!\n");
- else
- printf("\nNo!\n");
- free(ar[0]); //Free the memory that was allocated.
- free(ar);
- Redo++;
- }
- getch();
- return 0;
- }
- int Perfect_Matrix(int **ar, int size) //Checks if the matrix is a perfect matrix.
- {
- int i, j, count = 0;
- for (i = size - 1; i >= 0; i--)
- for (j = 1; j < size; j++)
- {
- if (ar[0][i] == ar[j][i])
- return 0;
- if (ar[size - i - 1][0] == ar[size - i - 1][j])
- return 0;
- 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.
- count++;
- }
- if (count == (size-1)*size) //Checks if the number of values is valid for a Perfect Matrix.
- return 1;
- else
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement