Question

In: Computer Science

This is the program that use to find the Mean (Average) and Median in C++. #include...

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

Solutions

Expert Solution

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:


Related Solutions

Find the average, median and mode income of the residents Find interquartile range Find the mean...
Find the average, median and mode income of the residents Find interquartile range Find the mean deviation from the mean and median The following data on income of residents generated from County X Min Max No. of people 1000 2000 28 2000 3000 52 3000 4000 45 4000 5000 35 5000 6000 63 6000 7000 130 7000 8000 110 8000 9000 62 9000 10000 55 10000 11000 38 11000 12000 35 12000 13000 50 13000 14000 55 14000 15000 88...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​ (e) answer the given question. Listed below are the weights in pounds of 1111 players randomly selected from the roster of a championship sports team. Are the results likely to be representative of all players in that​ sport's league? 221221     256256     248248     276276     220220     237237     298298     273273     220220     187187     298298    a. Find the mean. The mean is nothing ​pound(s). ​(Type an integer or...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​ (e) answer the given question. Listed below are the weights in pounds of 11 players randomly selected from the roster of a championship sports team. Are the results likely to be representative of all players in that​ sport's league? 246    273    298    259    266    200    260    306    246    248    200 a. Find the mean. The mean is nothing ​pound(s). ​(Type an integer or a...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​ (e) answer the given question. Listed below are the jersey numbers of 11 players randomly selected from the roster of a championship sports team. What do the results tell​ us? 4    47    78    75    10    38    26    55    39    79    6 a. Find the mean. . ​(Type an integer or a decimal rounded to one decimal place as​ needed.)
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the data and then​ (e) answer the given question. Listed below are the amounts​ (dollars) it costs for marriage proposal packages at different sports venues. Are there any​ outliers? 39 60 60 65 65 65 75 85 100 125 175 175 225 234 250 250 350 375 400 450 450 450 450 2500 3000 a. Find the mean. The mean is ​$nothing. ​(Type an integer or a decimal...
a. Find the​ mean, median, and range for each of the two data sets. Find the​ mean, median, and range for faculty parking.
Faculty: 3, 4, 2, 1, 2, 3, 5, 4, 4, 3, 2                     Student: 9, 10, 12, 6, 11, 14, 5, 8, 10, 14, 13                              The following data sets give the ages in years of a sample of cars in a faculty parking lot and a student parking lot at a college. Complete parts​ (a) through​ (e). . a. Find the​ mean, median, and range for each of the two data sets. Find the​ mean, median, and range for faculty parking....
Use Descriptive Statistics in Excel to find the mean, median, range and standard deviation of the...
Use Descriptive Statistics in Excel to find the mean, median, range and standard deviation of the data below. Table 1: Health Expenditures as a Percentage of GDP of countries around the world 3.35 5.96 10.64 5.24 3.79 5.65 7.66 7.38 5.87 11.15 5.96 4.78 7.75 2.72 9.50 7.69 10.05 11.96 8.18 6.74 5.89 6.20 5.98 8.83 6.78 6.66 9.45 5.41 5.16 8.55
Find the (a) mean, (b) median, (c) mode, and (d) midrange. Listed below are the weights...
Find the (a) mean, (b) median, (c) mode, and (d) midrange. Listed below are the weights in pounds of 11 players randomly selected, from the roster of a championship sports team. 274 188 227 200 260 246 279 198 260 278 279 215 271
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data....
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data. An experiment was conducted to determine whether a deficiency of carbon dioxide in the soil affects the phenotype of peas. Listed below are the phenotype codes where 1 equals smooth dash yellow1=smooth-yellow​, 2 equals smooth dash green2=smooth-green​, 3 equals wrinkled dash yellow3=wrinkled-yellow​, and 4 equals wrinkled dash green4=wrinkled-green. Do the results make​ sense? 2 1 1 4 1 1 3 4 2 2 2...
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data....
Find the​ (a) mean,​ (b) median,​ (c) mode, and​ (d) midrange for the given sample data. An experiment was conducted to determine whether a deficiency of carbon dioxide in the soil affects the phenotype of peas. Listed below are the phenotype codes where 1 equals smooth dash yellow​, 2 equals smooth dash green​, 3 equals wrinkled dash yellow​, and 4 equals wrinkled dash green. Do the results make​ sense? 2 2 1 4 3 1 4 1 4 2 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT