Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I've been working hard on this program and I'm not really making any success.
I made a two-dimensional array, but when I input a number I just get a large number with a - in front of it. I don't even know what algorithms to use to make the Magic Square's diagonals equal to one another. Here's my source code:
#include<fstream>
#include<iomanip>
using namespace std;

ofstream outfile;
int main()
{
	outfile.open("output.txt");

	int n; //intermediate variables and array information store
	cout << "Welcome to Magic Square Program!" << endl << endl;
	cout << "Enter order of the magic box: ";
	cin >> n;

	int MagicSquare[5][5];
	MagicSquare[0][2] = 1;
	for (int x = 0; x < n; x++)
	{
		for (int y = 0; y < n; y++){
			cout << setw(3) << x * y << MagicSquare[x][y];
	}
		cout << endl;
	}
system("pause");
	return 0;
}
Posted
Updated 13-Dec-19 4:46am

Quote:
cout << setw(3) << x * y << MagicSquare[x][y];
all the MagicSquare array but the MagicSquare[0][2] item is uninitialized, hence you are getting garbage there.
 
Share this answer
 
you must set the values of our array somewhere. like
C++
MagicSquare[x][y] = x * y;
before accessing it.

And a I told you check that n is below 5 or allocate memory
C++
int *MagicSquare = new int[n*n];
//at the ende
delete MagicSquare;//
 
Share this answer
 
Comments
CPallini 13-Feb-18 5:54am    
Did you tell the OP 3 years ago?
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900