arrays and median functions problem

Hello guys ! thanks alot for this website it helped me alot...I have a problem with making a median function works with this code...my array takes the data from a text file and char*,double* and int* dont work here... please help me

Thanks

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
 #include <iostream>
#include <fstream>
#include <string>
using namespace std;

 int  median(int A[x][0], int size)
{
    if (size %  2 == 0)
        return (data_array[size / 2] + data_array[size / 2 - 1]) / 2;
    else
        return data_array[size / 2];
}

  
int main()
{	
	int size = 1172
	double sum1;
	double sum2;
	double avg1;
	double avg2;
	int  data_array[size][2]; //You can make it dynamic to handle as much as the file has
	ifstream in_file("data.txt", ios::binary);
	//Check if the file is open
	if(!in_file.is_open()){
		cout << "File not opened..." << endl;
		return 1;
	}
	//read the file (two columns) to the array
	for(int i=0; !in_file.eof(); i++)
	{
		in_file >> data_array[i][0];
		in_file >> data_array[i][1];
	}
	
	//Display the array
	for (int i=0; i<size; i++)
	{
		cout << data_array[i][0] << ", " << data_array[i][1] << endl;
	}
	
	for (int x = 0; x < size; x++)
	{
	  sum1 = sum1 + data_array[x][0];
	  avg1 = sum1 / size;
	}
	  cout << endl<< avg1 << endl;
	  
	 for (int y = 0; y < size; y++)
	{
	  sum2 = sum2 + data_array[y][1];
	  avg2 = sum2 / size; 
	}
	  cout << endl<< avg2 << endl;
	
	int median1
	
	median1 = median ( data_array[x][0],size);
	cout << endl << median;
	  
    return 0;
    
}


  
  

.

I assumed you're only trying to find the median for the first column. To get it built and running, I've made and marked modifications to your code with // change

I hope that the changes will be more informative than a long essay, but they are just the mechanical aspects, you should reconsider the following points, a few minutes searching on this site should sort you out:

# Arrays, especially multidimensional arrays
# Passing arguments to functions, especially arrays and scoping, in particular global vs local variables
# vector<> !!!!
# The importance of semi-colons :-)

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

 int  median(int A[], int size) // change
{
    if (size %  2 == 0)
        return (A[size / 2] + A[size / 2 - 1]) / 2; // change
    else
        return A[size / 2]; // change
}


int main()
{
	const int size = 1172; // change
	double sum1;
	double sum2;
	double avg1;
	double avg2;
	int  data_array[size][2]; //You can make it dynamic to handle as much as the file has
	ifstream in_file("data.txt", ios::binary);
	//Check if the file is open
	if(!in_file.is_open()){
		cout << "File not opened..." << endl;
		return 1;
	}
	//read the file (two columns) to the array
	for(int i=0; !in_file.eof(); i++)
	{
		in_file >> data_array[i][0];
		in_file >> data_array[i][1];
	}

	//Display the array
	for (int i=0; i<size; i++)
	{
		cout << data_array[i][0] << ", " << data_array[i][1] << endl;
	}

	for (int x = 0; x < size; x++)
	{
	  sum1 = sum1 + data_array[x][0];
	  avg1 = sum1 / size;
	}
	  cout << endl<< avg1 << endl;

	 for (int y = 0; y < size; y++)
	{
	  sum2 = sum2 + data_array[y][1];
	  avg2 = sum2 / size;
	}
	  cout << endl<< avg2 << endl;

	int median1; // change

	median1 = median ( data_array[0],size); // change
	cout << endl << median1; // change

    return 0;

}
The median function should return a floating point value since, for example, the median of [1,2] is 1.5. To prevent integer division, median() should divide by 2.0, not 2. Finally, to get the right answer to main(), median1 should be defined as a float or double rather than an int.
@dhayden is right.

I wish we had a "like" button or up-vote, for situations like this.
Thanks tipaye :)
Get a room you two
Thank you @tipaye :D :D it worked but if I want to calculate the other column i just change the rank of the array of median1 right ?? and what about doing the standart deviation ?? same way ?

Thanks guys :D
Yeah, just follow the same pattern you used for median. Good luck.
Topic archived. No new replies allowed.