In: Computer Science
This is the program that use to find the Mean (Average) and Median in C++.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> // used by the exit() functiona
using namespace std;
int main(int argc, char* argv[])
{
// variables to control the disk file
ifstream infile;
char filename[200];
int recordCount = 0;
int recordsToSkip = 0;
// variables for fields of each record in the file
int AcctNo = 0;
char Name[100] = "";
double AcctBal = 0.0;
// varible used to determine the median
double median = 0.0;
double total = 0.0;
double mean = 0.0;
cout << "Enter the name of the data file: ";
cin >> filename;
// ---- PART 1, Count the number of records in the file
// Determine the mean when you know the record count and the total
of all balances
infile.open("C:\\Users\\An\\Desktop\\Balances1.txt");
if (infile.fail())
{
cerr << "Unable to open --" << filename << "--,
first pass" << endl;
exit(1);
}
while (!infile.eof()) // while not end of file
{
if (Name[0] = !0)// initialize to 0 to test for empty
records/
{
total += AcctBal;
recordCount++;
}
Name[0] = 0;
infile >> AcctNo >> Name >> AcctBal;
}
infile.close();
cout << "There are " << recordCount << " records
in " << filename << endl;
mean = total / recordCount;
cout << endl << "The mean value of the balance is "
<< setprecision(3) << fixed << mean <<
endl;
// ---- PART 2, Determine the number of records to skip
if (recordCount %2 == 1)
recordsToSkip = recordCount / 2; // Odd number of records
else
recordsToSkip = recordCount / 2 - 1; // Even number of
records
cout << "recordsToSkip = " << recordsToSkip <<
endl;
// ---- PART 3, open the file, skip leading records, determine
the median
infile.open("C:\\Users\\An\\Desktop\\Balances1.txt");
if (infile.fail())
{
cerr << "Unable to open --" << filename << "--,
second pass" << endl;
exit(1);
}
while (recordsToSkip != 0)
{
infile >> AcctNo >> Name >> AcctBal;
recordsToSkip--;
}
infile >> AcctNo >> Name >> AcctBal;
if (recordCount % 2 == 1)
{
median = AcctBal;
}
else
{
median = AcctBal;
infile >> AcctNo >> Name >> AcctBal;
median = (median + AcctBal) / 2;
}
infile.close();
// Display the results
cout << endl << "The median of " << filename
<< " is " << setprecision(3) << fixed <<
median << endl << endl;
return 0;
}
However, when I use Excel to double check the result. Those outputs are different. I think I have mistakes some where in the program. I need some help from you. Thank you.
This is the .txt file for the Balances ( Balance 1 and Balance 2)
http://program-info.net/C++/downloads/C++MedianOfFile/Balances-1.txt
http://program-info.net/C++/downloads/C++MedianOfFile/Balances-2.txt
Please find your solution below and if doubt comment and do upvote.
CODE:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib> // used by the exit() functiona
using namespace std;
int main(int argc, char* argv[])
{
// variables to control the disk file
ifstream infile;
char filename[200];
int recordCount = 0;
int recordsToSkip = 0;
// variables for fields of each record in the file
int AcctNo = 0;
char Name[100] = "";
//Name[0]=0;
double AcctBal = 0.0;
// varible used to determine the median
double median = 0.0;
double total = 0.0;
double mean = 0.0;
cout << "Enter the name of the data file: ";
cin >> filename;
// ---- PART 1, Count the number of records in the file
// Determine the mean when you know the record count and the total of all balances
infile.open("Balances1.txt");
if (infile.fail())
{
cerr << "Unable to open --" << filename << "--, first pass" << endl;
exit(1);
}
int i=1;
infile >> AcctNo >> Name >> AcctBal;
while (!infile.eof()) // while not end of file
{
//cout<<i++<<" "<<AcctNo <<" "<<Name<<" "<<AcctBal<<endl;
if (Name[0] = !0)// initialize to 0 to test for empty records/
{
total += AcctBal;
recordCount++;
}
Name[0] = 0;
infile >> AcctNo >> Name >> AcctBal;
//cout<<i++<<" "<<AcctNo <<" "<<Name<<" "<<AcctBal<<endl;
}
infile.close();
cout << "There are " << recordCount << " records in " << filename << endl;
// cout<<"Sum"<<total<<endl;
mean = total / recordCount;
cout << endl << "The mean value of the balance is " << setprecision(3) << fixed << mean << endl;
// ---- PART 2, Determine the number of records to skip
if (recordCount %2 == 1)
recordsToSkip = recordCount / 2; // Odd number of records
else
recordsToSkip = recordCount / 2 - 1; // Even number of records
cout << "recordsToSkip = " << recordsToSkip << endl;
// ---- PART 3, open the file, skip leading records, determine the median
infile.open("Balances1.txt");
if (infile.fail())
{
cerr << "Unable to open --" << filename << "--, second pass" << endl;
exit(1);
}
while (recordsToSkip != 0)
{
infile >> AcctNo >> Name >> AcctBal;
recordsToSkip--;
}
infile >> AcctNo >> Name >> AcctBal;
if (recordCount % 2 == 1)
{
median = AcctBal;
}
else
{
median = AcctBal;
infile >> AcctNo >> Name >> AcctBal;
median = (median + AcctBal) / 2;
}
infile.close();
// Display the results
cout << endl << "The median of " << filename << " is " << setprecision(3) << fixed << median << endl << endl;
return 0;
}
OUTPUT: