/*---------------------------------------------------------*/ /* Program chapter
ID: 3595637 • Letter: #
Question
/*---------------------------------------------------------*/
/* Program chapter4_8 */
/* */
/* This program computes a linear model for a set */
/* of altitude and ozone mixing ratio values. */
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
// Declare and initialize objects.
int count=0;
double x, y, first, last, sumx=0, sumy=0, sumx2=0,
sumxy=0, denominator, m, b;
string filename;
ifstream zone1;
// Open input file.
zone1.open(filename.c_str());
if(zone1.fail())
cerr << "Error opening input file ";
else
{
// While not at the end of the file,
// read and accumulate information.
zone1 >> x >> y;
while ( !zone1.eof() )
{
++count;
if (count == 1)
first = x;
sumx += x;
sumy += y;
sumx2 += x*x;
sumxy += x*y;
zone1 >> x >> y;
}
last = x;
// Compute slope and y-intercept.
denominator = sumx*sumx - count*sumx2;
m = (sumx*sumy - count*sumxy)/denominator;
b = (sumx*sumxy - sumx2*sumy)/denominator;
// Set format flags
cout.setf(ios::fixed);
cout.precision(2);
// Print summary information.
cout << "Range of altitudes in km: ";
cout << first << " to " << last << endl << endl;
cout << "Linear model: ";
cout << "ozone-mix-ratio = " << m << " altitude + "
<< b << endl;
// Close file and exit program.
zone1.close();
}
return 0;
}
/*---------------------------------------------------------*/
1. add statements to the pregram so that it allows you to enter an altitude in Km, and then uses the model to stimate a corresponding ozone mix ratio.
2.In addition, calculate the average percentage error (APE) of the model. i.e. calculate the PE for each data point and then get the average.
3. Based on the APE, print the range of acceptable output for what user entered in part 1.
Attached are the source code and the data file.
IMPORTANT NOTE1: Your solution should work for any file, as the given code does. Zone1 is just a sample to test your code. Don't you think of a solution that just works for this file and nothing else, OK?
IMPORTANT NOTE2: You cannot use arrays. We are not there yet.
Explanation / Answer
#include <fstream>
#include <string>
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
// Declare and initialize objects.
int count=0;
double x, y, first, last, sumx=0, sumy=0, sumx2=0,
sumxy=0, denominator, m, b;
string filename;
cout<<"Enter the filename";
cin>>filename;
ifstream zone1,zone2;
// Open input file.
zone1.open(filename.c_str());
if(zone1.fail())
cerr << "Error opening input file ";
else
{
// While not at the end of the file,
// read and accumulate information.
zone1 >> x >> y;
while ( !zone1.eof() )
{
++count;
if (count == 1)
first = x;
sumx += x;
sumy += y;
sumx2 += x*x;
sumxy += x*y;
zone1 >> x >> y;
}
last = x;
// Compute slope and y-intercept.
denominator = sumx*sumx - count*sumx2;
m = (sumx*sumy - count*sumxy)/denominator;
b = (sumx*sumxy - sumx2*sumy)/denominator;
// Set format flags
cout.setf(ios::fixed);
cout.precision(2);
// Print summary information.
cout << "Range of altitudes in km: ";
cout << first << " to " << last << endl << endl;
cout << "Linear model: ";
cout << "ozone-mix-ratio = " << m << " altitude + "
<< b << endl;
// Close file and exit program.
zone1.close();
}
double abs_m=m;
double pe;
double ape=0.0;
//cout<<abs_m<<endl;
cout<<"Enter the altitude values";
cin >> x;
//cin>>y;
++count;
if (count == 1)
first = x;
sumx += x;
sumy += y;
sumx2 += x*x;
sumxy += x*y;
last = x;
// Compute slope and y-intercept.
denominator = sumx*sumx - count*sumx2;
m = (sumx*sumy - count*sumxy)/denominator;
b = (sumx*sumxy - sumx2*sumy)/denominator;
// Set format flags
cout.setf(ios::fixed);
cout.precision(2);
// Print summary information.
cout << "Range of altitudes in km: ";
cout << first << " to " << last << endl << endl;
cout << "Linear model: ";
cout << "ozone-mix-ratio = " << m << " altitude + "
<< b << endl;
double user_m=m;
int counter=0;
//cout<<filename;
zone2.open(filename.c_str());
if(zone2.fail())
cerr << "Error opening input file ";
else
{
while ( !zone2.eof() )
{
zone2 >> x >> y;
++count;
cout<<x<<endl;
if (count == 1)
first = x;
sumx += x;
sumy += y;
sumx2 += x*x;
sumxy += x*y;
last = x;
// Compute slope and y-intercept.
denominator = sumx*sumx - count*sumx2;
m = (sumx*sumy - count*sumxy)/denominator;
b = (sumx*sumxy - sumx2*sumy)/denominator;
// Set format flags
cout.setf(ios::fixed);
cout.precision(2);
pe = abs(abs_m-m)/abs_m;
pe = pe*100;
//cout<<"M is"<<m<<endl;
//cout<<abs_m-m<<endl;
//cout<<pe<<endl;
ape+=pe;
counter++;
//zone2 >> x >> y;
}
//cout<<ape<<counter;
ape = ape/counter;
cout<<"APE "<<ape<<endl;
}
cout<<"Output range for the user "<< (user_m-(user_m*ape/100))<<"to "<<user_m+(user_m*ape/100)<<endl;
zone2.close();
getchar();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.