| | |
can anyone help me with writing a magic square program?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 0
can anyone help me with this? i can get it to run but when i run it, the matrix doesnt execute correctly...i am writing it in "C"..this should be a 4x4 magic square...thanks~
c Syntax (Toggle Plain Text)
#include <stdio.h> #define R 4 #define C 4 void Same(int []); int main() { int array[R][C], i, j, n[( R + R + 2)], out; printf("\n * MAGIC * SQUARE *"); printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix."); printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of"); printf("\neach row, column, and both diagonals have the same values."); printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n"); for (i = 0; i < R; i++) for (j = 0; j < C; j++); { scanf("%d", &array[i][j]); if (array[i][j] == -1) return 0; } for (i = 0; i < R; i++) { for (j = 0; j < C; j++) printf("%d ", array[i][j]); putchar("\n"); } for (i = 0; i < R; i++) { out = 0; for (j = 0; j < C; j++) { out = out + array[i][j]; } n[i] = out; } for (j = 0; j < C; j++) { out = 0; for (i = 0; i < C; i++) { out = out + array[i][j]; } n[(j + R)] = out; } for (i = 0, out = 0; i < R; i++) { out = out + array[i][i]; } n[( R + R )] = out; for (i = 0, out = 0; i < R; i++ ) { out = out + array[i][R - 1 - 1]; } n[R + R + 1] = out; Same(n); return 0; } void Same(int total[]) { int g = total[0], i; for (i = 1; i < (R + R + 20; i++) { if (g != total[i] ) i = (R + R + 2); } if (i == (R + R + 3) ) printf("Not a Magic Square!\n\n"); else printf("A Magic Square!\n\n"); }
Last edited by WaltP; Mar 12th, 2007 at 3:32 am. Reason: CODE tags -- did you see the words on the background of the post box?
Not even going to try to read this code. Format it and repost, and use CODE tags.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
can anyone help me with this? i can get it to run but when i run it, the matrix doesnt execute correctly...i am writing it in "C"..this should be a 4x4 magic square...thanks~
c Syntax (Toggle Plain Text)
#include <stdio.h> #define R 4 #define C 4 void Same(int []); int main() { int array[R][C], i, j, n[( R + R + 2)], out; printf("\n * MAGIC * SQUARE *"); printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix."); printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of"); printf("\neach row, column, and both diagonals have the same values."); printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n"); for (i = 0; i < R; i++) for (j = 0; j < C; j++); { scanf("%d", &array[i][j]); if (array[i][j] == -1) return 0; } for (i = 0; i < R; i++) { for (j = 0; j < C; j++) printf("%d ", array[i][j]); putchar("\n"); } for (i = 0; i < R; i++) { out = 0; for (j = 0; j < C; j++) { out = out + array[i][j]; } n[i] = out; } for (j = 0; j < C; j++) { out = 0; for (i = 0; i < C; i++) { out = out + array[i][j]; } n[(j + R)] = out; } for (i = 0, out = 0; i < R; i++) { out = out + array[i][i]; } n[( R + R )] = out; for (i = 0, out = 0; i < R; i++ ) { out = out + array[i][R - 1 - 1]; } n[R + R + 1] = out; Same(n); return 0; } void Same(int total[]) { int g = total[0], i; for (i = 1; i < (R + R + 20; i++) { if (g != total[i] ) i = (R + R + 2); } if (i == (R + R + 3) ) printf("Not a Magic Square!\n\n"); else printf("A Magic Square!\n\n"); }
for (i = 0; i < R; i++)
for (j = 0; j < C; j++);
![]() |
Similar Threads
- Magic Square (Java)
- Vector of vectors & magic square issues (C++)
- Print Magic square (C)
- help with "disabling" a button (C)
- writing a simple cat program (C)
Other Threads in the C Forum
- Previous Thread: assign elements to a multi-d array
- Next Thread: Read me:
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop initialization input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list lists locate looping lowest matrix meter microsoft number oddnumber opendocumentformat openwebfoundation overwrite owf pdf pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






