velkoon
velkoon

Reputation: 962

Transfer a 2D array to a csv file?

I have a matrix called "temp_matrix" that looks kind of like this:

0.0,100.0,100.0,100.0,
0.0,45.1,60.6,66.2,0,
0.0,45.1,60.6,66.2,0,
0.0,100.0,100.0,100.0,0,

...except mine is a 20x20 matrix.

I've tried:

ofstream excel_plate("plate.csv");
excel_plate.write(temp_matrix[0],20);
excel_plate.close();
cout << endl;

but can't compile.

I've tried:

ofstream excel_plate("plate.csv");
excel_plate << temp_matrix[0], 20, 20;
excel_plate.close();

...but just get a csv file with a string of characters in the very first cell that looks like this: 0052EE98.

Here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <math.h>
#include <Windows.h>
#include <algorithm>
#include <string>
#include <array>
#undef max

using namespace std;

const double neighbors = 4;

// START FUNCTION: Update Elements
void average(double *temp_matrix, int ROWS, int COLUMNS)
{
    int i = 0;
    int j = 0;

    double row_left = 0;
    double row_right = 0;
    double column_up = 100;
    double column_down = 0;
    double sum = 0;

    double av = 0;

    for (int i = 1; i < ROWS - 1; i++)
    {
        for (int j = 1; j < COLUMNS - 1; j++)
        {
            /*
            cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
            cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
            cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
            cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
            */

            row_left = *(temp_matrix + i*ROWS + (j-1));
            row_right = *(temp_matrix + i*ROWS + (j + 1));
            column_up = *(temp_matrix + (i-1)*ROWS + j);
            column_down = *(temp_matrix + (i+1)*ROWS + j);

            sum = row_left + row_right + column_up + column_down;
            av = sum / neighbors;

            *(temp_matrix + i*ROWS + j) = av;
        }
    }
}
// END FUNCTION: Update Elements

// START FUNCTION: Updtate Until Stable
void update(double *temp_matrix, int ROWS, int COLUMNS)
{
    int i = 0;
    int j = 0;

    double row_left = 0;
    double row_right = 0;
    double column_up = 100;
    double column_down = 0;
    double sum = 0;

    double old_num = 0;
    double new_num = 0;

    double difference = 0;
    double max_change = .11;

    while (max_change > .1)
    {
        max_change = -1;
        for (int i = 1; i < ROWS - 1; i++)
        {
            for (int j = 1; j < COLUMNS - 1; j++)
            {
                old_num = *(temp_matrix + i*ROWS + j);

                /*
                cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
                cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
                cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
                cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
                */

                row_left = *(temp_matrix + i*ROWS + (j - 1));
                row_right = *(temp_matrix + i*ROWS + (j + 1));
                column_up = *(temp_matrix + (i - 1)*ROWS + j);
                column_down = *(temp_matrix + (i + 1)*ROWS + j);

                sum = row_left + row_right + column_up + column_down;
                new_num = sum / neighbors;

                difference = new_num - old_num;
                if (difference > max_change)
                {
                    max_change = difference;
                }

                *(temp_matrix + i*ROWS + j) = new_num;
            }
        }
    }
}
// END FUNCTION: Update Until Stable

// START FUNCTION: Print Array
void print_array(double *temp_matrix, int ROWS, int COLUMNS)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            if (j > 0)
            {
                cout << ",";
            }
                cout << fixed << setw(5) << setprecision(1) << *(temp_matrix + i*ROWS + j);
        }
        cout << endl;
    }
}
// END FUNCTION: Print Array

// START FUNCTION: Print 4 Excel
void print_excel(double *temp_matrix, int ROWS, int COLUMNS)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cout << fixed << setprecision(1) << *(temp_matrix + i*ROWS + j) << ",";
        }
        cout << endl;
    }
}
// END FUNCTION: Print 4 Excel

int main()
{

    const int ROWS = 20;
    const int COLUMNS = 20;

    double temp_matrix[ROWS][COLUMNS] =
    {
        { 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 }
    };


    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    average(temp_matrix[0], 20, 20);
    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    update(temp_matrix[0], 20, 20);
    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    print_excel(temp_matrix[0], 20, 20);

    ofstream excel_plate("plate.csv");
    if (excel_plate.is_open())
    {
        excel_plate << temp_matrix[0], 20, 20;
        excel_plate.close();
    }
    else
    {
        cout << "Unable to open file.";
    }

    /*
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            if (j > 0)
            {
                cout << ",";
            }
            cout << temp_matrix[i][j];
        }
        cout << endl;
    }
    */

    system("pause");
    return 0;
}

This should be very, very simple. Please kindly help a noob out. :)

P.S. Because this is an assignment, you'll notice that I print out several matrices. The last one I print out is the format in which the matrix needs to be transferred to a .csv file.

Upvotes: 2

Views: 11455

Answers (2)

Cyprian Koech
Cyprian Koech

Reputation: 24

So, this code conatains a functions that should be able to transfer any 2d string array into a csv file

    #include <iostream>
    #include <fstream>
    #include<string>  
    
    /* This is for a string array, but if you want to use any other type, just replace
       'std::string' with int,double....*/
    
    template <size_t row, size_t col>
    void twodarray2csv(std::string(&array)[row][col], std::string filename)
    {
        std::ofstream myfile;
        myfile.open(filename);
    
    for (size_t i = 0; i < row; ++i)
    {
        for (size_t j = 0; j < col; ++j)
            if (j < (col - 1)) {
                myfile << array[i][j] << ",";
            }
            else if (j == (col - 1)) {
                myfile << array[i][j] << "\n";
            }
    }
}
 

Th template gets the row size and column size and also opens ofstream which is basically for just opening the csv file that will be the output file

int main() {
        //example
        std::string ArrayName[9][3];
        /* this for loop is just there to populate the array, if you have an array
         already skip this step */
            #include <iostream>
        #include <fstream>
        #include<string>  
        
        /* This is for a string array, but if you want to use any other type, just replace
           'std::string' with int,double....*/
        
        template <size_t row, size_t col>
        void twodarray2csv(std::string(&array)[row][col], std::string filename)
        {
            std::ofstream myfile;
            myfile.open(filename);
        
        for (size_t i = 0; i < row; ++i)
        {
            for (size_t j = 0; j < col; ++j)
                if (j < (col - 1)) {
                    myfile << array[i][j] << ",";
                }
                else if (j == (col - 1)) {
                    myfile << array[i][j] << "\n";
                }
        }
    }

So the function above uses a template to get the row size and column size of the array, after which it iterates through the array so that when it passes by elements of the same row it adds a comma to the file and at the end of each row it adds a new line (\n) which is a row separator

The part below is an example of how to use it and how it works First i just created a an Array called ArrayName, and populated it with just some numbers (int number)

 int number = 0;
                for (int i = 0; i < 9; i++) {
                    for (int x = 0; x < 3; x++) {
            
                        ArrayName[i][x] = number;
                        number++;
                    }
                }
            
            
                //this part converts the array to a csv file of desried name
                twodarray2csv(ArrayName, "outputfile.csv");
            
            
            
            
            
                return 0;
            }

The output will be a csv file with the name "outputfile.csv" stored in the same folder with the cpp file you have

Upvotes: 0

user657267
user657267

Reputation: 20990

The simplest way would be something like the following (assuming you can use c++11)

std::ofstream out("test.csv");

for (auto& row : temp_matrix) {
  for (auto col : row)
    out << col <<',';
  out << '\n';
}

Upvotes: 14

Related Questions