help on magic square

This is a discussion on help on magic square within the C Programming forums, part of the General Programming Boards category; I can't get this code to work for magic squares 4x4 or 5x5, please help Code: #include <stdio.h> #include <time.h> ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Help on magic square

    I can't get this code to work for magic squares 4x4 or 5x5, please help

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    int RandomInt(int n);
    void FillSquare (int sq[5][5], int n);
    int IsMagic(int sq[5][5], int n);
    void PrintSquare(int sq[5][5], int n);
    
    int
    main(){
      
      int n;
      srand(time(NULL));
    
      /* Ask for the size of the magic square */
    
      printf("Enter the size of magic square, between 3 and 5: ");
      scanf("%d", &n);
      
     
      /*  if the number entered isn't between 3 and 5 the program will exit. */
    
      if(n < 3 || n >5)
        {printf("number not between 3 and 5, please try again.");
        return 0;
        }
      else 
        {
          int sq[5][5];
          int IsMagicTrue=0;
          while (!IsMagicTrue)
    	{
    	  FillSquare(sq, n);
    	  IsMagicTrue=IsMagic(sq, n);
    	}
          PrintSquare(sq,n);
          return 0;
        }}
    
      /* Creating random integers from 1 to n */
    
    int RandomInt(int n) 
    {
      int r;
      
      r=1+rand()%n;
      return r;
    }
    
      /* Placing the integers in random spots of the Magic Square that are unoccupied */
    
    void FillSquare (int sq[5][5], int n) {
      int x;
      int y;
      int currentnum;
      for(x = 0; x < n; x++)
        {
          for (y = 0; y < n; y++)
    	{sq[x][y]=0;
    	}}
      
      currentnum = (n*n);
      x = RandomInt(n)-1;
      y = RandomInt(n)-1;
      while(currentnum > 0)
        
       /* This loop continues until magic square is complete */
        
        {if(sq[x][y] == 0)
          {sq[x][y] = currentnum;
          currentnum--;
          }
        x = RandomInt(n)-1;
        y = RandomInt(n)-1;
        }}
    
    int IsMagic(int sq[5][5], int n)
    {
      
      /* Checking each row to see if it is equal to (((n^3)+n)/2) */
      
      int total=0;
      int x;
      int y;
      int total2=0;
      for(x = 0; x < n; x++)
        {for(y = 0; y < n; y++)
          {
    	total += sq[x][y];
          }
        if(total != (((n*n*n)+n)/2))
          {return 0;}
        total=0;
        }
      
      
      /* Checking each column to see if it is equal to (((n^3)+n)/2) */
     total=0;
      for(y = 0; y < n; y++)
        {for(x = 0; x < n; x++)
          {
    	total += sq[x][y];
          }
        if(total != (((n*n*n)+n)/2))
          {return 0;}
        total=0;
        }
      
      /* Checking each diagonal to see if it is equal to (((n^3)+n)/2) */
      
      total =0;
      
      for(x = 0; x < n; x++)
        {
          total += sq[x][x];
          total2 += sq[n-1-x][x];
        }
      if(total != (((n*n*n)+n)/2))
        {return 0;}
      
      /* Finished all the checks */
      return 1;
    }
    
      /* This will print the magic square */
    
    void PrintSquare(int sq[5][5], int n)
    {
      int x;
      int y;
      for(x = 0; x < n; x++)
        {
          for(y = 0; y < n; y++)
    	{ 
    	  printf(" %d ", sq[x][y]);
    	}
          printf("\n");
        }
    }
    Last edited by katway; 03-07-2005 at 06:10 PM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    help on magic square

    i got this code to make magic squares, but how do i make it so that they aren't the same each time?
    Code:
    #include <stdio.h>
    
    enum {
    	MAXIMUM_SIZE = 5
    };
    enum{
            MINIMUM_SIZE = 3
    };
    	
    int
    main(void) {
        int square[MAXIMUM_SIZE][MAXIMUM_SIZE];
        int x,y,i,j,size;
        
        printf("Enter the size of the magic square you wish to create: ");
        scanf("%d", &size);
        
        if (size > MAXIMUM_SIZE) {
            printf("Sorry the size has to be smaller than %d\n", MAXIMUM_SIZE);
            return 1;
        }
        
        if (size < MINIMUM_SIZE ) {
            printf("Sorry the size has to be greater than %d\n", MINIMUM_SIZE);
            return 1;
        }
        
        x = size*size + size/2;
        y = size*size;
        
        for (i = 1; i <= size*size; i++) {
            square[x % size][y % size] = i;
            if (i % size == 0) {
                y++;
            } else {
                x--;
                y--;
            }
        }
        
        for (i = 0; i < size; i++) {
            for (j = 0; j < size; j++)
                printf("%4d ", square[j][i]);
            printf("\n");   
        }
        
        return 0;
    }
    Last edited by katway; 03-07-2005 at 06:06 PM. Reason: changed

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In the future please keep all posts that relate to the same problem in one thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Windows crashing Magic Square
    By KoshiB in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2006, 09:02 PM
  3. magic square whoes
    By caws in forum C Programming
    Replies: 9
    Last Post: 03-30-2003, 09:36 PM
  4. Help with magic square program
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-15-2002, 05:57 AM
  5. magic square
    By dizzyhippy in forum C Programming
    Replies: 1
    Last Post: 03-11-2002, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21