260 likes | 280 Views
Arrays and Strings. Chapter 7. Array Introduction. In everyday life we commonly group similar objects into units. The most basic element in C++ that does this is the array. Arrays can hold a few data items or tens of thousands to millions and billions.
E N D
Arrays and Strings Chapter 7
Array Introduction • In everyday life we commonly group similar objects into units. • The most basic element in C++ that does this is the array. • Arrays can hold a few data items or tens of thousands to millions and billions. • The data items grouped in an array can be simple types such as int or char, or they can be user defined such as structures or objects.
Array Introduction • Array’s are like structures in that they both group a number of items into a larger unit. • But a structure usually groups items of different types, and an array groups items of the same type. • More importantly, the items in a structure are accessed by name, while those in an array are accessed by an index number.
Array Introduction • Using an index number to specify an item allows easy access to a large number of items. • Arrays exist in almost every computer language. • Later on in this chapter we will look at array’s of characters, in other words strings.
Array Fundamentals • First we will look at a simple example to introduce array’s. • This program “replay.cpp”, creates an array of four integers representing the ages of four people. It then asks the user to enter four values, which it places in the array. • Lets look at “replay.cpp”.
Defining Arrays • Like other variables in C++, an array must be defined before it can be used to store information. • And like other definitions, an array definition specifies a variable type and name. • But now it includes another feature: size.
Defining Arrays • The size specifies how many data items the array will contain. • It immediately follows the name, and is surrounded by square brackets. • In the last example, the array is type int. The name of the array comes next, followed by an opening bracket, the array size, and a closing bracket.
Defining Arrays • The number in brackets must be a constant or an expression that evaluates to a constant. • Also the number in brackets must be an integer. • In our last example the number was 4.
Array Elements • The items in an array are called elements. • All the elements in an array are of the same type; only the values vary. • In computer memory, each element of an array are stored sequentially. • Storing values in this way speeds up the computer because it can read from one memory location to the next very quickly.
Defining Arrays • Notice that the first array element is numbered 0. • So if there are 4 elements in an array, the last element is numbered 3. • This can be confusing and is something to remember about array’s. • Computers count starting with 0. Humans usually count starting with 1.
Accessing Array Elements • In our last example, we access each array element twice. • The first time was to insert a value into the array. “cin >> age[j];” • Next we read it out with the line. “cout << “\nYou entered “ << age[j];” • In both cases the expression for the array element is age[j];
Accessing Array Elements • An array then consists of the name of the array, followed by brackets delimiting a variable j. • Which of the four array elements is specified by this expression depends on the value of j. • So age[0] refers to the first element, age[1] to the second, age[2] to the third and age[3] to the forth.
Accessing Array Elements • The variable in the brackets is called the array index. • Since j is the loop variable in both for loops, it starts at 0 and is incremented until it reaches 3, that goes through each of the array elements in turn.
Averaging Array Elements • Our second example of arrays is sales.cpp. The user enters a series of six values representing sales for each day of the week and then calculates the average of these values. • We use array type double so that monetary values can be entered. • Let’s look at “sales.cpp”.
Initializing Arrays • You can give values to each array element when the array is first defined. • Next we will look at an example, DAYS, that sets 12 array elements in the array days_per_month to the number of days in each month. • Let’s look at “days.cpp”.
Multidimensional Arrays • So far we have looked at arrays of one dimension: a single variable specifies each array element. • But arrays can have higher dimensions. • Next we will look at a program that uses a two-dimensional array to store sales figures for several districts and several months. • Let’s look at “salemon.cpp”.
Defining Multidimensional Arrays • The array is defined with two size specifiers, each enclosed in brackets: “double sales[DISTRICTS][MONTHS];” • You can think of a two dimensional array as a checkerboard. Another way to think of it is an array of arrays. • It is an array of DISTRICTS elements, each of which is an array of MONTHS.
Defining Multidimensional Arrays • There can be arrays of even more than 2 dimensions. • A three-dimensional array is an array of arrays of arrays. • It is accessed with three indexes: elem = dimen3[x][y][z];
Accessing Multidimensional Array Elements • Array elements in two-dimensional arrays require two indexes: • sales[d][m]; • Notice that each index has its own set of brackets. Commas are not used. Don’t write sales[d,m].
Initializing Multidimensional Arrays • You can also initialize multidimensional arrays. • Lets see how this works by looking at a variation of the SALEMON program that uses an initialized array instead of asking for input from the user. • Let’s look at “saleinit.cpp”.
Passing Arrays to Functions • Arrays can be used as arguments to functions. • Let’s look at an example of passing an array, “salefunc.cpp”.
Function Call with Array Arguments • When the function is called, only the name of the array is used as an argument. • display(sales); // function call • This name actually represents the memory address of the array. • This deals with pointers, which we will learn in chapter 10.
Function Call with Array Arguments • Using an address for an array argument is similar to using a reference argument. • The values of the array elements are are not copied into the function. • Instead, the function works on the original array, although it refers to it by a different name.
Function Call with Array Arguments • This system is used for arrays because they can be very large, duplicating an entire array in every function that called it would be very time-consuming and wasteful of memory. • Remember that an address is not the same as a reference.
Function Definition with Array Arguments • In the function definition the declarator looks like this: • void display(double funsales[DISTRICTS][MONTHS]) • The array argument uses the data type, a name, and the sizes of the array dimensions.
Array’s of structures and objects • Next time we will look at array’s of structures and objects • We will also see array of chars, better known as “strings”. • And we will look at the built in string class.