need some help with an odd magic square assignment please

1)is there a limit my code should have as to the odd integer the user can input?

2)as far as declearing my array i think i need numbers inside those braces right, i just cant put in the user's odd integer input

3)how do i set about filling my array?

plz reply asap..thnx

#include <iostream>

using namespace std;

void fillmatrix(int mat[][?], int declaredmatrixsize);

int main()
{

int matrix[][]; 

int matrixsize;

fillmatrix(matrix, matrixsize);

cout<<"Please enter an odd integer for the matrix size"<<endl;

cin>>matrixsize;

while(matrixsize != matrixsize / 2)
{

	cout<<"Please enter an odd for the matrix size"<<endl;

	cin>>matrixsize;

	fillmatrix(matrix, matrixsize);
}

return 0;
}

void fillmatrix(int mat[][?], int declaredmatrixsize)
{

   for(int row = 0; row < declaredmatrixsize; row++)
   {
      for(int col = 0; col < declaredmatrixsize; col++)
      {
         ar[row][col] = ;
      }
   }
}

yeah but what should my max be thats my question..cuz i dont think 20 is enough...and what about filling my array??

Well If You dont have an exact limitation to a particular dataset i prefer that you use a vector for storing all the numbers, And May be a vector array of vectors.

And filling i guess you can do it by taking them from a filestream or you can fill from input

for(int i=0;i<=MAXRows;i++)
{
for(int j=0;j<=MAXColumns;j++)
{
cin>>matrix[i][j];
}
}

This would ask the user to input.

My example shows how to clear it.

Also, 20 is plenty for getting the algorithm to work. If you need more later, then it's a relatively easy change to expand it, knowing that you have something working already.

There's no need to add the complexity of say dynamic memory in the early stage.

Or as Sky says, use vectors. This is C++ after all.

yeah i cant use vectors cuz i never learnt how do it

to put it simply, i cant use something my instructor has taught me cuz it would then be obvious i got help. i have to stay within the limits of my knowledge

The size of your array can be handled with dynamic memory, as I demonstrated earlier, or with an array that will be big enough to suit your needs, as Salem demonstrated, or with a vector of vectors as Sky Diploma suggested. Those are your choices. If you don't know about pointers to do the dynamic memory business and you don't know about vectors, then your only option is Salems. If your instructor doesn't give you a maximum value of n to use then choose whatever size you want for the maximum size of n and move on to the rest of the project.

here is the code for magic matrix you can make it more efficient through dynamic arrays

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
  int n=3;
 
int MagicSquare[3][3];

 
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++) 
		{
		  MagicSquare[i][j] = 0;
		}
	}
//int* MagicS 
   //cin.get();

  int newRow,
  newCol;

  // Set the indices for the middle of the bottom i
  int i =0 ;
  int j= (n) / 2;
MagicSquare[i][j] = 1;
  // Fill each element of the array using the magic array
  for ( int value = 2; value <= n*n; value++ )
  {
    // MagicSquare[i][j] = value;
     // Find the next cell, wrapping around if necessary.
     newRow = (i + n - 1) % n;
     newCol = (j + 1) % n;
	/*newRow = (i + n -1) % n;
	newCol = (j + n  ) % n;*/
     // If the cell is empty, remember those indices for the
     // next assignment.
     if ( MagicSquare[newRow][newCol] == 0 )
     {
        i = newRow;
        j = newCol;
     }
     else
     {
        // The cell was full. Use the cell above the previous one.
        i = (i + 1 + n) % n;
		//j= (j+1)%n;
     }
		MagicSquare[i][j] = value;
  }
 cin.get();

  for(int x=0; x<n; x++)
  {
     for(int y=0; y<n; y++)
         cout << MagicSquare[x][y]<<" ";
     cout << endl;
  }
  cin.get();
  return 0;
}

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

This article has been dead for over six months. Start a new discussion instead.