【问题标题】:Converting 2D array to text using c++ functions使用 C++ 函数将二维数组转换为文本
【发布时间】: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);
}

【问题讨论】:

    标签: c++ arrays function


    【解决方案1】:

    例如:

        using namespace std;
    
        int** 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();
                }
            }
    
            return Array;
        }
    
        void File(int **arr, int rows, int columns)
        {
    
            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.c_str());
    
            for (int y = 0; y < rows; y++)
            {
                for (int x = 0; x < columns; x++)
                {
                    fout<<arr[y][x]<<" ";
                }
    
                fout<<endl;
            }
    
            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));
    
            int **array = Array(rows, columns);
    
            Array(rows, columns);
    
            File(array, rows, columns);
    
            system("pause");
    
            for (int y = 0; y < rows; y++)
            {
                delete []array[y];
            }
    
            delete []array;
    
    
            return (0);
        }
    

    请注意,您忘记包含系统的 cstdlib 并且您不能在流的构造函数中传递字符串,您需要使用字符串的 c_str() 方法。在 Visual Studio 中,无需此更正即可工作,但也许这会有所帮助。 如果我们使用 new 我们需要使用 delete 来防止内存泄漏。

    【讨论】:

    • 完美!感谢您的帮助以及有关使用删除的说明。
    【解决方案2】:

    您可以使用相同的嵌套 for 循环将数组插入到文件中,而文件中则用于将值插入到数组中

    【讨论】:

      【解决方案3】:
      for (int y = 0; y < rows; y++) { 
          for (int x = 0; x < columns; x++) { 
              // write Array[y][x] to file here
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 1970-01-01
        • 2015-03-08
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多