如何在c ++中执行行和列? [英] How can do I do rows and columns in c++?

查看:51
本文介绍了如何在c ++中执行行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我试图用数字输入一个数组输入到一个魔术方阵,中间的数字为1。我不知道如何制作网格

这是我的代码!



In my code I'm trying to make an array out of numbers input into a magic square with the number 1 in the top middle. I don't know how to make the grid
Here's my code!

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

ofstream outfile;
ifstream infile;
int main(){
	outfile.open("output.txt");
	infile.open("numbers.txt");
	int row, col, n = 5;
	int MagicSquare[5][5], m = 0, s = m/2;
	int ms = n * n; //nxn
	int classification; //classification odd or even
	cout << "Enter order of the square: ";

	while (!cin.eof()){
		cin >> MagicSquare[5][5];
		classification = MagicSquare[5][5];

		//Putting the number 1 in top row middle column
		MagicSquare[1][3] = 1;

		if (classification % 2 == 0){
			cout << "Please enter odd integers only: ";
		}
		for (int i = 1; i <= ms; i++){
			MagicSquare[m][s] = i;
			m++;
			s--;
		}
		for (int row = 0; row < n; row++){
			for (int col = 0; col < n; col--)
			{
				cout << MagicSquare[m][s];
			}
				cout << MagicSquare[m][s];
		}
	}
			system("pause");
			return 0;
			}

推荐答案

以下说法有误。

The following statement is wrong.
cin >> MagicSquare[5][5];
classification = MagicSquare[5][5];



您无法合法地解决 MagicSquare [5] [5] ,因为这些偏移超出了数组的末尾。您声明 MagicSquare [5] [5] ,这意味着它的有效偏移量在[0 .. 4]范围内。



这也是毫无意义的,为什么不呢:


You cannot legally address MagicSquare[5][5], as those offsets are beyond the end of the array. You declared MagicSquare[5][5], which means its valid offsets are in the range [0 .. 4].

It's also rather pointless, why not just:

cin >> classification;



你还有声明:


You also have the statement:

MagicSquare[1][3] = 1;



每次通过你的循环,再次大多数是多余的;但也错了,那不是顶级中间,而是第二排第四栏。



我建议你在纸上画你的方格,对列和行进行编号[0。 .4]并检查插入数字的最佳方式是什么。


every time through your loop, again mostly redundant; but also wrong, that is not top middle, but second row fourth column.

I would suggest you draw your square on paper, number the columns and rows [0 .. 4] and check how is the best way to insert the numbers.


这篇关于如何在c ++中执行行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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