So this is the program description as per hw requirements.

The following is a procedure for constructing an n x n magic square for any odd integer n.

Place 1 in the middle column of the top row.

Then after integer k has been placed, move up one row and one column to the right to place the next integer k + 1, unless one of the following occurs:

If a move takes you above the top row in the ith column, move to the bottom of the ith column and place k + 1 there

If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side.

If a move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k + 1 immediately below k.

Write a program to construct n x n magic square for any odd value of n.

thnx

#include <iostream>

using namespace std;

void fillmatrix(int matrix[][n], int size);

int main()

{
	int n;

	cout<<"Enter the integer value for your magic square"<<endl;

	cin>>n;

	int matrix[n][n];

	fillmatrix(matrix, n);

	

	return 0;

}

void fillmatrix(int matrix[][n], int size)
{
	for(int row = 0; row < size; row++)
   {
      for()
      {
		 //not sure how to set this part up
      }
   }
}

Recommended Answers

All 12 Replies

i know my code is pretty pathetic so far..but dont plan on majoring in computer sci..its just an engineering req..got 2 weeks left till end of class

Answer these questions and you'll be well on your way.

How do you access a given element of a 2 dimensional array?

What is the range of valid indexes for an array of size n x n?

Determine the indexes of the element occupying the first row middle column.

If you are at element Q which has indexes row and col what indexes does the element one row up and one column to the right have?

How will you know if element Q is in the top row or the farthest right hand column of the array?

yeah these are ques unfortunately i dont really have the ans to

i know my code is pretty pathetic so far..but dont plan on majoring in computer sci..its just an engineering req..got 2 weeks left till end of class

Why bother to tell us this? You have to take all sorts of classes that aren't in your major. Presumably someone in the Engineering Department thinks you need to know how to do this to be an engineer.

yeah these are ques unfortunately i dont really have the ans to

So start putting some work in and looking for the answers. Look at your notes, Look at some tutorials, ask a question on this forum that shows that you have the slightest willingness to put in any work at all as opposed to someone here writing the whole thing for you.

i asked 4 help and i dint say do my assigment...i obviously have gone over my notes..the problem is i dont know how to start filling in my columns the way its supposed to be

ok to be more specific..based on my understanding of the hw requirements the matrix is user-defined and i have a problem setting up the matrix to fill up the middle. this is what i initially
what im thinking is since the user input is always odd and it will start from 0, ill be able to fill in the middle column

void fillmatrix(int matrix[][n], int size)
{
	for(int row = 0; row < size; row++)
   {
      for(int col = 0; col = n; col / 2)
      {
		  matrix[row][col] = 1;
      }
   }
}

Okay, here's one way set up a matrix (the same as a 2 dimensional array) of any given size, n x n, with n supplied by the user.

int n;
cout << "enter size of n x n matrix" << endl;
cin >> n;

int matrix**;
matrix = new int*[n];
for(int i = 0; i < n; ++i)
  matrix[i] = new int[n];

You can't do it the way you tried because in the way you tried n has to be a known constant value at compile time. Using the above code the user can provide n. That is n can be provided at run time, not at compile time.

Now play around with this. If n equals 5 and you wanted to put the value 356 in the 3rd element of the 4 row, how would you do it? If you wanted to print the value of the 2nd element in the first row to the screen, how would you do it? What is the range of valid indexes for this matrix where n is 5? When you can answer these questions then answere them all again for a matrix with n of 11 and then for a matrix with n of 127.

Then try answering the previous set of questions.

Then try following the directions given by your instructor.

thnx 4 the fix..can u explain the code though, as in what exactly did u do??

Since you don't know the value of n at compile time you can't know how much memory to declare until you know the value of n at run time. Therefore you use the keyword new to declare the memory you want to use for matrix and you declare matrix to be of type pointer to pointer to type int so you can assign memory to it.

int * x;
x = new int;

declares x to be a pointer on the heap using dynamic memory. The address of the memory for the int is found by new and is assigned to x.

int * intArray;
intArry = new int[5];

This declares memory for 5 ints on the heap using dynamic memory.

int n;
cin >> n;
int * myArray2 = new int[n];

This declares memory for n ints using dynamic memory, as long as there is enough memory to do it.

int ** matrix; //I see I blew it the first time out of the box, sorry. matrix is a pointer to pointer to type int. Although, technically, it's not an array of arrays or a two dimensional array, for most purposes, and certainly for your purposes it behaves just like one when it comes to addressing individual elements of the variable, etc.

matrix = new int*[n]; //Assigns memory for n pointers to type int. Each matrix is a pointer to type int. As noted above that pointer can be used to access memory for an array of int. Then for each matrix, where i ranges from 0 to less than n, you assign memory for an array of n ints, just like above.

for(int i = 0; i < n; ++i)
matrix = new int[n];

Hence, each matrix is a pointer to type int, or if you prefer you can think of it as a row of ints in the matrix, and each matrix[x][y] is an actual, individual int in matrix, where x can be thought of as the row number and y as the column number that the int occupies.

thnx for the explaination..but heres the problem we havent done pointers so im kinda lost in ur explaination..ive sent an email to my instructor askin if the assignments due be4 that lecture or not..is there a way around this without pointers??

any1 with any ideas??

Rather than mess about with memory allocation (which is just as hard as the real problem you need to answer), start with.

const int MAX = 20;
void initsquare( int square[][MAX] ) {
  for ( int r = 0 ; r < MAX ; r++ ) {
    for ( int c = 0 ; c < MAX ; c++ ) {
      square[r][c] = 0;
    }
  }
}

int main ( ) {
  int square[MAX][MAX];
  initsquare( square );
  int n;
  cout << "How big?";
  cin >> n;
  // check it's < MAX, then do your thing
}

When you've got the rest of the code working, then (if you want), it's a relatively painless transition to using a dynamic solution based on something you know that works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.