Return to Existing Everywhere Main Page

C++ Example on Indexing Arrays

 

The following C++ program loads an array from data stored in a file. The output of processing the array is written to an output file. Once the data is loaded into the array, the contents are displayed. The array is indexed in various configurations. The idea is that there are a variety of ways of working with simple data structures.

The data of the array are integers stored in a file. The number of integers should not greater than the size of the array. Besides indexing and displaying the contents at these indexes, some of the functions include finding the maximum value and the sum of the elements contained in the aray.

#include 
#include 
#include 
#include 

using namespace std;

void load( ifstream&, int []);
void part_1( ofstream&, int [], int);
void part_2( ofstream&, int [], int);
void part_3( ofstream&, int [], int);
void part_4( ofstream&, int [], int);
void part_5( ofstream&, int [], int);
void part_6( ofstream&, int [], int);
void part_7( ofstream&, int [], int);

int main(int argc, char *argv[])
{
    int size = 20;
    int arr[size];
    ifstream infile("p8_input_data.txt");
    ofstream outfile("p8_output_data.txt");
    
//initialization
    load( infile, arr);
//part 1
    part_1( outfile, arr, size);
    
//part 2
    part_2( outfile, arr, size);
    
//part 3
    part_3( outfile, arr, size);
    
//part 4
    part_4( outfile, arr, size);
    
//part 5
    part_5( outfile, arr, size);

//part 6
    part_6( outfile, arr, size);
    
//part 7
    part_7( outfile, arr, size);

    infile.close();
    outfile.close();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void load( ifstream& infile, int arr[] )
{
    int i = 0;
    while( !infile.eof() )
            infile >> arr[i++];
}

void part_1( ofstream& outfile, int arr[], int size)
{
    cout << endl << "Contents of the array:" << endl;
    outfile << endl << "Contents of the array:" << endl;

    for(int i = 0; i < size; i++)
            {
             cout << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
             cout << setw(5) << arr[i] << endl;
             outfile << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
             outfile << setw(5) << arr[i] << endl;
            }
    cout << endl;
    outfile << endl;
}

void part_2( ofstream& outfile, int arr[], int size)
{
    cout << endl << "Contents of the array from 18 to 7:" << endl;
    outfile << endl << "Contents of the array from 18 to 7:" << endl;

    for(int i = size-2; i >= 7; i--)
            {
             cout << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
             cout << setw(5) << arr[i] << endl;
             outfile << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
             outfile << setw(5) << arr[i] << endl;
            }
    cout << endl;
    outfile << endl;
}

void part_3( ofstream& outfile, int arr[], int size)
{
    cout << endl << "Contents of the array for even index values:" << endl;
    outfile << endl << "Contents of the array for even index values:" << endl;
    
    for(int i = size-1; i >= 0; i--)
            {
             if( i % 2 == 0 )
                 {
                  cout << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  cout << setw(5) << arr[i] << endl;
                  outfile << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  outfile << setw(5) << arr[i] << endl;
                 }
            }
    cout << endl;
    outfile << endl;
}

void part_4( ofstream& outfile, int arr[], int size)
{
    cout << endl << "Contents of the array for even valued content:" << endl;
    outfile << endl << "Contents of the array for even valued content:" << endl;

    for(int i = size-1; i >= 0; i--)
            {
             if( arr[i] % 2 == 0 )
                 {
                  cout << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  cout << setw(5) << arr[i] << endl;
                  outfile << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  outfile << setw(5) << arr[i] << endl;
                 }
            }
    cout << endl;
    outfile << endl;
}

void part_5( ofstream& outfile, int arr[], int size)
{
    int max = 0;
    for(int i = size-1; i >= 0; i--)
            {
             if( max < arr[i] )
                 max = arr[i];
            }
    cout << "The max value is: " << max << endl;
    outfile << "The max value is: " << max << endl;
}

void part_6( ofstream& outfile, int arr[], int size)
{
    int sum = 0;
    double average = 0;

    for(int i = size-1; i >= 0; i--)
            {
             sum += arr[i];
            }
    average = sum/(double)size;
    cout << "The sum is: " << sum << "\tThe average is: " << average << endl;
    outfile << "The sum is: " << sum << "\tThe average is: " << average << endl;
}

void part_7( ofstream& outfile, int arr[], int size)
{
    cout << endl << "Contents of the array where the content is three times the index:" << endl;
    outfile << endl << "Contents of the array where the content is three times the index:" << endl;

    for(int i = size-1; i >= 0; i--)
            {
             if( arr[i] == 3*i )
                 {
                  cout << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  cout << setw(5) << arr[i] << endl;
                  outfile << setw(10) << "index: " << setw(5) << i << setw(20) << "element: ";
                  outfile << setw(5) << arr[i] << endl;
                 }
            }
    cout << endl;
    outfile << endl;
}