I wan to wirte in cout << ... to have the matrix in output in the same format that I wrote it in text file like this:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7

class Classname {   
    double data[3][3];  
public:
    void Read(char *fname);
    void Writedata();
}
void Classname::Read(char *fname)
{   int i, j;
    ifstream fin(fname);
        fin >> 3;
// read the matrix
    for(i= 0; i<3; i++)
        for(j= 0; j<3; j++)
            fin >> data[i][j];
}

void Classname::Writedata(char *fname)
{   // writes model to cout in same format it was read in

cout << "matrix = "  << endl;
cout << matrix[0][0] << " " << matrix[0][1] << " " << matrix[0][2] << endl;
cout << matrix[1][0] << " " << matrix[1][1] << " " << matrix[1][2] << endl;
cout << matrix[2][0] << " " << matrix[2][1] << " " << matrix[2][2] << endl;

};

void pbsolve(char *fname)
{
    cout << "pbsolve " << fname << endl;

    Classname pb;

    np.Read(fname);
    np.Writedata(fname);

}
Member Avatar for Search_not

Try something similar to this... Insert your own values and arrays where needed. Change the condition of the for-loops if your array is larger than 3.

for(int i = 0; i < 3; i++){   // i = row
    for(int j = 0; j < 3; j++){  //j = column
        cout << matrix[i][j] << " ";   
    }
    cout << endl;  
}

Thanks. I have another format of data in text file like this:

1
2 3

2
4 5
6 7

3
8 9
3 5
2 6

Main number mentioned in the code are (1, 2 ,3) and every main number has some amount of rows. The main number 1 has one row, and main number 2 has two rows, and ...
I want to have in output in the same format again. The problem is that it wants an initial number for "numberof_rows". Can you please see the Classname::Writedata() loop?

class Classname {   
    double data[3][3];  
public:
    void Read(char *fname);
    void Writedata();
}
void Classname::Read(char *fname)
{   int i, j;
    ifstream fin(fname);
        fin >> 3;

        int numberof_rows;
    int k= 0;

    for(i= 0; i<mainnumber; i++) {
        fin >> numberof_rows;

        row_per_mainnumber[i]= numberof_rows;
        first_row[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // read each row
            fin >> first_one[k];
            fin >> second_one[k];
            k++;
        }
    }
    total_rows= k;
}

void Classname::Writedata()
{   // writes model to cout in same format it was read in

        int numberof_rows;
    int k= 0;

    for(i= 0; i<mainnumber; i++) {

        cout << i+1 << endl;

        row_per_mainnumber[i]= numberof_rows;
        first_row[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // write each line

            cout << first_one[k] << " " << second_one[k] << " ";
            cout << endl;  
            k++;
        }
    }
    total_rows= k;
}
Member Avatar for Search_not

Try declaring your numberof_rows as an instance variable so that when you assign it a value in Classname::Read then it keeps that value. e.g.

class Classname{
    double data[3][3];
    int numberof_rows;   //now a private instance variable

  public:
    void Read(char *fname);
    void Writedata();
}

void Classname::Read(char *fname){
    //read data from file
    //assign value to numberof_rows
}

void Classname::Writedata(){
    //print out values using numberof_rows
}

I would advise that you change the format of the text file for easier reading and use of the data stored within

Does it mean I need to add only (int numberof_rows;) to the Classname? It didn't work. Maybe this one is more accurate.
"Mainnumber" is the first number of each matrix (1,2,3) and for each "mainnumber" (i), the number of rows are determined for each (i) by ("nof_row_per_mainnumber[i]"= numberof_rows;).
For every "Mainnumber" (matrix) it starts from the first row ("first_rows[i]"= k;).
1
2 3

2
4 5
6 7

3
8 9
3 5
2 6

class Classname {  

        int mainnumber;
        int nof_row_per_mainnumber[3];
        int numberof_rows;
    int first_rows[3];
        double first_array[6];
        double second_array[6];
        int total_rows;

public:
    void Read(char *fname);
    void Writedata();
}
void Classname::Read(char *fname)
{   int i, j;
    ifstream fin(fname);
        fin >> 3;
        int numberof_rows;
    int k= 0;
    for(i= 0; i<mainnumber; i++) {
        fin >> numberof_rows;
        no_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // read each row
            fin >> first_array[k];
            fin >> second_array[k];
            k++;
        }
    }
    total_rows= k;
}
void Classname::Writedata()
{   // writes model to cout in same format it was read in
        int numberof_rows;
    int k= 0;
    for(i= 0; i<mainnumber; i++) {
        cout << i+1 << endl;
        no_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // write each line
            cout << first_array[k] << " " << second_array[k] << " ";
            cout << endl;  
            k++;
        }
    }
    total_rows= k;
}

Hi,I wrote the code gaian in the previous post, can you please take a look at it? I declared numberof_rows, but I dont know how to assign it a value in Classname::Read.

It is the corrected version:

class Classname {  

        int mainnumber;
        int nof_row_per_mainnumber[3];
        int numberof_rows;
    int first_rows[3];
        double first_array[6];
    double second_array[6];
        int total_rows;

public:
    void Read(char *fname);
    void Writedata();
}
void Classname::Read(char *fname)
{   int i, j;
    ifstream fin(fname);
        fin >> mainnumber;

    int numberof_rows;
    int k= 0;
    for(i= 0; i<mainnumber; i++) {
        fin >> numberof_rows;
        nof_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // read each row
            fin >> first_array[k];
            fin >> second_array[k];
            k++;
        }
    }
    total_rows= k;
}
void Classname::Writedata()
{   // writes model to cout in same format it was read in

    int i, j;
    int numberof_rows;
    int k= 0;

    for(i= 0; i<mainnumber; i++) {
        cout << i+1 << endl;
        nof_row_per_mainnumber[i]= numberof_rows;
        first_rows[i]= k;
        for(j= 0; j<numberof_rows; j++) {    // write each line
            cout << first_array[k] << " " << second_array[k] << " ";
            cout << endl;  
            k++;
        }
    }
    total_rows= k;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.