Solved

Printing Odd Magic Square

Posted on 2008-06-23
4
705 Views
Last Modified: 2010-05-19
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
Comment
Question by:geminibabie05
[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 10

Expert Comment

by:margajet24
ID: 21852330
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 21852366
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
 

Author Comment

by:geminibabie05
ID: 21852394
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
 

Author Closing Comment

by:geminibabie05
ID: 31470041
Thank you, Sunnycoder.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 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