【发布时间】:2015-12-05 07:21:16
【问题描述】:
我的愚蠢问题可能有一个简单的答案,但我现在似乎无法进行逻辑思考。
到目前为止,我有一个函数来创建一个二维数组(基于用户输入的维度)并为数组的每个元素生成一个随机数。
下一部分是我卡住的地方......
如何将此二维数组移动到另一个函数中,然后将其导出到文本文件中?
如果我是对的并且有一个简单的解决方案,你还能告诉我怎么做吗?我更像是一个视觉学习者。
-谢谢
# include <iostream>
# include <fstream>
# include <string>
# include <ctime>
using namespace std;
void Array(int rows, int columns)
{
int **Array = new int*[rows];
for (int i = 0; i < rows; ++i)
Array[i] = new int[columns];
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < columns; x++)
{
Array[y][x] = rand();
}
}
}
void File()
{
string Name;
cout << "Enter a file name you would like the array to be located under.\n";
cin >> Name;
Name = Name + ".txt";
ofstream fout(Name);
// This is where i would have the array inserted into the text file...
fout.close();
}
int main()
{
int rows, columns;
cout << "Input the number of columns you would like to have in the array. \n";
cin >> columns;
cout << "Input the number of rows you would like to have in the array. \n";
cin >> rows;
srand(time(NULL));
Array(rows, columns);
File();
system("pause");
return (0);
}
【问题讨论】: