CIS 150 Arrays

Objectives

  • Explain what an array is.
  • Explain what an index of an array is.
  • Calculate the lowest and highest indices in an array given the length of the array.
  • Declare an array.
  • Initialize an array.
  • Access and modify specific elements of an array.
  • Create a loop to iterate through and process all elements of an array.
  • Pass arrays to functions for processing.
  • Write code to fill an array with user input.
  • Write code to find the minimum value in an array.
  • Write code to find the maximum value in an array.
  • Write code to find the sum of the elements of an array.
  • Write code to display the elements of an array.
  • Describe some limitations of arrays in C++.
  • Declare, initialize, and use a two dimensional array

Array overview

Like functions, arrays are one of the most important topics that we discuss in this class. One of a computer's main jobs is to process large quantities of data. Up until this chapter we created a variable to store each data value used in a program. Now we get to use arrays, a data structure that allows us to store multiple values in memory using a single variable name.

An array is a group of data, all of the same type, with one name. But how can we get a specific piece of data if they are all stored using the same variable name? This is achieved by using an index (subscript) which allows the programmer to access one particular value out of the group of values. The index also makes it easy to process arrays using loops, especially for loops, since the loop counter of the loop can be used as an index into the array.

All of the elements in a C++ array must be the same data type. If the array is multi-dimensional, all of the rows must be the same length, all of the columns must be the same length, etc.

Declaring and initializing an array

  • Declaring an array of 10 ints named num: int num[10];
  • Declaring an array of 5 ints named num and initializing the array: int num[5] = {3, 42, -17, 3, 2};
  • The first element of an array is at index 0.
  • If an array is declared to have n elements, the last element is at index n-1.
  • Array declarations must use a constant integer > 0 for the array size.
  • Arrays can not be resized once they have been declared.
  • You can use an integer literal (which is a constant) for the array size.
  • You can declare an integer constant like this (preferred style): const int SIZE = 10;
  • You can also create a constant the old C-way (try to avoid): #define SIZE 10;
  • The highest index in an array is one less than the length. An array of 15 elements has indices 0 through 14.
  • You have to keep track of the array length in C++.
  • C++ usually allows you to access elements of an array that do not exist, such as index -1 and indices beyond the end.

Accessing elements of an array

  • Array elements can be used like any other variable.
  • Array elements are accessed using the array name followed by the index in [ ].
  • Display the first element of array num: cout << num[0] << endl;
  • Display the last element of array num (assuming it has 15 elements): cout << num[14] << endl;
  • Display the last element of array num (assuming it has SIZE elements): cout << num[SIZE-1] << endl;
  • Double the value in the second element of array x: x[1] = 2 * x[1];

Using loops to process arrays

  • A for loop is most commonly used to process arrays because it is the easiest form to use.
  • The loop usually iterates through all elements of the array: for (i=0; i<SIZE; i++)
  • Sometimes you skip the first element: for (i=1; i<SIZE; i++)
  • Sometimes you skip the last element: for (i=1; i<SIZE-1; i++)

Fill an array with user input

// Assume SIZE holds the length of the array for (i=0; i<SIZE; i++) { // people are used to counting from 1, so adjust up 1 on display cout << "Enter number " << (i+1) << ": "; cin >> ar[i]; }

Display every element of an array

// Assume SIZE holds the length of the array for (i=0; i<SIZE; i++) { cout << ar[i] << ' '; } cout << endl;

Add up all the elements of an array

// Assume SIZE holds the length of the array sum = 0; for (i=0; i<SIZE; i++) { sum = sum + ar[i]; }

Find the minimum value of an array

// Assume SIZE holds the length of the array min = ar[0]; // assume the first element is the smallest for (i=1; i<SIZE; i++) { // then keep replacing it if a smaller element is found if (ar[i] < min) min = ar[i]; }

Find the maximum value of an array

If you know how to find the minimum value, finding the maximum value should be easy.

Passing an array to a function

// Assume nums is an array of ints // Assume SIZE is the length of array nums sort(nums, SIZE);

Specifying an array as a function parameter

void sort(int ar[], int length) { // Where ar is the array and length is its length .... code to sort array ar .... }

Note: Arrays are passed by reference. The name of the array is actually a pointer to its first element. That means that array elements can be modified in functions that the arrays are passed to. It also means that there is usually no reason to return an array from a function.

Two dimesional arrays

// declaration and initialization int ar[2][5] = {{1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}; // processing (doubles every element of the array) for (int row=0; row<2; row++) { for (int col=0; col<5; col++) { ar[row][col] = ar[row][col] * 2; } }