Top Banner
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
25

Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Jan 19, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

Data Files

ELEC 206

Computer Applications for Electrical Engineers

Dr. Ron Hayne

Page 2: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 2

Data Files

Defining File Streams Reading Data Files Generating a Data File Numerical Technique

Linear Regression

Problem Solving Applied

Page 3: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 3

File Stream Objects

File Stream Classes Derived from Stream Classes Input: ifstream Output: ofstream Header: fstream

Additional Member Functions open() fail() close()

Page 4: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 4

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;

Page 5: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 5

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);

Page 6: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 6

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();

Page 7: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 7

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

Page 8: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 8

Specified Number of Records

sensor1.dat10

0.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

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;

...

}

Page 9: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 9

Trailer or Sentinel Signals

sensor2.dat0.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

sensor2.open(filename.c_str());

if(sensor2.fail())

{

cout << "Error";

}

else

{

sensor2 >> time >> motion;

do

{

...

sensor2 >> time >> motion;

} while (time >= 0);

Page 10: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 10

End-of-File

sensor3.dat0.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

sensor3.open(filename.c_str());

if(sensor3.fail())

{

cout << "Error";

}

else

{

sensor3 >> time >> motion;

while ( !sensor3.eof() )

{

...

sensor3 >> time >> motion;

}

Page 11: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 11

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;

Page 12: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 12

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;

}

Page 13: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 13

Error Checkingint 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;

}

}

Page 14: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 14

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

22xnx

xynyxm

22

2

xnx

yxxyxb

Page 15: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 15

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

Linear Model

Range of Altitudes

zone1.dat

Page 16: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 16

Problem Solving Applied

Hand ExampleAlt(km) Ratio(ppmv)zone1.dat

20 3

24 4

26 5

28 6

n = 4 Σx = 98 Σy = 18 Σxy = 454 Σx2 = 2436

m = 0.37 b = -4.6

Page 17: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 17

Problem Solving Applied

Algorithm DevelopmentRead data file valuesCompute sums and rangesCompute slope and y-intercept

Page 18: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 18

Problem Solving Applied

Algorithm RefinementSet count to zeroSet sumx, sumy, sumxy, sumx2 to zerowhile 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 xcompute m and bprint first, last, m, b

Page 19: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

/*----------------------------------------------------*//* 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;

Page 20: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

// 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;

Page 21: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

// 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;}

Page 22: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 22

Problem Solving Applied

Testing

Page 23: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 23

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

Page 24: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 24

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

Page 25: Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.

206_C4 25

Test #1 Review

Structured Programming Algorithm Development Conditional Expressions Selection Statements Loops

Data Files Defining File Streams Reading Data Files Generating Data Files