#include <iostream> #include <fstream> #include <string> using namespace std; in
ID: 3591086 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout<< "This program generates a summery report a data file that has a trailer record with negative values" <<endl;
//Declare and initialize objects.
int num_data_pts(0) , k;
double time, motion, sum (0), max, min;
string filename;
ifstream sensor2;
ofstream report;
//prompt user for name of input file.
cout << "Enter the name of the input file ";
cin >> filename ;
//open file and read the first data point.
sensor2.open(filename.c_str());
if (sensor2.fail())
{
cerr<< "Error opening input file/n ";
exit(1);
}
// Open report file.
report.open("sensor2Report.txt");
sensor2 >> time >> motion;
// Intialize objects using first data point.
max = min =motion;
//Update summary until trailer record read.
do
{
sum +=motion;
if (motion > max)
{
max=motion;
}
if (motion<min)
{
min=motion;
}
num_data_pts++;
sensor2 >> time >> motion;
} while (time >=0);
// Set format flags.
report.setf(ios::fixed);
report.setf(ios::showpoint);
report.precision(2);
//Print summary information.
report << "Number of sensor readings: "
<< num_data_pts << endl
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<<" minimum reading: "
<< min << endl;
// Close files and exit program.
sensor2.close();
report.close();
return 0;
}
Sensor2.dat
0 132.5
0.1 147.2
0.2 148.3
0.3 157.3
0.4 163.2
0.5 158.2
0.6 169.3
0.7 148.2
0.8 137.6
0.9 135.9
-99 -99
Sesor2Report.txt
Number of sensor readings: 10
Average raeding: 149.77
Maximum reading: 169.30
Minimum reading: 132.50
This Program developed the loop contained a condition that testedthe program for the first time that the loop was executed. when the condition was true the max and min values were initialized to the first motion value. if the data values used with these programs were very long, the time required to execute this selection statement could begin to be substantial. one way to avoid this test is to read the first set of data and initialize the objects before entering the loop. this change may required other cahnges in the program
1.Modify the program so that it works correctly after the condition in removed that tests for the first time that the loop is executed.
Explanation / Answer
1) Modified your code ... and changes made as bold statement for quick referrence
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout<< "This program generates a summery report a data file that has a trailer record with negative values" <<endl;
//Declare and initialize objects.
int num_data_pts(0) , k;
int currentReadingIndex = 0; // this variable used to avoid first time check
double time, motion, sum (0), max, min;
string filename;
ifstream sensor2;
ofstream report;
//prompt user for name of input file.
cout << "Enter the name of the input file ";
cin >> filename ;
//open file and read the first data point.
sensor2.open(filename.c_str());
if (sensor2.fail())
{
cerr<< "Error opening input file/n ";
exit(1);
}
// Open report file.
report.open("sensor2Report.txt");
sensor2 >> time >> motion;
// Intialize objects using first data point.
// Here initialize first data data points will happen
max = min = motion;
//Update summary until trailer record read.
do
{
sum +=motion;
if(currentReadingIndex > 0) { // this will avoid reading first time
if (motion > max)
{
max=motion;
}
if (motion<min)
{
min=motion;
}
}
currentReadingIndex++; // this will ensure, next time above condition will execute
num_data_pts++;
sensor2 >> time >> motion;
} while (time >=0);
// Set format flags.
report.setf(ios::fixed);
report.setf(ios::showpoint);
report.precision(2);
//Print summary information.
report << "Number of sensor readings: "
<< num_data_pts << endl
<< sum/num_data_pts << endl
<< "Maximum reading: "
<< max << endl
<<" minimum reading: "
<< min << endl;
// Close files and exit program.
sensor2.close();
report.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.