Download presentation
Presentation is loading. Please wait.
Published byJames Barry Higgins Modified over 9 years ago
1
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
2
206_C42 Data Files Defining File Streams Reading Data Files Generating a Data File Numerical Technique Linear Regression Problem Solving Applied
3
206_C43 File Stream Objects File Stream Classes Derived from Stream Classes Input:ifstream Output:ofstream Header:fstream Additional Member Functions open() fail() close()
4
206_C44 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;
5
206_C45 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);
6
206_C46 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();
7
206_C47 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
8
206_C48 Specified Number of Records sensor1.dat 10 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;... }
9
206_C49 Trailer or Sentinel Signals sensor2.dat 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 -99 sensor2.open(filename.c_str()); if(sensor2.fail()) { cout << "Error"; } else { sensor2 >> time >> motion; do {... sensor2 >> time >> motion; } while (time >= 0);
10
206_C410 End-of-File sensor3.dat 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 sensor3.open(filename.c_str()); if(sensor3.fail()) { cout << "Error"; } else { sensor3 >> time >> motion; while ( !sensor3.eof() ) {... sensor3 >> time >> motion; }
11
206_C411 Generating a Data File... #include... int main() {... string filename; ofstream balloon;... // Prompt user for name of output file. cout << "enter the name of the output file"; cin >> filename;
12
206_C412 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; }
13
206_C413 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; }
14
206_C414 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
15
206_C415 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
16
206_C416 Problem Solving Applied Hand Example Alt(km) Ratio(ppmv) zone1.dat 20 3 24 4 26 5 28 6 n = 4 Σx = 98 Σy = 18 Σxy = 454 Σx 2 = 2436 m = 0.37 b = -4.6
17
206_C417 Problem Solving Applied Algorithm Development Read data file values Compute sums and ranges Compute slope and y-intercept
18
206_C418 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, x 2 to sumx2 set last to x compute m and b print first, last, m, b
19
/*----------------------------------------------------*/ /* Program chapter4_6 */ /* */ /* This program computes a linear model for a set */ /* of altitude and ozone mixing ratio values. */ #include 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;
20
// 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;
21
// 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; }
22
206_C422 Problem Solving Applied Testing
23
206_C423 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
24
206_C424 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
25
206_C425 Test #1 Review Structured Programming Algorithm Development Conditional Expressions Selection Statements Loops Data Files Defining File Streams Reading Data Files Generating Data Files
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.