saving data to file in array format

Hi I apologize in advance I am a newbie to posting and c++.
I am writing a program to analyze data for a physics experiment; we will have an array that has about 400 columns and about 1000 rows. I need to analyze the data for delta functions basically array[1][0]-array[0][0]....and so forth all the way to array [1000][0] then do the same for the remaining columns ad rows. I am able to solve de delta function for all the rows and columns but when I ofstream to file all the numbers are in one row one column format. is there a way that I can save the information in a txt. file format in a two by two matrix I.E.

[delta row 1] [delta row 2] [delta row 3] [delta row 4]
[delta row 1] [delta row 2] [delta row 3] [delta row 4]
[delta row 1] [delta row 2] [delta row 3] [delta row 4]

as well is this a simple endeavor or a advance project?

this is the code I have so far;
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib> // you needed for you runing the test file input
#include <iomanip>
#include <array>
#include <stdio.h>
#include <sstream>


int rowA = 0;
int columnA = 0;

using namespace std;


int main()

{


string lineA;
double x;
double arrayA[1000][100] = { { 0 } };
string fileName;
ifstream fileIN;
double arrayB[100][100] = { { 0 } };





cout << "Enter the file name to be analyzed: " << endl;
cin >> fileName;

fileIN.open(fileName);

// Error check of file

if (fileIN.fail()){

cout << "There was an Error reading file: " << fileName << endl;

exit(1);
}



// Reading the Data File

cout << "\n" << endl;
while (fileIN.good()){
while (getline(fileIN, lineA)){
istringstream streamA(lineA);
columnA = 0;
while (streamA >> x){
arrayA[rowA][columnA] = x;
columnA++;

}
rowA++;
}
}

cout << "# of Rows = " << rowA << endl;
cout << "# of Columns = " << columnA << endl;
cout << "" << endl;

fileIN.close();

double largest;
int col;
int row;


/*Checks the column for larges number */
for (col = 0; col < columnA; col++){

largest = arrayA[0][col];

for (row = 0; row < rowA; row++){

if (largest <= arrayA[row][col]){

largest = arrayA[row][col];
//arrayB[col] = arrayA[row][col];

}


}

cout << "the largest number in colunm " << col + 1 << " = " << largest << endl;

}


/*Checks the row for larges number */

double largest2;

for (row = 0; row < rowA; row++){

largest2 = arrayA[row][0];
for (col = 0; col < columnA; col++){

if (largest2 <= arrayA[row][col]){

largest2 = arrayA[row][col];



}
}

cout << "the largest number in the row " << row + 1 << " = " << largest2 << endl;
}


// This Calculates The Delta of fist Column (i want to use column one for time)
double timeDelta;

for (col = 0; col < 1; col++){
for (row = 0; row < rowA - 1; row++){


timeDelta = arrayA[row][col];
// cout << timeDelta << endl;


cout << "Delta Time = ";
cout << arrayA[row + 1][col] - arrayA[row][col] << endl;
}

}

// This Calculates Delta A1 to A(infinity) of each row so it checks A1-A2, A2-A3 and so forth where A's of enth is each row per column

double g;
double s;
double delta;
for (row = 0; row < rowA - 1; row++){

for (col = 1; col < columnA; col++){

g = arrayA[row + 1][col];
s = arrayA[row][col];
delta = s - g;
arrayB[row][col] = arrayA[row][col] - arrayA[row + 1][col];

}
cout << endl;
}

// my attempt to save data to file in array method

string saveFile;
ofstream FileToBeSaveUnderThisName;

cout << "File Output Name; ";
cin >> saveFile;
FileToBeSaveUnderThisName.open(saveFile);

for (int i = 0; i < rowA; i++){
for (int j = 1; j < columnA; j++){

cout << right << setw(10) << arrayB[i][j] << "";

}

cout << endl;
}

FileToBeSaveUnderThisName.close();




return 0;

}



First of all, use the code tags to format your code next time: It's much easier to read.

When you write out your matrices, just put a newline after the data where you want your line to end:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

for( int i = 0; i < rowA; i++ )
    {
    for( int j = 0; j < columnA; j++ )
        {
        cout << right << setw( 10 ) << "[" << arrayB[ i ][ j ] << "]";

        if( j % 4 )            /*   (Every fourth iteration of j)    */
            cout << endl;

        }    /* for( j < columnA )    */

    }    /*    for( i < rowA )    */



Last edited on
Topic archived. No new replies allowed.