[C++] The code samples show how to read a disk file and skip records until you r
ID: 3875096 • Letter: #
Question
[C++]
The code samples show how to read a disk file and skip records until you reach the location of the records that will contain the values to be used when computing the median value. You can verify that you have the correct results by opening the files with a spreadsheet program (Microsoft Excel, Apple Numbers or OpenOffice Calc), select the column with the balances and compute the median value with one of the statistical functions.
Two data files are provided for this lab. One file has an odd number of records and the other file has an even number of records. You need to run the program for each file and compute the median value for each file.
Sample code is given at the end of this lab assignment. Replace the cout that is inside the while loop with code to compute the total of all balances. After the while loop completes, display the total of all balances.
Balances-1.txt
---------------------------------------------------------
Balances-2.txt
---------------------------------------------------------
Explanation / Answer
The outputs are
Enter the name of the data file: Bal1.txt
There are 117 records in Bal1.txt
The median of Bal1.txt is 1149.42
The total of Bal1.txt is 132927
Enter the name of the data file: Bal2.txt
There are 118 records in Bal2.txt
The median of Bal2.txt is 1146.19
The total of Bal2.txt is 133630
The code for the problem is
#include <iostream>
#include <fstream>
#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;
cout << "Enter the name of the data file: ";
cin >> filename;
// ---- PART 1, Count the number of records in the file
infile.open(filename);
if (infile.fail())
{
cerr << "Unable to open --" << filename << "--, first pass" << endl;
exit(1);
}
while (!infile.eof()) // while not end of file
{
Name[0] = 0; // initialize to 0 to test for empty records/
infile >> AcctNo >> Name >> AcctBal;
if (Name[0] != 0) // ignore empty records
{
recordCount++;
total+=AcctBal;
}
}
infile.close();
cout << "There are " << recordCount << " records in " << filename << 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
// ---- PART 3, open the file, skip leading records, determine the mean
infile.open(filename);
while (recordsToSkip>0) // while not end of file
{
Name[0] = 0; // initialize to 0 to test for empty records/
infile >> AcctNo >> Name >> AcctBal;
if (Name[0] != 0)
recordsToSkip--;
}
median=AcctBal;
// - - - - - You need to complete the program
// Display the results
cout << "The median of " << filename << " is " << median << endl << endl;
cout << "The total of " << filename << " is " << total << endl << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.