Chapter 31 One-Dimensional Arrays and Maps

31.1 Introduction

Variables are a good way to store values in memory but they have one limitation—they can hold only one value at a time. There are many cases, however, where a program needs to keep a large amount of data in memory, and variables are not the best choice.

For example, consider the following exercise:

Write a C++ program that lets the user enter three numbers. It then displays them sorted in ascending order.

Consider the following code fragment. It lets the user enter the three numbers.

for (i = 0; i <= 2; i++) {
cin >> number;
}

When the loop finally finishes iterating, the variable number contains only that last number that was provided. Unfortunately, all the previous two numbers ...

Get C++ and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.