/* An example on the use of arrays : file: 6ex1.cpp FALL 1998 ___________________________________ Jacob Y. Kazakia jyk0 October 13, 1998 Example 1 of week 6 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________ Purpose: This program reads two columns of 20 float numbers from a file named 6ex1data.txt into two arrays a[20] and b[20]. It outputs these numbers to a file named 6ex1rep.txt . It then outputs to the same file every other number of the first column and the sum of these outputed numbers. */ #include #include #include void main() { // declare the variables of the main function float a[20]; // This is an array of twenty entries named as: // a[0], a[1], a[2], ......., a[18], a[19]. float b[20]; // This is an array of twenty entries named as: // b[0], b[1], b[2], ......., b[18], b[19]. int m; ifstream registrar ( "6ex1data.txt" , ios:: in); ofstream bursar ( "6ex1rep.txt" , ios:: out); for ( m = 0 ; m <= 19 ; m++ ) { registrar >> a[m]>> b[m]; bursar << setiosflags( ios :: scientific) << setprecision(4); bursar << " a( " << setw(2) << m <<" ) = "<< setw(15) << a[m]; bursar << " b( " << setw(2) << m <<" ) = "<< setw(15) << b[m]; bursar << endl; } bursar <<"\n\n Now outputing every other of the twenty numbers \n"; bursar <<" on the first column \n\n" ; float sum = 0; for ( m = 0 ; m <= 19 ; m = m + 2 ) { bursar << " a( " << setw(2)<< m <<" ) = "<< setw(15) << a[m]; bursar << endl; sum = sum + a[m]; } bursar << " \n\n The sum of the outputed values is: " << sum << endl << endl ; cout<< " \n\n DONE ! \n\n"; cout<<" \n\n enter e (exit) to terminate the program...."; char hold; cin>>hold; } /* THIS IS THE REPORT FILE: a( 0 ) = 4.5000e+00 b( 0 ) = 8.9000e+00 a( 1 ) = -2.3000e+00 b( 1 ) = 8.9000e+00 a( 2 ) = 1.2346e-05 b( 2 ) = 4.5000e+01 a( 3 ) = 1.2346e+01 b( 3 ) = -1.2357e+02 a( 4 ) = 4.5780e+01 b( 4 ) = 1.2000e+35 a( 5 ) = 0.0000e+00 b( 5 ) = 0.0000e+00 a( 6 ) = 1.0000e+00 b( 6 ) = 2.0000e+00 a( 7 ) = 4.5000e+00 b( 7 ) = 8.9000e+00 a( 8 ) = -2.3000e+00 b( 8 ) = 8.9000e+00 a( 9 ) = 1.2346e-05 b( 9 ) = 4.6000e+01 a( 10 ) = 1.2346e+01 b( 10 ) = -1.2357e+02 a( 11 ) = 4.5780e+01 b( 11 ) = 1.2000e+35 a( 12 ) = 0.0000e+00 b( 12 ) = 3.0000e+01 a( 13 ) = 1.0000e+00 b( 13 ) = 2.3000e+00 a( 14 ) = 4.5000e+00 b( 14 ) = 8.9000e+00 a( 15 ) = -2.3000e+00 b( 15 ) = 8.9000e+00 a( 16 ) = 1.2346e-05 b( 16 ) = 4.5000e+01 a( 17 ) = 1.2346e+01 b( 17 ) = -1.2357e+02 a( 18 ) = 4.5780e+01 b( 18 ) = 1.2000e+35 a( 19 ) = 0.0000e+00 b( 19 ) = 5.0000e+01 Now outputing every other of the twenty numbers on the first column a( 0 ) = 4.5000e+00 a( 2 ) = 1.2346e-05 a( 4 ) = 4.5780e+01 a( 6 ) = 1.0000e+00 a( 8 ) = -2.3000e+00 a( 10 ) = 1.2346e+01 a( 12 ) = 0.0000e+00 a( 14 ) = 4.5000e+00 a( 16 ) = 1.2346e-05 a( 18 ) = 4.5780e+01 The sum of the outputed values is: 1.1161e+02 */