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: kkx123 is an unknown quantity at this point 
Solved Threads: 0
kkx123 kkx123 is offline Offline
Newbie Poster

can anyone help me with writing a magic square program?

 
0
  #1
Mar 12th, 2007
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~

  1. #include <stdio.h>
  2. #define R 4
  3. #define C 4
  4.  
  5. void Same(int []);
  6. int main()
  7. {
  8. int array[R][C], i, j, n[( R + R + 2)], out;
  9. printf("\n * MAGIC * SQUARE *");
  10. printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix.");
  11. printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of");
  12. printf("\neach row, column, and both diagonals have the same values.");
  13. printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n");
  14. for (i = 0; i < R; i++)
  15. for (j = 0; j < C; j++);
  16. {
  17. scanf("%d", &array[i][j]);
  18. if (array[i][j] == -1)
  19. return 0;
  20. }
  21. for (i = 0; i < R; i++)
  22. {
  23. for (j = 0; j < C; j++)
  24. printf("%d ", array[i][j]);
  25. putchar("\n");
  26. }
  27. for (i = 0; i < R; i++)
  28. {
  29. out = 0;
  30. for (j = 0; j < C; j++)
  31. {
  32. out = out + array[i][j];
  33. }
  34. n[i] = out;
  35. }
  36. for (j = 0; j < C; j++)
  37. {
  38. out = 0;
  39. for (i = 0; i < C; i++)
  40. {
  41. out = out + array[i][j];
  42. }
  43. n[(j + R)] = out;
  44. }
  45. for (i = 0, out = 0; i < R; i++)
  46. {
  47. out = out + array[i][i];
  48. }
  49. n[( R + R )] = out;
  50. for (i = 0, out = 0; i < R; i++ )
  51. {
  52. out = out + array[i][R - 1 - 1];
  53. }
  54. n[R + R + 1] = out;
  55. Same(n);
  56. return 0;
  57. }
  58. void Same(int total[])
  59. {
  60. int g = total[0], i;
  61. for (i = 1; i < (R + R + 20; i++)
  62. {
  63. if (g != total[i] )
  64. i = (R + R + 2);
  65. }
  66. if (i == (R + R + 3) )
  67. printf("Not a Magic Square!\n\n");
  68. else
  69. printf("A Magic Square!\n\n");
  70. }
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: can anyone help me with writing a magic square program?

 
0
  #2
Mar 12th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 29
Reputation: fulyaoner is an unknown quantity at this point 
Solved Threads: 2
fulyaoner's Avatar
fulyaoner fulyaoner is offline Offline
Light Poster

Re: can anyone help me with writing a magic square program?

 
0
  #3
Mar 12th, 2007
Originally Posted by kkx123 View Post
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~

  1. #include <stdio.h>
  2. #define R 4
  3. #define C 4
  4.  
  5. void Same(int []);
  6. int main()
  7. {
  8. int array[R][C], i, j, n[( R + R + 2)], out;
  9. printf("\n * MAGIC * SQUARE *");
  10. printf("\n\n\nThis program will ask for 16 numbers and it will store it in a matrix.");
  11. printf("\nA magic square is an n by n matrix that is filled with numbers. The sum of");
  12. printf("\neach row, column, and both diagonals have the same values.");
  13. printf("\n\n\nPlease enter 16 integers below. Enter -1 to EXIT.\n");
  14. for (i = 0; i < R; i++)
  15. for (j = 0; j < C; j++);
  16. {
  17. scanf("%d", &array[i][j]);
  18. if (array[i][j] == -1)
  19. return 0;
  20. }
  21. for (i = 0; i < R; i++)
  22. {
  23. for (j = 0; j < C; j++)
  24. printf("%d ", array[i][j]);
  25. putchar("\n");
  26. }
  27. for (i = 0; i < R; i++)
  28. {
  29. out = 0;
  30. for (j = 0; j < C; j++)
  31. {
  32. out = out + array[i][j];
  33. }
  34. n[i] = out;
  35. }
  36. for (j = 0; j < C; j++)
  37. {
  38. out = 0;
  39. for (i = 0; i < C; i++)
  40. {
  41. out = out + array[i][j];
  42. }
  43. n[(j + R)] = out;
  44. }
  45. for (i = 0, out = 0; i < R; i++)
  46. {
  47. out = out + array[i][i];
  48. }
  49. n[( R + R )] = out;
  50. for (i = 0, out = 0; i < R; i++ )
  51. {
  52. out = out + array[i][R - 1 - 1];
  53. }
  54. n[R + R + 1] = out;
  55. Same(n);
  56. return 0;
  57. }
  58. void Same(int total[])
  59. {
  60. int g = total[0], i;
  61. for (i = 1; i < (R + R + 20; i++)
  62. {
  63. if (g != total[i] )
  64. i = (R + R + 2);
  65. }
  66. if (i == (R + R + 3) )
  67. printf("Not a Magic Square!\n\n");
  68. else
  69. printf("A Magic Square!\n\n");
  70. }
the very first error I see is a ZOMBIE ; at the end of the second for loop.

for (i = 0; i < R; i++)
for (j = 0; j < C; j++);
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: kkx123 is an unknown quantity at this point 
Solved Threads: 0
kkx123 kkx123 is offline Offline
Newbie Poster

Re: can anyone help me with writing a magic square program?

 
0
  #4
Mar 12th, 2007
ah yea thanks guys for looking at it...
yea i figured out that there was a zombie ; at the end of the for loop ^_^ lol i was thinking too much about how the program should look like that i didnt realize that tiny little ";"!! i feel so mad at myself, hehe thanks alot guys
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC