Change code to read a 3x3 matrix to an nxn matrix from a file

Hi.

The code below reads in a 3x3 matrix, how can I modify it so that it will read in an nxn matrix?

Thank you for your help,
Harry

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
54
55
56
57
58
59
60
61
62
63



#include <iomanip>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>



//int rowA = 0;
//int colA = 0;

using namespace std;

int main() {

	
	
	vector < vector <double> > data;

	ifstream myFile ( "test_data.txt" );

	if(myFile.is_open()) {
		//file is open for reading
		cout << "Sucessully opened the file\n";

	while( !myFile.eof()) {  //while not at the end of the file

		double x1t, x2t, x3t;

		myFile >> x1t >> x2t >> x3t;

		vector <double> temp;


		temp.push_back(x1t);
		temp.push_back(x2t);
		temp.push_back(x3t);

		data.push_back(temp);
			
	}
		myFile.close();

	} else {
		cout << "Unable to open file\n";
		exit(1);
	}

	for ( int i = 0; i < data.size() - 1; i++ ) {
		printf("x1 = %3.2f, x2 = %3.2f, x3 = %3.2f\n", data[i][0], data[i][1], data[i][2]);
	}

	

	return 0;



}


the data looks like this..

a b c
d e f
g h i
The simple solution is to loop nxn times and read and push one number at a time.
Topic archived. No new replies allowed.