# C++ Arrays and Algorithms

In this sample chapter, Steve Donovan discusses how to declare, initialize, and use arrays; how to search for particular values and how to sort arrays; and how to use the standard containers, including vectors, lists, and maps.
This sample chapter is excerpted from C++ by Example: "UnderC" Learning Edition, by Steve Donovan.
This chapter is from the book

### This chapter is from the book 

So far we have discussed the core arithmetic and logical operations of C++, as well as features such as strings and input/output that are part of the standard library. In the case study in Chapter 2, "Functions and Control Statements," we hit some limitations of basic C++. To pass a number of integers as a parameter, we were forced to write them as a string. There are two problems with this; first, there is no guarantee that these are all valid integers, and second, this would be very inefficient for really large lists of integers. That case study also depends heavily on having everything like ID lists in the correct order, but we cannot control the world, and often things happen in an order other than what we plan, so we cannot insist on sorted lists. C++ provides a basic built-in way to set up tables of numbers or objects, called arrays. The standard library also provides more sophisticated ways to group items together, called the standard containers.

Algorithms are standard procedures (or recipes) for doing operations such as searching and sorting. I will compare two ways to search tables of values (binary and linear search), and I will introduce the very powerful library of general algorithms in the standard C++ library.

In this chapter you will learn

• How to declare, initialize, and use arrays

• How to search for particular values and how to sort arrays

• How to use the standard containers, including vectors, lists, and maps

## Arrays

Arrays contain a linear table of values, which must all be of the same type, called the base type. These values are called the array's elements, and any element can be specified by its position in the array, called its index. In this section we will discuss C++'s built-in arrays, together with typical array operations like copying and searching. The next section will then look at the standard containers, which are `intelligent' arrays.

### Arrays as Tables of Values

A C++ array declaration reserves space for a number of elements. For instance, the following declarations create an array of ints and an array of chars:

```;> int arr1 [10];
;> char arr2 [10];
;> sizeof(arr1);
(int) 40
;> sizeof(arr2);
(int) 10```

You can see in this example that in general, the space occupied by an array is the number of elements multiplied by the size of each element. The number of elements in an array is usually called its dimension, and it can be any positive constant. You can declare more than one array at a time; in the following code, notice that the dimension follows each new array name. The dimension of a3 is the expression M*N, which is acceptable because it contains no variables. You can mix regular variables in as well, although this is not considered a stylish thing to do. The variable x as declared here is just a plain double.

```;> const int N = 20, M = 30;
;> double a1[6],a2[N],a3[M*N],x;```

You declare two-dimensional arrays (which are used for representing tables or mathematical matrixes) with two dimensions, rather than with a comma-separated pair, as in the following example:

`;> int table[10][10];  // NOT int table[10,10]!!`

### Initializing Arrays

You can initialize arrays by using a list of values separated by commas enclosed in braces, as in the following declaration of arr. The declaration of names shows how you can optionally leave out the constant in the brackets and let the system work out the dimension from the size of the list. For global declarations, the values in the list must be constants.

```;> int arr[4] = {10,5,6,1};
;> string names[] = {"Peter","Alice","James"};
;> double interest_rates[]
= {
5.1,
5.6,
6.0,
7.0
};
;> int numbers[] = {1,2,3,4,5,6,7,8};```

You cannot assign arrays in the following fashion because C++ arrays are not variables. You also cannot use a1 = a2, where both a1 and a2 are arrays.

```;> arr = {1,2,3,5};
CON 10:parse error
CON 10:error in expression```

You can, however, treat each element of the array as if it is a variable; this is called indexing the array, and the index is often also called a subscript. To access the i-th element of arr you say arr[i], where the index i is put in square brackets ([]) like in other languages such as Pascal. This expression can be used wherever a variable can be used. The array index goes from 0 to one less than the size of the array. Using the definitions of arr and names from the preceding examples, you can say the following:

```;> arr[0];
(int&) 10
;> arr[3];
(int&) 1
;> arr[0] + 2*arr[3];
(int) 12
;> arr[2] = 2;
(int&) 2
;> names[2] + " Brown";
(string) `James Brown'```

You can make constant arrays that cannot be written to afterward:

```;> const int AC[] = {10,2,4};
;> AC[2] = 2;
CON 71:Can't assign to a const type```

There are two basic rules to keep in mind with C++ arrays: Arrays go from 0 to n-1 (not 1 to n), and there is no bounds checking whatsoever with arrays. Therefore, if you write past the end of an array, the value will probably go somewhere else. In the following case, we have changed the value of var, (which was an innocent bystander) by forgetting both rules:

```;> int a1[2];
;> int var;
;> a1[2] = 5;
(int&) 5
;> var;
(int) var = 5```

NOTE

The fact that C++ arrays always begin at 0 is a benefit. In some other languages, you can change the start value by a global setting.

NOTE

Running over the end of an array is particularly bad if the array is declared local to a function, and on most systems, this will crash the program badly. The first C program I ever wrote did this, and my DOS machine went down in flames. Up to then I had trained on Pascal, which slaps you on the wrist if you exceed array bounds, but it doesn't amputate the whole hand. You may regard this as dumb and dangerous behavior, and many would agree with you, but C was designed to be fast and somewhat reckless.

You initialize most arrays by using a for loop. for loops naturally go from 0 to n-1, as required. This code is equivalent to the initialization of the array numbers from an earlier example in this chapter:

`;> for(int i = 0; i < 8; i++) numbers[i] = i+1;`

Arrays make possible code that otherwise would require big ugly switch statements. Consider the problem of starting with a date in International Standards Organization (ISO) form, and working out the day number (counting from January 1). One solution begins like this and would require quite a bit more tedious coding:

```   int days = days(date);
switch(month(date)) { // runs from 1 to 12
case 2: days += 31;  break;
case 3: days += 61;  break;
....
}```

Another solution would be to initialize an array with the lengths of the months and then generate a running sum. Thereafter, you could use just one line. Here's how you would implement this solution:

```;> int days_per_month[] = {0,31,28,31,30,31,31,30,31,30,31};
;> int days_upto_month[13];
;> int sum = 0;
;  int days_upto_month[0] = 0;
;> for(int i = 1; i <= 12; i++)
days_upto_month[i] = days_upto_month[i-1] + days_per_month[i];
;> int days = days_upto_month[month(day)] + day;```

### Passing Arrays to Functions

You can pass an array to a function, by declaring an array argument with an empty dimension. You usually have to pass the size as another argument because there is no size information in C/C++ arrays. The advantage of this is that the following function can be passed any array of ints:

```void show_arr(int arr[], int n)
{
for(int i = 0; i < n; i++) cout << arr[i] << ` `;
cout << endl;
}
;> show_arr(numbers,8);
1 2 3 4 5 6 7 8```

However, you cannot pass this function an array of doubles. The following error message means that the compiler failed to match the actual argument (double[]) with the formal argument (which was int[]):

```;> double dd[] = {1.2, 3.4, 1.2};
;> show_arr(dd,3);
CON 28:Could not match void show_arr(double[],const int);```

A double value is organized completely differently from an int type. If the compiler allowed you to pass an array of doubles, you would get very odd integers displayed. On the other hand, simply passing a double value is not much trouble in C++; the compiler sensibly converts the double to an integer and gives you a mild warning that this will involve a loss of precision. This difference between passing a double value and passing an array of doubles exists because in C++, ordinary variables (that is, scalars) are passed by value; they are actually copied into the formal argument. Arrays, on the other hand, are passed by reference. This difference is similar to the difference between mailing someone his or her own copy of a document and telling the person where to find it on a network. An interesting result of being able to pass arrays by reference is that you can change an array's elements within a function. (If you altered the document on the network, this would change that document for all readers.) The following small function modifies the first element of its array argument:

