转换二维数组用C ++函数文本 [英] Converting 2D array to text using c++ functions

查看:139
本文介绍了转换二维数组用C ++函数文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能是一个简单的答案我的愚蠢的问题,但我似乎无法逻辑思考现在。

什么我迄今为止是创建一个二维数组(基于用户输入的尺寸),并产生用于所述阵列中的每个元素的随机数的函数。

这接下来的部分是我卡住...

我如何移动这个二维数组到另一个功能,将其导出到文本文件?

如果我是正确的,有一个简单的解决方案,可能你还告诉我怎么做。我更视觉学习者。

- 谢谢

 #包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;串GT;
#包括LT&;&的ctime GT;使用命名空间std;无效阵列(INT行,诠释列)
{
    INT **阵列=新的INT * [行]
的for(int i = 0; I<行++ I)
    阵列[我] =新的INT [列];对于(INT Y = 0; Y<行; Y ++)
{
    对于(INT X = 0; X<列; X ++)
    {
        数组[Y] [X] = RAND();
    }
}
}无效文件()
{字符串名称;COUT<< 输入你想要的阵列可位于下一个文件名\\ n;
CIN>>名称;名称=名称+名.txt;的ofstream FOUT(姓名);//这是我将不得不插入文本文件中的数组...fout.close();
}
诠释的main()
{
INT行,列;    COUT<< 输入您希望的列数为具有阵列中\\名;
    CIN>>列;
    COUT<< 输入您希望的行数有阵列中\\名;
    CIN>>行;函数srand(时间(NULL));阵列(行,列);
文件();系统(暂停);
返回(0);
}


解决方案

例如:

 使用命名空间std;    INT **阵列(INT行,诠释列)
    {
        INT **阵列=新的INT * [行]        的for(int i = 0; I<行++ I)
            阵列[我] =新的INT [列];        对于(INT Y = 0; Y<行; Y ++)
        {
            对于(INT X = 0; X<列; X ++)
            {
                数组[Y] [X] = RAND();
            }
        }        返回数组;
    }    无效文件(INT **改编,诠释行,诠释列)
    {        字符串名称;        COUT<< 输入你想要的阵列可位于下一个文件名\\ n;
        CIN>>名称;
        名称=名称+名.txt;        ofstream的FOUT(Name.c_str());        对于(INT Y = 0; Y<行; Y ++)
        {
            对于(INT X = 0; X<列; X ++)
            {
                FOUT<<常用3 [Y] [X]<<;
            }            FOUT<< ENDL;
        }        fout.close();
    }
    诠释的main()
    {
        INT行,列;        COUT<< 输入您希望的列数为具有阵列中\\名;
        CIN>>列;
        COUT<< 输入您希望的行数有阵列中\\名;
        CIN>>行;        函数srand(时间(NULL));        INT **阵列=阵列(行,列);        阵列(行,列);        文件(阵列,行,列);        系统(暂停);        对于(INT Y = 0; Y<行; Y ++)
        {
            删除[]数组[Y];
        }        删除[]数组;
        返回(0);
    }

和请注意,你忘了,包括系统cstdlib,你不能在构造函数中的ofstream传递字符串,则需要使用c_str()字符串方法。在Visual Studio这将是没有这个修正的作品,但也许这将是有益的。
如果我们使用新的,我们需要使用删除preventing内存泄漏。

There probably is a simple answer to my stupid question, but I just can't seem to think logically right now.

What I have so far is a function to create a 2D array (based on the dimensions the user enters) and generate a random number for each element of the array.

This next part is where I get stuck...

How do I move this 2D array into another function that will export it into a text file?

If I was right and there is an easy solution, could you still show me how to do it. I'm more of a visually learner.

-Thanks

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

解决方案

For example:

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

And please note that you forgot to include cstdlib for system and you can not pass string in constructor ofstream, you need use c_str() method of string. In visual studio it will be works without this corrections but maybe this will be helpful. And if we use new we need use delete for preventing memory leaks.

这篇关于转换二维数组用C ++函数文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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