Matrix doesn't display properly in Linux only

Hello again,
I'm a beginner in C++, and maybe in Linux too.
I don't know why my program behaves a little different in Linux.

I am trying to output a matrix of double numbers in a fashionable manner in my console, and log them too to a text file. In windows, my console outputs a nice matrix of 12-digit doubles, the text file also outputs the same matrix. But in linux, it's not. The terminal outputs the matrix OK but the text file prints the numbers in irregular positions (which doesn't look like a matrix anymore).

I hope you can help me with this.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void display() {
        
        ofstream logfile("matrix.log");
	cout.precision(12);
	logfile.precision(12);	

	for (int i=0; i<32; i++) {
                // the row label
		if ((i+1)!=32) {
                        // 0 should be row 1
			cout << (i+1) << "\t";
		} else {
			cout << "Total\t";
		}
                // the data
		for (int j=0; j<13; j++) {
			cout << myMatrix[i][j] << "\t";
		}
		cout << endl;
	}
}


myMatrix[32][13] is an array of array of double numbers. 32 rows and 13 columns.

My output in the textfile is like this in Windows:
1
2
3
4
5
6
7
8
9
1	0		0		0		0		0		0		0		0		0		0		0		0		0		
2	900		0		0		0		0		0		0		0		0		0		0		0		900		
3	0		0		0		0		0		0		0		0		0		0		0		0		0		
4	1200		0		0		0		0		0		0		0		0		0		0		0		1200		
5	0		0		0		0		0		0		0		0		0		0		0		0		0		
6	1500		0		0		0		0		0		0		0		0		0		0		0		1500		
7	900		0		0		0		0		0		0		0		0		0		0		0		900		
8	300		0		0		0		0		0		0		0		0		0		0		0		300		
...


My output looks like this in the text file when run in linux:
1
2
3
4
5
1	9710700	97107000	971070000	9710700000	97107000000	971070000000	9710700000000	97107000000000	971070000000000	9710700000000000	97107000000000000	971070000000000000	9710700000000000009710700	
12	97107000000000000097107009751800	971070000000000000971070097518000	9710700000000000009710700975180000	97107000000000000097107009751800000	971070000000000000971070097518000000	9710700000000000009710700975180000000	97107000000000000097107009751800000000	971070000000000000971070097518000000000	9710700000000000009710700975180000000000	97107000000000000097107009751800000000000	971070000000000000971070097518000000000000	9710700000000000009710700975180000000000000	97107000000000000097107009751800000000000009751800	
123	971070000000000000971070097518000000000000097518009615900	9710700000000000009710700975180000000000000975180096159000	97107000000000000097107009751800000000000009751800961590000	971070000000000000971070097518000000000000097518009615900000	9710700000000000009710700975180000000000000975180096159000000	97107000000000000097107009751800000000000009751800961590000000	971070000000000000971070097518000000000000097518009615900000000	9710700000000000009710700975180000000000000975180096159000000000	97107000000000000097107009751800000000000009751800961590000000000	971070000000000000971070097518000000000000097518009615900000000000	9710700000000000009710700975180000000000000975180096159000000000000	97107000000000000097107009751800000000000009751800961590000000000000	971070000000000000971070097518000000000000097518009615900000000000009615900	
1234
... and a whole lot more


Thank you so much in advance for your usual help.
I'm not sure your printing the rows right

When you print cout does it have to be i + 1

After I run it on my comp, I'm going to repost an answer.

-- edit can you post the whole code?--
Last edited on
You should probably be using vector<vector<double> > instead of an array, but I think this does what you want (and it is compiled in linux):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>

using namespace std;

void fillmat(double mat[][13], int m, int n, istream *is=0);
void printmat(double mat[][13], int m, int n, ostream &os=cout);

int main()
{
    double mat[32][13];
    
    fillmat(mat,32,13);
    
    printmat(mat,32,13);
    
    ofstream logOS("log.txt");
    printmat(mat,32,13,logOS);
    
    logOS.close(); /*IMPORTANT*/
    return 0;
}

void fillmat(double mat[][13], int m, int n, istream *is)
{
    if(is){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                (*is) >> mat[i][j];
            }
        }
    }else{
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                mat[i][j]=(double)(i+1)/(double)(j+1);
            }
        }
    }
}

void printmat(double mat[][13], int m, int n, ostream &os)
{
    os.precision(4);
    for(int i=0; i<m; i++){
        if(i!=m-1) os << i << ":\t";
        else os << "total:\t";
        for(int j=0; j<n; j++){
            os << mat[i][j] << "\t";
        }
        os << endl;
    }
}


EDIT:
You could also try the following to generalize the functions operating on your array:
1
2
3
4
5
6
7
8
9
10
template<int m, int n>
inline void display(double (&array)[m][n], ostream &os=cout){ printmat(&array[0][0],m,n,os); }
//...function definitions
int main()
{
    //...setup
    display(mat);
    //...finish
}

This should allow you to bypass sending the heigth and width of the matrix.

OOPS!: one more IMPOTANT thing!!!!
I noticed when I first read your code that you did not close your file (I forgot to as well).
This is important and weird things can happen if you don't do it!


1
2
3
4
5
6
7
8
9
10

int main() //or whatever context you opened the file in
{
    ofstream fout("foo.txt");

    //do stuff

    fout.close();
}

Last edited on
qtdenise: I think more important than how you're outputting the matrix is how you're reading the matrix from the file.

OOPS!: one more IMPOTANT thing!!!!
I noticed when I first read your code that you did not close your file (I forgot to as well).
This is important and weird things can happen if you don't do it!
The destructor of std::fstream automatically closes the file. Obviously, the destructor is called when the object goes out of scope. In this case, when the function returns. Obviously, this doesn't apply to object constructed on the heap.
You only need to manually close the file if you're going to open it again, or perhaps if you want to release the resource as soon as possible.
I guess I should have known that, but it must be some leftover C programming paranoia.

Isn't it at least good style to close the file?
Topic archived. No new replies allowed.