## Chapter Goals

• To learn how to use one-dimensional and two-dimensional arrays
• To become familiar with C-strings

## Arrays

• Vectors are a convinient mechanism for collecting elements of the same type.
• A second mechanism for collecting elements of the same type is using arrays.
• Arrays are a lower-level abstraction than vectors, so they are less convenient.
• Arrays cannot be resized.
• The array size must be set when the program is compiled.
• Vectors are a recent addition to C++, so many older programs use arrays instead.
• Arrays are faster and more efficient than vectors - this can be important in some applications.
• Declaring an array is very similar to declaring a vector.
`double salaries[10]; /* array *//* compare to.... */vector<double> salaries(10); /* vector */`
• Arrays can never change size.
• The array size must be set when the program is compiled.
• You can't ask the user how many elements and then allocate a sufficient number because the size must be a constant!
• When defining an array, you must guess on the maximum number of elements you need to store.
`const int SALARIES_CAPACITY = 100;double salaries[SALARIES_CAPACITY];`
• You must keep a constant to hold the capacity of the array.
• You must keep a companion variable that counts how many elements are actually used. The following loop collects data and fills up the salaries array.
`int salaries_size = 0;while (more && salaries_size < SALARIES_CAPACITY){  cout << "Enter salary or 0 to quit: ";   double x;   cin >> x;   if (cin.fail() || x == 0) more = false;   else   {  salaries[salaries_size] = x;      salaries_size++;   }}`
• At the end of this loop, salaries_size contains the actual number of elements in the array.

Syntax 9.3: Array Variable Definition

`type_name variable_name[size];`
 Example: `int scores[20];` Purpose: Define a new variable of an array type.

## Array Parameters

• When writing a function with an array parameter, you place an empty[] behind the parameter name:
`double maximum(double a[], int a_size);`
• You need to pass the size of the array into the function, because the function has no other way of querying the size of the array (there is no size() member function).
• Unlike all other parameters, array parameters are always passed by reference.
`void raise_by_percent(double s[], int s_size, double p){  int i;   for (i = 0; i < s_size; i++) s[i] = s[i]*(1 + p/100);}`
• Never use an & when defining an array parameter.
• Use the const keyword whenever a function does not actually modify an array.
`double maximum(const double a[], int a_size);`
• If a function adds elements to an array, you need to pass the array, the maximum size, and the current size.
• The current size must be passed as a reference parameter.
`void read_data(double a[], int A_CAPACITY, int& a_size){  a_size = 0;   while (a_size < A_CAPACITY)   {  double x;      cin >> x;      if (cin.fail()) return;      a[a_size] = x;      a_size++;   }}`
• Arrays cannot be function return types.
• To "return" an array, the caller of the function must provide an array parameter to hold the result.

## Character Arrays

• There was a time when C++ had no string class.
• All string processing was carried out by manipulating arrays of the type char.
• The char type denotes an individual character and is delimited by single quotes (ASCII table).
`char input = 'y'; /* don't confuse with "y" */`
• A character array is used to hold a string.
`char greeting[] = "Hello";   /* same as */char greeting[6] = "Hello"; `
• The array occupies six characters - one for each letter and a zero terminator.
 greeting[0] greeting[1] greeting[2] greeting[3] greeting[4] greeting[5] 'H' 'e' 'l' 'l' 'o' '\0'
• You do not need to specify the size of the array variable for a character array constant.
• Many string functions in the standard library depend on zero terminators in character arrays.
`int strlen(const char s[]){  int i = 0;   while (s[i] != '\0') i++;   return i;}`
• If the zero terminator is not present, the function will keep on looking past the end of the array until it happens to encounter a zero byte.
• It's important to not forget the space for the zero terminator.
• It's helpful to declare character arrays with an "extra space" for the zero terminator.
`const int MYSTRING_MAXLENGTH = 4;char mystring[MYSTRING_MAXLENGTH + 1];`

## Character Arrays (append.cpp)

• Generally it is best to avoid the use of character arrays - the string class is safer and far more convenient.
• Occasionally you need to convert a string into a character array to call a function that was written before the string class was invented.
• Example: to convert a character array containing digits into its integer value.
`int atoi(const char s[]);`
• Use the c_str() member function to convert a string into a character array.
`string year = "1999";int y = atoi(year.c_str());`

## Two-Dimensional Arrays

• It often happens that we want to store collections of numbers that have a two-dimensional layout.
• Such an arrangement, consisting of row and columns of values, is called a two-dimensional array, or a matrix.
• C++ uses an array with two subscripts to store a two-dimensional array:
`const int BALANCE_ROWS = 11;const int BALANCE_COLS = 6;double balances[BALANCE_ROWS][BALANCE_COLS];`
• To select a particular element in the two-dimensional array, we need to specify two subscripts in separate brackets to select the row and column.

Syntax 9.4 : Two-Dimensional Array Definition

`type_name variable_name[size1][size2];`
 Example: `double monthly_sales[NREGIONS][12];` Purpose: Define a new variable that is a two-dimensional array.
• When passing a two-dimensional array to a function, you must specify the number of columns as a constant with the parameter type.
`void print_table(const double table[][BALANCE_COLS], int table_rows){  const int WIDTH = 10;   cout << fixed << setprecision(2);   for (int i = 0; i < table_rows; i++)   {  for (int j = 0; j < BALANCES_COLS; j++)         cout << setw(WIDTH) << table[i][j];      cout << "\n";   }}`
• The two dimensional arrays are stored as a sequence of elements in the memory
• When you access table[i][j], the computer merely computes the offset i*BALANCE_COLS + j and locates this element.
• It does not check whether i is less than BALANCE_ROWS.