0

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);
}
JohnM
  • 13
  • 3

3 Answers3

1

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.

Evgeniy331
  • 404
  • 2
  • 8
0

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

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0
for (int y = 0; y < rows; y++) { 
    for (int x = 0; x < columns; x++) { 
        // write Array[y][x] to file here
    }
}
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20