passing arrays in functions

Im trying to separate my code up and make it modular so the main program reads a bit easier. I am having problems receiving the array back after I try and fill it in a function. I am most likely doing some simple mistake.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

double datafile ()
{

//FILLING DATA ARRAYS 
// ask user for dat file
 
	ifstream dat;
	do {
		cout << "Input filename where the raw data exist-->";
		char filename [50];
		cin.getline(filename,50);
		dat.open(filename);
		if (!dat.is_open())
		cout << "File Does Not Exist!" << endl ;
	} while (!dat.is_open());

// read from file  and fill arrays 

	double data[10][3]; // array reads angle, w(angle) errw(angle)
	int x = 0; 
	double  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++;
	}

return (data[10][3]);
}

//======================================================================


//======================================================================

double valuesfile ()
{

// SECOND ARRAY FILLING 
// fill arrays for testing models 

	double a,b,c,d,e,f,g,h;
	int x ;

	ifstream table;
	do {
		cout << "Input filename where the data table exist-->";
		char file [50];
		cin.getline(file,50);
		table.open(file);
			if (!table.is_open())
			cout << "File Does Not Exist!" << endl ;
	} while (!table.is_open());


	double values [10][6]; //array from 1967 table values are:
			      // jf , ji,l1,l2,f2,f4 
	x = 0 	;

	while (table >> a >> b >> c >> d >> e >> f){

		values [x][0] = a;
		values [x][1] = b;		
		values [x][2] = c;
		values [x][3] = d;
		values [x][4] = e;
		values [x][5] = f;


		x++;
	}		

	table.close();

return (values[10][6]);
}
//======================================================================
//			MAIN PROG 
//======================================================================

int main () 
{

	double data[10][3] ;
	data[10][3]= datafile;
	double values[10][6] ;
	values[10][6]= valuesfile;

// more stuff that doesn't matter  
Of course your code is invalid. It would be better to allocate dynamically arrays in the two functions and return pointers to them to main.
Last edited on
you cannot access data[10][3] and values[10][6]. You can atmost access data[9][2] and values[9][5].
Also, at present you are not returning arrays, but only a single element from the array. To pass entir arrays, you will have to take them as paramters to the respective functions.
Either dynamically allocate, or pass the arrays as parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void datafile(double data[10][3])
{

}

void valuesfile(double values[10][6])
{

}

int main ()
{
	double data[10][3];
	datafile(data);

	double values[10][6] ;
	valuesfile(values);
}
Topic archived. No new replies allowed.