Filling an Array

I am filling a multidimensional array up from a file and inside the loop when i print the value of what i want it is correct but when i cout outside of the loop it's a different value


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// this is the code
// read from file  and fill arrays 

	float data[9][2]; // array reads angle, w(angle) errw(angle)
	int x = 0; 
	float  a,b,c,d,e,f;

	while (	dat >> a >> b >> c){

		data[x][0] = a;
		data[x][1] = b;
		data[x][2] = c;

		cout << data[x][2] << endl;
		x++;

	}

	dat.close();

		cout << data[0][2] << endl;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// this is the text to screen 

Input filename where the raw data exist-->dat.dat
0.023009
0.017027
0.013554
0.015127
0.010707
0.014515
0.01358
0.016766
0.022473
0.077017
37.5  // this is the number that is funny that i print out after
Input filename where the data table exist-->


here is the text file its reading from

1
2
3
4
5
6
7
8
9
10
17.500000 1.101942 0.023009
37.500000 1.030780 0.017027
55.000000 0.951339 0.013554
71.000000 0.993998 0.015127
90.000000 0.931244 0.010707
109.000000 0.942470 0.014515
125.000000 0.953977 0.013580
142.500000 1.009818 0.016766
162.500000 1.027957 0.022473
180.000000 1.056475 0.077017


p.s. i dont know how to get the screen output on this forum like you guys do, sorry for the confusion..
Last edited on
out of bounds:

data[9][2] means you can use the indexes 0 and 1 (=2 values) only and not the index 2
data[9][2] means you can read 9 lines only. Just count how many lines you have in your file
but doesnt 0 count as well ?? thus making 0 - 9 ten values??
Last edited on
but doesnt 0 count as well ?? thus making 0 - 9 ten values??
exactly

with
float data[9][2];

data[9][0] = ... -> out of bounds. You can only use the indexes 0 to 8 (= 9 values).
AHHH!! Understood

Thanks !
Topic archived. No new replies allowed.