Arrays

A variable can store a single value.  An array is a containers that can store many values.  In many programs it is inconvenient to create a variable name for each value being used.  It is often easier to refer to a whole list of numbers by a single name.  Individual items in the list are referred to by their position in the list using subscripts.  Arrays are frequently used with loops which make it efficient to cycle through the elements in an array.  Arrays in C++ are zero offset.   The declaration float a[5] reserves room for five real numbers a[0], a[1], a[2], a[3] and a[4].

One-Dimensional Arrays

Declaration

Array declarations have the form: data_type array_name[number_of_items];

// Compute the total of several scores
# include<iostream>
using namespace std;

int main( )
{
    const int NMAX = 5;
    int i, scores[NMAX], total;
   
    total = 0;
    for (i = 0; i < NMAX; i++)
    {
        cout << “enter a score \n”;
        cin >> scores[i];
        total += scores[i];
    }
    cout << “the total of the scores = “ << total;
    return 0;
}

Initialization

Note the size of an array may be omitted when initializing values are included in the declaration statement.

int gallons[5] = {12, 43, 56, 45, 78} or
int gallons[ ] = {12, 43, 56, 45, 78}

Arrays as arguments

# include<iostream>
using namespace std;

double findmax(double [ ], int); // function prototype

int main( )
{
    const int NMAX = 5;
    double nums[NMAX] = {1.4, 3.2, 5.3, 1.5, 3.8};
    cout << “the maximum number is “ << findmax(nums, NMAX) << endl;
    return 0;
}

double findmax(double vals[ ], int n)
{
    int i,
    double max;
    max = vals[0];
    for (i = 1; i < n; i++)
    {
        if (vals[i] > max)
            max = vals[i];
    }
    return max;
}

Dynamic memory allocation

# include<iostream>
using namespace std;

int main( )
{
    int i, size;
    cout << “enter size of the array \n”;
    cin >> size;
 
    int * list = new int[size];

    for (i = 0, i < size; i++)
    {
        list[i] = i;
        cout << list[i];
    }
 
    delete [ ] list;
    return 0;
}

Two-dimensional arrays

# include<iostream>
# include<iomanip>
using namespace std;

const int ROWS = 3;
const int COLS = 4;
void display(int [][COLS])

int main( )
{
    int matrix[][COLS] = {8, 16, 9, 52,
                          3, 15, 27, 6,
                         14, 25, 2, 10};
    display(matrix);
    return 0;
}

void display(int nums[][COLS])
{
    int i, j;
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            cout << setw(4) << nums[i][j];
        }
    }
    cout << endl;
    }
    return;
}

Exercises

Homework

Write a program to insert an element into an sorted array.  This is accomplished by shifting the appropriate elements forward to make room for the new element.  Your program should use the array  int scores[20] = {0,10,20,30,40,50,60,70,80,90}; and allow the user to enter an integer to be inserted from the keyboard.  Ideally, the program will use main( ) as a driver and have a print and insert function.