Converting 2D array to text using c++ functions

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

Answers


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.


You can use the same nested for loop for inserting array into file which you are using for inserting values into the array


for (int y = 0; y < rows; y++) { 
    for (int x = 0; x < columns; x++) { 
        // write Array[y][x] to file here
    }
}

Need Your Help

I need help building a loop in Excel-VBA

excel vba excel-vba loops

I have a block of data like below that I need to insert into a sheet with several thousand rows.

Git "efrror: RPC failed; result=55, HTTP code = 0" on push

git github push git-push

I've spent all day on this one and could really use some help. When I try to push a relatively large commit,