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
- * OpenMP Version */
- #include <stdio.h>
- #include <omp.h>
- int isMagic(void);
- main()
- {
- FILE *ofp;
- double start, stop;
- start = omp_get_wtime();
- omp_set_num_threads(4);
- ofp = fopen("output.txt", "w");
- if (isMagic() == 0)
- fprintf(ofp, "%s\n", "NO");
- else
- fprintf(ofp, "%s\n", "YES");
- fclose(ofp);
- stop = omp_get_wtime();
- printf("time is: %lf\n", stop - start);
- return 0;
- }
- int isMagic(void)
- {
- int i, j;
- FILE *ifp;
- int size, sum = 0, diag = 0, line = 0;
- int square[10][10];
- int col[10];
- ifp = fopen("input.txt", "r");
- fscanf(ifp, "%d\n", &size);
- #pragma omp parallel
- {
- #pragma omp sections
- {
- #pragma omp section
- {
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++)
- fscanf(ifp, "%d", &square[i][j]);
- }
- }
- #pragma omp section
- {
- 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