Transcript Data Files

Data Files
ELEC 206
Computer Applications for
Electrical Engineers
Dr. Ron Hayne
Data Files
 Defining File Streams
 Reading Data Files
 Generating a Data File
 Numerical Technique

Linear Regression
 Problem Solving Applied
206_C4
2
File Stream Objects
 File Stream Classes




Derived from Stream Classes
Input: ifstream
Output: ofstream
Header: fstream
 Additional Member Functions



open()
fail()
close()
206_C4
3
ifstream Class
 ifstream Object

ifstream sensor1;
 File Association

sensor1.open("sensor1.dat");
 Define and Initialize

ifstream sensor1("sensor1.dat");
 Error Checking

if( sensor1.fail() )
 File Input

sensor1 >> t >> motion;
206_C4
4
ofstream Class
 ofstream Object

ofstream balloon;
 File Association

balloon.open("balloon.dat");
 Define and Initialize

ofstream balloon("balloon.dat");
 Append to Existing File

balloon.open("balloon.dat", ios::app);
206_C4
5
ofstream Class
 Prompt for Filename




string filename;
cout << "Enter output filename";
cin >> filename;
balloon.open(filename.c_str());
 File Output

balloon << time << ' ' << height << ' '
<< velocity/3600 << endl;
 Close File

balloon.close();
206_C4
6
Reading Data Files
 Specified Number of Records

Counter-controlled Loop
 Trailer or Sentinel Signal

Sentinel-controlled Loop
 End of Data File

End-of-file Loop
206_C4
7
Specified Number of Records
 sensor1.dat
10
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
132.5
147.2
148.3
157.3
163.2
158.2
169.3
148.2
137.6
135.9
sensor1.open(filename.c_str());
if(sensor1.fail())
{
cout << "Error";
}
else
{
sensor1 >> num_data_pts;
for (k=1; k<=num_data_pts; k++)
{
sensor1 >> time >> motion;
...
}
206_C4
8
Trailer or Sentinel Signals
 sensor2.dat
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
-99
132.5
147.2
148.3
157.3
163.2
158.2
169.3
148.2
137.6
135.9
-99
sensor2.open(filename.c_str());
if(sensor2.fail())
{
cout << "Error";
}
else
{
sensor2 >> time >> motion;
do
{
...
sensor2 >> time >> motion;
} while (time >= 0);
206_C4
9
End-of-File
 sensor3.dat
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
132.5
147.2
148.3
157.3
163.2
158.2
169.3
148.2
137.6
135.9
sensor3.open(filename.c_str());
if(sensor3.fail())
{
cout << "Error";
}
else
{
sensor3 >> time >> motion;
while ( !sensor3.eof() )
{
...
sensor3 >> time >> motion;
}
206_C4
10
Generating a Data File
...
#include <fstream>
#include <string>
...
int main()
{
...
string filename;
ofstream balloon;
...
// Prompt user for name of output file.
cout << "enter the name of the output file";
cin >> filename;
206_C4
11
Generating a Data File
// Open output file
balloon.open(filename.c_str());
// Set format flags for file output.
balloon << fixed << setprecision(2);
...
// Write data to file.
balloon << setw(6) << time << setw(10) << height
<< setw(10) << velocity/3600 << endl;
...
// Close file and exit program.
balloon.close();
return 0;
}
206_C4
12
Error Checking
int ivar1, ivar2;
cin >> ivar1 >> ivar2;
while(!cin.eof))
{
if(!cin)
{
cerr << "Error reading from standard input\n";
exit(1);
}
else
{
cout << ivar1 << ' ' << ivar2 << endl;
cin >> ivar1 >> ivar2;
}
206_C4
13
}
Linear Regression
 Determine the linear equation that is the best
fit to a set of data points

Least-squares


Minimize sum of squared distances between line and
data points
y = mx + b
x  y  n xy

m
 x   n x
2
2
206_C4
x  xy   x  y

b
 x   n x
2
2
2
14
Problem Solving Applied
 Ozone Measurements

Problem Statement


Use least-squares technique to determine a linear
model for estimating ozone mixing ratio at a specified
altitude
Input/Output Description
Range of Altitudes
Linear Model
zone1.dat
206_C4
15
Problem Solving Applied

Hand Example
Alt(km) Ratio(ppmv)
 zone1.dat

20
24
26
28
3
4
5
6





n=4
Σx = 98
Σy = 18
Σxy = 454
Σx2 = 2436
 m = 0.37
 b = -4.6
206_C4
16
Problem Solving Applied

Algorithm Development
Read data file values
 Compute sums and ranges
 Compute slope and y-intercept

206_C4
17
Problem Solving Applied

Algorithm Refinement
Set count to zero
 Set sumx, sumy, sumxy, sumx2 to zero
 while not at end-of-file
 read x, y
 increment count
 if count = 1, then set first to x
 add x to sumx, y to sumy, xy to sumxy, x2 to sumx2
 set last to x
 compute m and b
 print first, last, m, b

206_C4
18
/*----------------------------------------------------*/
/* Program chapter4_6
*/
/*
*/
/* This program computes a linear model for a set
*/
/* of altitude and ozone mixing ratio values.
*/
#include<iostream>
#include<iomanip>
#include <fstream>
#include <string>
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("zone1.dat");
ifstream zone1;
// Open input file.
zone1.open(filename.c_str());
if(zone1.fail())
cerr << "Error opening input file\n";
else
{
// 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 << fixed << setprecision(2);
// Print summary information.
cout << "Range of altitudes in km: \n";
cout << first << " to " << last << endl << endl;
cout << "Linear model: \n";
cout << "ozone-mix-ratio = " << m << " * altitude + "
<< b << endl;
// Close file and exit program
zone1.close();
} // end else
system("PAUSE");
return 0;
}
Problem Solving Applied

Testing
206_C4
22
Summary




Defining File Streams
Reading Data Files
Generating a Data File
Numerical Technique

Linear Regression
 Problem Solving Applied
 End of Chapter Summary



C++ Statements
Style Notes
Debugging Notes
206_C4
23
Test #1 Review




Computer Hardware and Software
Engineering Problem-Solving Methodology
Program Structure
Simple C++




Constants and Variables
C++ Operators
Standard Input and Output
Basic Functions
206_C4
24
Test #1 Review
 Structured Programming




Algorithm Development
Conditional Expressions
Selection Statements
Loops
 Data Files



Defining File Streams
Reading Data Files
Generating Data Files
206_C4
25