需要帮助我的魔方计划:( [英] Need help with my magic square program :(

查看:73
本文介绍了需要帮助我的魔方计划:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习这个项目,我并没有取得任何成功。

我制作了一个二维数组,但是当我输入一个数字时,我得到一个很大的数字有一个 - 在它面前。我甚至不知道使用什么算法来使Magic Square的对角线彼此相等。这是我的源代码:

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;
}

推荐答案

Quote:

cout<< setw(3)<< x * y<< MagicSquare [x] [y];

cout << setw(3) << x * y << MagicSquare[x][y];

所有 MagicSquare 数组,但 MagicSquare [0] [2] item未初始化,因此你在那里得到垃圾。

all the MagicSquare array but the MagicSquare[0][2] item is uninitialized, hence you are getting garbage there.


你必须在某处设置我们数组的值。喜欢
you must set the values of our array somewhere. like
MagicSquare[x][y] = x * y;

,然后才能访问它。



和< b>我告诉你检查n是否低于5或分配内存

before accessing it.

And a I told you check that n is below 5 or allocate memory

int *MagicSquare = new int[n*n];
//at the ende
delete MagicSquare;//


这篇关于需要帮助我的魔方计划:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