```;> void modify_array(int arr[])  { arr[0] = 777; }
;> modify_array(numbers);
;> show_arr(numbers,8);
777 2 3 4 5 6 7 8```

What if you didn't want the array to be (accidently) modified? This was always an issue with C, but C++ has a number of solutions. One solution is to use the standard library's vector class, which we will discuss later in this chapter. Another good solution is to make the array parameter const, as in the following example, so that it is impossible to modify the parameter within the function:

```void just_looking(const int arr[]) {
cout << arr[0] << endl;
}```

A const parameter is a promise from the function to the rest of the program: "Pass me data, and I shall not modify it in any form." It's as if I tell you where to find the document on the server, but make it read-only.

Data arrays are commonly read from files. Because arrays are passed by reference, it is easy to write a function to do this job. The following example uses a few new shortcuts:

```int read_arr(string file, int arr[], int max_n)
{
ifstream in(file.c_str());
int i = 0;
while (in >> arr[i]) {
++i;
if (i == max_n) return i;
}
return i;
}```

In this example, if you initialize the ifstream object in with a filename, that file will be automatically opened. The file is automatically closed when in goes out of scope. Also, the bad() method tells you if the file cannot be opened. There is no simple way to work out how many numbers are in any given file, so there's always the danger that you might overrun an array. In functions that modify arrays, it is common for the actual allocated array size to be passed. This function reads up to max_n numbers and will exit the file read loop if there are more numbers; it will return the number of values read, which is one plus the last index.

Writing out arrays to a file is straightforward. You can use the remainder operator (%) to control the number of values written out per line, as in the following example. (i+1) % 6 is zero for i = 5,11,17,...—that is, the condition is true for every sixth value. (I added 1 to i so that it would not put out a new line for i == 0.)

```void write_arr(string file, int arr[], int n)
{
ofstream out(file.c_str());
for(int i = 0; i < n; i++) {
out << arr[i];
if ((i+1) % 6 == 0) out << endl;
else out << ` `;
}
out << endl;
}```

### Searching

You will find that you often need to search for a value in a table of numbers. You want to know the index of that value within that table, or even simply whether the value is present. The simplest method is to run along until you find the value, or key. If you don't find the key, you just return -1 to indicate that the key is not present in the array, as in the following example, where the function linear_search() is defined:

```int linear_search(int arr[], int n, int val)
{
for(int i = 0; i < n; i++)
if (arr[i] == val) return i;
return -1;
}
;> linear_search(numbers,4);
(int) 3
;> linear_search(numbers,42);
(int) -1```

This method is fast enough for small tables, but it isn't adequate for large tables that need to be processed quickly. A much better method is to use a binary search, but the elements of that table must be in ascending order. To perform a binary search, you first divide the table in two; the key must be either in the first half or the second half. You compare the key to the value in the middle; if the key is less than the middle value, you choose the first half, and if the key is greater than the middle value, you choose the second half. Then you repeat the process, dividing the chosen half into halves and comparing the key, until either the key is equal to the value or until you cannot divide the table any further. Here is a C++ example of performing a binary search:

```int bin_search(int arr[], int n, int val)
{
int low = 0, high = n-1;    // initially pick the whole range
while (low <= high) {
int mid = (low+high)/2;  // average value...
if (val == arr[mid]) return mid;   // found the key
if (val < arr[mid]) high = mid-1;  // pick the first half
else   low  = mid+1;  // pick the second half
}
return -1;  // did not find the key
}```

How much faster is a binary search than a linear search? Well, if there are 1,000 entries in a table, a binary search will take only about 30 tries (on average) to find the key. The catch here is that the table must be sorted. If it's already sorted, then it seems a good idea to insert any new value in its proper place. Finding the place is easy: You run along and stop when the key is less than the value. The code for finding the position is similar to the code for a linear search, but -1 now means that the key was larger than all the existing values and can simply be appended to the table:

