• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

Printing Odd Magic Square

I have coded a two-diminsional array set up to place one at the top row, middle column and follow conditions to place each number in the right row and column to equal a magic sqaure. The program runs successfully but somewhere has logical errors. When my program compiles, the printed square shows what appears to be zeros or -858993460. Please advise any corrections. Thank You.

#include "stdafx.h"
void MagicAlg(int   magic[20][20], int n);
void PrintMagic(int  magic[20][20], int n);



int main()
{      
      int n;
      printf("Enter an odd number >= 3\n" );
      scanf("%d", &n);
      
      int magic[20][20];
      PrintMagic(magic, n);
      
      return (0);

}
      void MagicAlg(int magic[20][20], int n)
{
      int  a;
      int i=0;
      int j=n/2;

      for (a=1; a<=n*n; ++a)
      {
            magic[i][j] = a;      //1 is placed at row/column 01 with following conditions filling in square.
            i--;
            j++;

            if (a%n == 0)            //If number is divisible by index, row down 1, column same.
            {
              i += 2;
              j--;
            }
            else
            {
              if (j==n)                  //If column = index, column = column - index.
                  j -= n;
              else if (i<0)            //If row < 0, row = row + index.
                  i += n;
            }
        }

}

void PrintMagic(int magic[20][20], int n)
{
  for (int i=0; i<n; i++)
  {
    for (int j=0; j<n; j++)
      printf(" %d", magic[i][j]);

    printf("\n");
  }
}
0
geminibabie05
Asked:
geminibabie05
  • 2
1 Solution
 
margajet24Commented:
void MagicAlg(int magic[20][20], int n) -> void MagicAlg(int magic[][], int n)

void PrintMagic(int magic[20][20], int n) -> void PrintMagic(int magic[][], int n)
0
 
sunnycoderCommented:
Hello geminibabie05,

You do not call MagicAlg!!!

      int magic[20][20];
      MagicAlg(magic,n);
      PrintMagic(magic, n);

Also, you would probably like to add checks for input verification.

Regards,
sunnycoder
0
 
geminibabie05Author Commented:
When I do >>void PrintMagic(int magic[][], int n), the function header and definition have errors of magic missing subscribt. And, the print function cannot convert parameter 1 'int [20][20]' to 'int [][1]'
0
 
geminibabie05Author Commented:
Thank you, Sunnycoder.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now