Example for One-dimensional Arrays:

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

You can have a look at the data file 6ex1data.txt and you will see that the numbers are written in various formats.  The file looks ugly.
/* 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()
{
The declaration of arrays a and b.  Both are of size 20.
 
The streams for reading from the file and writing to another are being established.
// 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);
A simple counting loop is used to read the numbers from the file into the arrays and then output them to a different file. Note that by using format statements, the outputted data is made to look nice.
NOTE THAT WE COULD HAVE DONE THIS WITHOUT USING ARRAYS
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;
}
This is the part that has to be done using arrays.  We need to go back to the numbers we read from the file and output them in a different order than the one they were read in. We increase the index by 2 each time and thus we output every other number.
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 ;
When the output of a program is directed to a file, it is a good idea to send a message to the default screen so that the user knows that the task has been done.
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

© 2001 J.Y. Kazakia. All rights reserved