Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* determines if a numerical square magic (sum of elems in each column
- * equally sum of elems in each row and equally sum of elems
- * in main diagonal) or not magic */
- #include <stdio.h>
- int isMagic(void);
- main()
- {
- FILE *ofp;
- ofp = fopen("output.txt", "w");
- if (isMagic() == 0)
- fprintf(ofp, "%s\n", "NO");
- else
- fprintf(ofp, "%s\n", "YES");
- fclose(ofp);
- return 0;
- }
- int isMagic(void)
- {
- int i, j;
- FILE *ifp;
- int size, sum = 0, diag = 0, line = 0;
- char square[10][10];
- char col[10];
- ifp = fopen("input.txt", "r");
- fscanf(ifp, "%d\n", &size);
- for (i = 0; i < size; i++)
- for (j = 0; j < size; j++)
- fscanf(ifp, "%d", &square[i][j]);
- for (j = 0; j < size; j++) {
- sum += square[0][j];
- col[j] = 0;
- }
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- line += square[i][j];
- col[j] += square[i][j];
- if (i == j)
- diag += square[i][j];
- }
- if (line != sum)
- return 0;
- else
- line = 0;
- }
- for (j = 0; j < size; j++) {
- if (col[j] != sum)
- return 0;
- }
- if (diag != sum)
- return 0;
- fclose(ifp);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment