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.