```int linear_pos(int arr[], int n, int val)
{
for(int i = 0; i < n; i++)
if (arr[i] > val) return i;
return -1;
}```

### Inserting

It takes a surprising amount of effort to insert a new value into an array. To make space for the new value means moving the rest of the array along. For example, consider that inserting 4 into the sequence 1 3 9 11 15; linear_pos() gives an index of 2 (because 9 is greater than 4). Everything above that position must be shifted up to make room; the top line shows the array subscripts:

0  1  2  3  4  5 subscripts

1  3| 9  11 15 before shifting up by one

1  3 xxx 9  11 15 after shifting up by one

Here I've shown the sequence before and after the shift. We have to put 4 before index 2 (that is, the third position). So everything from the third position up (9 11 15) has got to move up one. That is, the shift involves A[5] = A[4], A[4] = A[3], and A[3] = A[2]. We can then set A[2] = 4. In general:

```void insert_at(int arr[], int n, int idx, int val)
{
if (idx==-1) arr[n] = val;  // append
else {
for(int i = n; i > idx; i—)
arr[i] = arr[i-1];
arr[idx] = val;
}
}
;> int n = 5;
;> int pos = linear_pos(arr,n,4);
;> insert_at(arr,n,pos,4);
;> show_arr(arr,n);
1 3 4 9 11 15```

Once insert_at() is defined, then it is straightforward to put a new element into an array so that it remains in order. The special case when idx is -1 just involves putting the value at the end. But generally there will be a lot of shuffling, making insertion much slower than array access. Note that this routine breaks the first rule for dealing with arrays: It goes beyond the end of the array and writes to arr[n]. For this to work, the array needs to have a dimension of at least n+1.

This is a good example of a for loop doing something different from what you've seen for loops do before: in this case, the for loop is going down from n to idx+1. In tricky cases like this, I encourage you to experiment (that's what UnderC is for.) But nothing beats sitting down with a piece of paper and a pencil.

As you can see from the examples in this section, it isn't straightforward to insert a value into an array in order. The algorithm for deleting an item is similar to insert_at() and involves shifting to the left; I leave this as an exercise for a rainy afternoon.

### Sorting

Sorting a sequence that is not in any order also involves moving a lot of data around. The following algorithm is called a bubble sort because the largest numbers "bubble" up to the top. It includes calls to show_arr() so that you can see how the larger numbers move up and the smaller numbers move down:

```void bsort(int arr[], int n)
{
int i,j;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
if (arr[i] > arr[j]) { // swap arr[i] and arr[j]
show_arr(arr,n);  // print out array before swap
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
show_arr(arr,n); // show sorted array
}
;> int b[] = {55,10,2,3,6};
;> bsort(b,5);
55 10 2 3 6
10 55 2 3 6
2 55 10 3 6
2 10 55 3 6
2 3 55 10 6
2 3 10 55 6
2 3 6 55 10
2 3 6 10 55```

### InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

## Overview

Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

## Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

### Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

### Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

### Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

### Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

### Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

### Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

## Other Collection and Use of Information

### Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

### Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

### Do Not Track

This site currently does not respond to Do Not Track signals.

## Security

Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

## Children

This site is not directed to children under the age of 13.

## Marketing

Pearson may send or direct marketing communications to users, provided that

• Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
• Such marketing is consistent with applicable law and Pearson's legal obligations.
• Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
• Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

## Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

## Choice/Opt-out

Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

## Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

## Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

## Sharing and Disclosure

Pearson may disclose personal information, as follows:

• As required by law.
• With the consent of the individual (or their parent, if the individual is a minor)
• In response to a subpoena, court order or legal process, to the extent permitted or required by law
• To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
• In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
• To investigate or address actual or suspected fraud or other illegal activities
• To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
• To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
• To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.