Please write a C++ program to read a list of
numbers from a datafile, and calculate the average, lowest and
largest number.
Please use the following command to copy the datafile scores.txt to
your home directory:
Please use end-of-file loop to read all the
floating-point numbers from the datafile scores.txt.
Please display the average, lowest and largest number of all the
numbers in the datafile.
Please create a function to process the datafile, and call the
dataProcessing function in the main function.
/**********************************************/
Where we have to save the input file ..???
/**********************************************/
// scores.txt (Input file)
78
89
98
87
76
65
55
99
87
54
/**********************************************/
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
// function declarations
void dataProcessing(double nos[], int total_num, double& min,
double& max, double& avg);
int main()
{
// Declaring variables
int total_num=0;
double min, max;
double avg;
string filename="scores.txt";
// defines an input stream for the data file
ifstream dataIn;
double num;
dataIn.open(filename.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
//Reading the data from the file
while(dataIn>>num)
{
total_num++;
}
dataIn.close();
// Creating array dynamically
double* nos = new double[total_num];
dataIn.open(filename.c_str());
for(int i=0;i<total_num;i++)
{
dataIn>>num;
nos[i]=num;
}
dataIn.close();
dataProcessing(nos, total_num, min, max, avg);
// Displaying the output
cout << "Minimum :" << min << endl;
cout << "Maximum :" << max << endl;
cout << "Average :" << avg << endl;
}
return 0;
}
// This function will find out the minimum , maximum and average
score in array
void dataProcessing(double nos[], int total_num, double& min,
double& max, double& avg)
{
min=nos[0];
max=nos[0];
double sum=0;
for(int i=0;i<total_num;i++)
{
if(min>nos[i])
{
min=nos[i];
}
if(max<nos[i])
{
max=nos[i];
}
sum+=nos[i];
}
avg=sum/total_num;
}
/***********************************************/
/***********************************************/
Output:
/***********************************************/
Get Answers For Free
Most questions answered within 1 hours.