Mean and Median of two different files.txt.

Write your question here.
somebody please help, i've been on this since yesterday,it was compiling before but the calculations were wrong, now it's not even compiling.
please please help.

The program is suppose to read into two files and calculate the mean and median of each file.

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
96
97
98
99
  
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
using namespace std;


float calc_mean(int data[],int size)
{
	float sum, mean = 0.0;
	for (int i = 0; i < size; i++)
	{
		sum += data[i];
	}
	mean =((float)sum)/size;
	return mean;
}
 // function to find median
float median(float data[],int size)
{


	if(size % 2 == 0);
	{
		int median = data[size/2];
	}

	{
        median = data[size/2] + data[size/2-1]/2.0;
	}
	return median;
}
int main()
{

	string line;
	float data_of_fileA[100],data_of_fileB[105];
	int size_A = 0,size_B = 0;
	ifstream myfile ("fileA.txt");
	if (myfile.is_open())
	{
		while ( getline (myfile,line) )
		{
			size_A++;
		}
		//data_of_fileA = new float[size_A]
		myfile.close();
		ifstream myfile1 ("fileA.txt");
		float a;
		int flag = 0;
		while (myfile1 >> a)
		{
		    data_of_fileA[flag]= a;
		    flag++;
		}
		myfile1.close();
	}
	ifstream myfileb ("fileB.txt");
	if (myfileb.is_open())
	{
		while ( getline (myfileb,line) )
		{
			size_B++;
		}

		myfileb.close();
		ifstream myfileb1 ("fileB.txt");
		float a;
		int flag = 0;
		while (myfileb1 > a)
		{
		    data_of_fileB[flag]= a;
		    flag++;
		}
		myfileb1.close();
	}

	std::sort(data_of_fileA, data_of_fileA + size_A, std::greater<float>());
	std::sort(data_of_fileB, data_of_fileB + size_B, std::greater<float>());

	float mean_fileA,mean_fileB,median_fileA,median_fileB;
	mean_fileA = calc_mean(data_of_fileA,size_A);
	mean_fileB = calc_mean(data_of_fileB,size_B);
	median_fileA = median(data_of_fileA,size_A);
	median_fileB = median(data_of_fileB,size_B);
	cout<<"Mean of fileA : "<<mean_fileA<<endl;
	cout<<"Mean of fileB : "<<mean_fileB<<endl;
	cout<<"Median of fileA : "<<median_fileA<<endl;
	cout<<"Median of fileB : "<<median_fileB<<endl;
	return 0;
}

Last edited on
Why have you not included the compiler errors?
Let's address the compilation errors, and let's start by looking at your float median(float data[], int size) function [sic]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float median(float data[],int size)
{


	if(size % 2 == 0);
	{
		int median = data[size/2];
	}

	{
        median = data[size/2] + data[size/2-1]/2.0;
	}
	return median;
}


On line 5, the condition of your if-control structure is immediately followed by a semi-colon (;). This results in the if-control structure doing nothing (empty body) when the condition is satisfied. Remove the semi-colon.

On line 7, you instantiate a local integer named 'median'. You then attempt to access this integer on line 11 despite the fact that it is out of scope. You'll have to declare the integer before the if-control structure.

On line 9, you're (presumably) missing a matching else to your if on line 5.

On line 13, you're attempting to return 'median', which is out of scope. This doesn't work for the same reason I've described in your second error - correcting it should resolve this issue as well.

Let's look at your main function [sic]:

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
int main()
{

	string line;
	float data_of_fileA[100],data_of_fileB[105];
	int size_A = 0,size_B = 0;
	ifstream myfile ("fileA.txt");
	if (myfile.is_open())
	{
		while ( getline (myfile,line) )
		{
			size_A++;
		}
		//data_of_fileA = new float[size_A]
		myfile.close();
		ifstream myfile1 ("fileA.txt");
		float a;
		int flag = 0;
		while (myfile1 >> a)
		{
		    data_of_fileA[flag]= a;
		    flag++;
		}
		myfile1.close();
	}
	ifstream myfileb ("fileB.txt");
	if (myfileb.is_open())
	{
		while ( getline (myfileb,line) )
		{
			size_B++;
		}

		myfileb.close();
		ifstream myfileb1 ("fileB.txt");
		float a;
		int flag = 0;
		while (myfileb1 > a)
		{
		    data_of_fileB[flag]= a;
		    flag++;
		}
		myfileb1.close();
	}

	std::sort(data_of_fileA, data_of_fileA + size_A, std::greater<float>());
	std::sort(data_of_fileB, data_of_fileB + size_B, std::greater<float>());

	float mean_fileA,mean_fileB,median_fileA,median_fileB;
	mean_fileA = calc_mean(data_of_fileA,size_A);
	mean_fileB = calc_mean(data_of_fileB,size_B);
	median_fileA = median(data_of_fileA,size_A);
	median_fileB = median(data_of_fileB,size_B);
	cout<<"Mean of fileA : "<<mean_fileA<<endl;
	cout<<"Mean of fileB : "<<mean_fileB<<endl;
	cout<<"Median of fileA : "<<median_fileA<<endl;
	cout<<"Median of fileB : "<<median_fileB<<endl;
	return 0;
}


On line 38, std::ifstream doesn't have an operator '>' accepting a float. What you meant to write was '>>', std::ifstream's extraction operator.

On lines 50 and 51, your function 'calc_mean' expects an array of integers, not floats.

*EDIT* this is not a compilation error, but you should probably address this as well. Your 'calc_mean' function (I've changed it to accept an array of float's to reflect my earlier suggestion):
1
2
3
4
5
6
7
8
9
10
float calc_mean(float data[],int size)
{
	float sum, mean = 0.0;
	for (int i = 0; i < size; i++)
	{
		sum += data[i];
	}
	mean =((float)sum)/size;
	return mean;
}


On line 3, you declare 'sum'. It is not initialized. After the for-loop on line 8, you use 'sum' to assign a value to 'mean'. If I pass a value of zero as the size of my array to this function, the for-loop will not run and 'sum' will not have a value, which means it will be used without being initialized.
Last edited on
oh wow, i couldn't see all that. thank you so much!
If I pass a value of zero as the size of my array to this function, the for-loop will not run and 'sum' will not have a value, which means it will be used without being initialized.

And even if the array size was non-zero, it would still be unitialized and possibly lead to an invalid sum.
And even if the array size was non-zero, it would still be unitialized and possibly lead to an invalid sum.

Ha! Good catch. Should have probably started with that :)
i fixed the compiling issues and now the output is wrong its giving me garbage for the mean of both files and o for the median of both files.

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
96
97
98
99
100
101
102
103
104
105



#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>


using namespace std;


float calc_mean(float data[],int size)
{
	int sum, mean;
	for (int i = 0; i < size; i++)
	{
		sum += data[i];
	}
	mean =((float)sum)/size;
	return mean;
}
 // function to find median
float calc_median(float data[],int size)
{
    int median;


	if(size % 2 == 0)
	{
		 median = data[size/2];
	}
    else
	{
        median = data[size/2] + data[size/2-1]/2.0;
	}
	return median ;
}
int main()
{

	string line;
	float data_of_fileA[100],data_of_fileB[105];
	int size_A = 0,size_B = 0;
	ifstream myfile ("fileA.txt");
	if (myfile.is_open())
	{
		while ( getline (myfile,line) )
		{
			size_A++;
		}
//data_of_fileA = new float[size_A]
		myfile.close();
		ifstream myfile1 ("fileA.txt");
		float a;
		int flag = 0;
		while (myfile1 >> a)
		{
		    data_of_fileA[flag]= a;
		    flag++;
		}
		myfile1.close();
	}
	ifstream myfileb ("fileB.txt");
	if (myfileb.is_open())
	{
		while ( getline (myfileb,line) )
		{
			size_B++;
		}

		myfileb.close();
		ifstream myfileb1 ("fileB.txt");
		float a;
		int flag = 0;
		while (myfileb1 >> a)
		{
		    data_of_fileB[flag]= a;
		    flag++;
		}
		myfileb1.close();
	}

	std::sort(data_of_fileA, data_of_fileA + size_A, std::greater<float>());
	std::sort(data_of_fileB, data_of_fileB + size_B, std::greater<float>());

	int mean_fileA,mean_fileB;
	int median_fileA,median_fileB;
	mean_fileA = calc_mean(data_of_fileA,size_A);
	mean_fileB = calc_mean(data_of_fileB,size_B);
	median_fileA = calc_median(data_of_fileA,size_A);
	median_fileB = calc_median(data_of_fileB,size_B);
	cout<<"Mean of fileA : "<<mean_fileA<<endl;
	cout<<"Mean of fileB : "<<mean_fileB<<endl;
	cout<<"Median of fileA : "<<median_fileA<<endl;
	cout<<"Median of fileB : "<<median_fileB<<endl;
	return 0;
}

Some variables are still unitialized. Variables start off witha random value unless you set them to some value. So if you want a variable to start with 0, you must assign 0 to that variable.
Topic archived. No new replies allowed.