Solved

Printing Odd Magic Square

Posted on 2008-06-23
4
701 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
  • 2
4 Comments
 
LVL 10

Expert Comment

by:margajet24
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, Sunnycoder.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now