Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

WRITE A PROGRAM FOR MAGIC SQUARES

Posted on 1997-09-07
4
Medium Priority
?
572 Views
Last Modified: 2006-11-17
WRITE A PROGRAM THAT GENERATES A MAGIC SQUARE BY THE FOLLOWING METHOD.  AN 5X5, AN 7X7, AND A 3X3 SQUARE.  THE SUM OF EVERY ROW, THE SUM OF EVERY COLUMN AND THE SUM OF THE TWO DIAGONALS MUST BE EQUAL FOR A SQUARE TO BE MAGIC.

0
Comment
Question by:tester090797
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
Slarti earned 400 total points
ID: 1254235
This sounds like a high-complexity problem, but there is a very simple solution for the particular case of odd-length magic squares. The following program illustrates this solution. It is written for Borland C++ using only ANSI C commands, so you should have no problems porting it.

------- BEGIN PROGRAM CODE
#include <stdio.h>
#include <mem.h>

#define MAXN 9
typedef int Square[MAXN][MAXN];

int main() {
      Square s={{0}};
      int i,x,y,xnew,ynew,n;

      /* get length of square */
      do {
            printf("Type length of square (odd number between 3 and %d): ",MAXN);
            scanf("%d",&n);
      } while(n%2==0 || n<3 || n>MAXN);

      /* calculate magic square */
      x=0;
      y=n/2;
      for(i=0; i<n*n; i++) {
            /* place current value */
            s[x][y]=i+1;
            /* find next spot */
            xnew = (x+n-1)%n;
            ynew = (y+n-1)%n;
            if(s[xnew][ynew]==0) {
                  x=xnew;
                  y=ynew;
            } else
                  x=(x+1)%n; /* y remains the same */
      }

      /* display result */
      for(y=0; y<n; y++) {
            for(x=0; x<n; x++)
                  printf("%d\t",s[x][y]);
            printf("\n");
      }

      return 0;
}
------- END PROGRAM CODE

That's it. The complexity is proportional to n - you can't do it any faster than that. You can, of course, create larger squares by increasing the value of MAXN.

0
 
LVL 2

Expert Comment

by:Slarti
ID: 1254236
Sorry, my mistake. The complexity is proportional to n squared. But I still think you won't be able to make it any faster.
0
 

Author Comment

by:tester090797
ID: 1254237
THANK YOU!  I BEING A WEEKEND WARRIOR ON LEARNING HOW TO PROGRAMM IN 'C' FOUND THE SOLUTION TO BE VERY USEFUL.  SOME BOOKS I'VE PURCHASED IS ARE EITHER USEFUL OR TOO "TECH."  I'M USING MICROSOFT VISUAL C/C++ 5.0, THE ONLY THING I HAD TO DO TO GET RID OF AN COMPILE ERROR WAS TO REMOVE #INCLUDE <MEM.H> AND IT DID ITS THING.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question