Unit 6 - Arrays - Elementary Statistics return to unit course outline

Example for One-dimensional Arrays:

Read the explanations provided for the example and look at the second example provided

Place your mouse in the areas below to see comments about the corresponding sections of code  
Code
 
/* An example on the use of arrays :
                                  file: 6ex1.cpp
   FALL 1998
   ___________________________________
   Jacob Y. Kazakia   jyk0
   October 13, 1998
   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  outputted numbers.
                */
 
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
 
void main()
{
 
// declare the variables of the main function
 
float a[20]; //This is an array of twenty entries
            // a[0], a[1], a[2], ......., a[18], a[19].
 
float b[20]; //This is an array of twenty entries
            // 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);
 bursar << setprecision(4);
 bursar << "   a( " << setw(2) << m <<" ) = " ;
 bursar << setw(15) << a[m];
 bursar << "    b( " << setw(2) << m <<" ) = ";
 bursar << setw(15) << b[m];
 bursar << endl;
}
 
bursar <<"\n\n Now outputting every other  number \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: ";
bursar <<  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 outputting 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 outputted values is: 1.1161e+02
 
     */
This was the data file:


         4.5  8.9
         -2.3  8.9
         1.234567e-5    45
         12.34567   -123.567
         45.78      12e34
         0   0
         1 2
         4.5  8.9
         -2.3  8.9
         1.234567e-5    46
         12.34567   -123.567
         45.78      12e34
         0   30
         1 2.3
         4.5  8.9
         -2.3  8.9
         1.234567e-5    45
         12.34567   -123.567
         45.78      12e34
         0   50

(text file of the code)

(printable file)

(another example)

© 2001 J.Y. Kazakia. All rights reserved