Running your first C++ program in Borland C++



Arrays

An array is a list of data objects, all of the same data type. These are stored sequentially in memory. They all have the same variable name, and are referred to individually by means of subscripts in brackets,

An array gives us a way to store all the marks for a class and then refer to a specific student's mark.

num

┌──────┐

0 │ 3 │ num[0] holds 3

├──────┤

1 | 15 │ num[1] holds 15

├──────┤

2 | -2 | num[2] holds -2

├──────┤

3 | | num[3] and num[4] do not have values

├──────┤

4 | |

└──────┘

Figure 7-1 Array num

HIGHLIGHTS

• The numbers 0, 1, 2, 3, 4 are called subscripts; individual elements of the array are called subscripted variables.

• The square brackets distinguish array subscripts from other types of parenthesized expressions.

• The subscripts of all arrays in C++ are numbered starting with 0 and increase each time by 1.

Declaring an Array

To use an array, we must declare it. For example, an array called nums that can hold ten integers is declared as follows:

int nums[10];

• The 10 specifies the number of elements in the array (usually referred to as the size or dimension). Array nums has ten elements, numbered from 0 to 9.

• 0 is the lower bound or smallest subscript in every array.

• 9 is the upper bound or largest subscript.

• In the program, a subscript which refers to nums should be in the range from 0 to 9.

• The type in the array declaration tells what data type applies to each element, from the lower to the upper bound; in the array nums, the data type for each element is int.

Keep in mind that the declaration sets aside ten storage locations for the nums array, but the program does not have to use them all.

CAUTION

Even though there are ten elements in the array, the highest subscript is 9. An attempt to use subscript 10 references a storage location outside the array, which is usually the location of another variable. If you assign or read a value into that location, you will destroy the value of the other variable. C++ does not send an error message since it does not check for invalid subscripts.

It is possible to initialize all of an array in the declaration, although that usually makes sense only for small arrays. The following declaration initializes all ten elements in the nums array:

int nums[10] = {5,11,-1,17,30,6,47,48,21,-32};

Arrays of Other Data Types

Although the examples so far have all been arrays of type int, we can have arrays of other data types, such as float, double, or char.

e.g.:

X[3] = 24.6;

X[2] = 4 * A + 13;

In this case, X is a one-dimensional array.

Example:

|4 |-3 |5 |6 |

|Y[0] |Y[1] |Y[2] |Y[3] |

|4.7 |9.2 | |2 |3 |

|Z[0] |Z[1] | |X[0] |X[1] |

|A |1 | |B |2 | |C |3 | |D |4 |

What is the value of each of the following?

Y[0] _______ Y[1] _______ X[0] _______

A _______ Y[B] _______ Z[A] _______

B _______ Y[C] _______ Z[B] _______

Y[A] _______ X[A] _______ Y[A+2] _______

The subscript of a particular element refers to the position of that element in the array. It is a relative address. When an array is stored in memory, it is stored much this way, with the elements in contiguous storage locations.

Starting the subscripts from 0 may not be totally natural to us, but it has the advantage that the subscript of a particular array element is always the same as the number of "steps" needed to jump to that element (from the beginning of the array), i.e., a[2] is 2 step away from a[0]; a[5] is 5 steps away from a[0]; etc.

To declare an array:

int X [100];

Here we initialize the first five elements in the declaration and assign values to the next seven; the other elements remain uninitialized:

char message[20] = {'w','e','i','r','d'};

message[5] = ' '; // assign a blank to message[5]

message[6] = 's';

message[7] = 't';

message[8] = 'u';

message[9] = 'f';

message[10] = 'f';

for (int i = 0; i < 11; i++)

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download