2D array

Say for example, I have a 4x4 matrix of numbers in a file.
1
2
3
4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4


How do I put them in a 2d array and then display them?

This is what I have tried so far and I thought it might work but it doesn't.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    int arr[4][4];
    ifstream file("numbers.txt");

    for(int row=0;row<4;row++){
        for(int col=0;col<4;col++)
            file << arr[row] << arr[col];
    }

    for(int row=0;row<4;row++){
        for(int col=0;col<4;col++)
            cout << arr[row] << " " << arr[col];
        cout << endl;

    }

}
closed account (j3Rz8vqX)
Array documentation:
http://www.cplusplus.com/doc/tutorial/arrays/

File documentation:
http://www.cplusplus.com/doc/tutorial/files/

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    int arr[4][4];
    ifstream file("numbers.txt");

    for(int row=0;row<4;row++){
        for(int col=0;col<4;col++){
            file >> arr[row][col];
            cout << arr[row][col]<<' ';
        }
        cout << endl;
    }
    return 0;
}
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Process returned 0 (0x0)   execution time : 0.009 s
Press any key to continue.
Uhh, need help with another thing.
I'm having a practice with using Classes and the 2d array problem.
Everything seems to work fine until the output.

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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>

using namespace std;

class myClass{
    private:
        int arr[4][4];
    public:
        myClass();
        void print();
};

myClass::myClass(){
    int arr[4][4] =
    {
       {1,2,3,4},
       {1,2,3,4},
       {1,2,3,4},
       {1,2,3,4}
    };
};

void myClass::print(){
    for(int i=0;i<4;i++){
        for(int a=0;a<4;a++)
            cout << arr[i][a] << ' ';
        cout << endl;
    }
};

int main(){
    fstream file("numbers.txt");
    myClass A;
    A.print();



}


Here's the output:

4660232 4705716 2686428 4289894
4640848 2686524 1 4705716
6 0 6 16
1 4200720 4635564 4683024

Process returned 0 (0x0)   execution time : 0.687 s
Press any key to continue.


I'm not getting the:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
closed account (j3Rz8vqX)
Quick response:

You never initialized your array.

Line 15 is a completely different array, not the one available in your class' private; available only to the scope of the constructor.

Edit:
Only array declaration can be declared in that manner.
So, to preserve your efforts, possibly do a simple work around like the below.

A possible solution:
1
2
3
4
5
6
7
8
9
10
11
12
myClass::myClass(){
    int temp[4][4] =
    {
       {1,2,3,4},
       {1,2,3,4},
       {1,2,3,4},
       {1,2,3,4}
    };
    for(int i=0;i<4;++i)
        for(int j=0;j<4;++j)
            arr[i][j]=temp[i][j];
}
Last edited on
Mmm, now to try doing that by reading the numbers from a file @.@
closed account (j3Rz8vqX)
In the above, we read data from one array to populate another.

For a file, it would be a similar process.

Read the data from the file, to populate your array.

How the data is read is dependent on the format that the data is within the file.

The above post:
1
2
3
4
5
6
7
    for(int row=0;row<4;row++){
        for(int col=0;col<4;col++){
            file >> arr[row][col];
            //cout << arr[row][col]<<' ';
        }
        //cout << endl;
    }

Would already do so. The printouts are simply printing out the data that is being recorded into your array.
Topic archived. No new replies allowed.