> a[i]; cout << "In reverse order: "; for (int i=SIZE-1; i>=0; i--) cout << "\t" << a[i]; }"> > a[i]; cout << "In reverse order: "; for (int i=SIZE-1; i>=0; i--) cout << "\t" << a[i]; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.

Similar presentations


Presentation on theme: "Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively."— Presentation transcript:

1 Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively 0, 1, 2, 3,.... These numbers are called index values or subscripts of the array. The term “subscript” is used because as a mathematical sequence, an array would be written with subscripts: a0, a1, a2, …. The subscripts locate the element’s position within the array, thereby giving direct access into the array. If the name of the array is a, then a[0] is the name of the element that is in position 0, a[1] is the name of the element that is in position 1, etc. In general, the ith element is in position i–1. So if the array has n elements, their names are a[0], a[1], a[2], …, a[n-1].

2 PROCESSING ARRAYS An array is a composite object: it is composed of several elements with independent values. In contrast, an ordinary variable of a primitive type is called a scalar object. The first example shows that array elements can be assigned and accessed the same as ordinary scalar objects. int main() { double a[3]; a[2] = 55.55; a[0] = 11.11; a[1] = 33.33; cout << "a[0] = " << a[0] << endl; cout << "a[1] = " << a[1] << endl; cout << "a[2] = " << a[2] << endl; }

3 Processing Arrays EXAMPLE Printing a Sequence in Order This program reads five numbers and then prints them in reverse order: int main() { const int SIZE=5; // defines the size N for 5 elements double a[SIZE]; // declares the array’s elements as type double cout << "Enter " << SIZE << " numbers:\t"; for (int i=0; i<SIZE; i++) cin >> a[i]; cout << "In reverse order: "; for (int i=SIZE-1; i>=0; i--) cout << "\t" << a[i]; }

4 INITIALIZING AN ARRAY This program initializes the array a and then prints its values: int main() { float a[] = { 22.2,44.4, 66.6 }; int size = sizeof(a)/sizeof(float); for (int i=0; i<size; i++) cout << "\ta[" << i << "] = " << a[i] << endl; } The first line declares a to be the array of 3 elements described above. The second line uses the sizeof() function to compute the actual number of elements in the array. The value of sizeof(float) is 4 because on this machine a float value occupies 4 bytes in memory. The value of sizeof(a) is 12 because the complete array occupies 12 bytes in memory. Therefore, the value of size is computed to be 12/4 = 3.

5 Initializing an Array with Trailing Zeros This program initializes the array a and then prints its values: int main() { float a[7] = { 22.2,44.4,66.6 }; int size = sizeof(a)/sizeof(float); for (int i=0; i<size; i++) cout << "\ta[" << i << "] = " << a[i] << endl; }

6 ARRAY INDEX OUT OF BOUNDS In some programming languages, an index variable will not be allowed to go beyond the bounds set by the array’s definition. For example, in Pascal, if an array a is defined to be indexed from 0 to 3, then the reference a[6] will crash the program. This is a security device that does not exist for arrays in C++ (or C). As the next example shows, the index variable may run far beyond its defined range without any error being detected by the computer. This program has a run-time error: it accesses a part of memory that is not allocated: int main() { const int SIZE=4; float a[SIZE] = { 33.3, 44.4,55.5,66.6 }; for (int i=0; i<7; i++) // ERROR: index is out of bounds! cout << "\ta[" << i << "] = " << a[i] << endl; }

7 PASSING AN ARRAY TO A FUNCTION The code float a[] that declares an array a in the previous examples tells the compiler two things: the name of the array is a, and the array’s elements have type float. The symbol a stores the array’s memory address. So the code float a[] provides all the information that the compiler needs to declare the array. The size of the array (i.e., the number of elements in the array) does not need to be conveyed to the compiler. C++ requires the same information to be passed to a function that uses an array as a parameter.

8 EXAMPLE Passing an Array to a Function that Returns its Sum int sum(int[],int); int main() { int a[] = { 11,33, 55,77 }; int size = sizeof(a)/sizeof(int); cout << "sum(a,size) = " << sum(a,size) << endl; } int sum(int a[],int n) { int sum=0; for (int i=0; i<n; i++) sum += a[i]; return sum; }

9 Input and Output Functions for an Array This program uses a read() function to input values into the array a interactively. Then it uses a print() function to print the array: void read(int[],int&); void print(int[],int); int main() { const int MAXSIZE=100; int a[MAXSIZE]={0},size; read(a,size); cout << "The array has " << size << " elements: "; print(a,size); } void read(int a[],in t& n) { cout << "Enter integers. Terminate with 0:\n"; n = 0; do { cout << "a[" << n << "]: "; cin >> a[n]; } while (a[n++] != 0 && n < MAXSIZE); --n; // don't count the 0 } void print(int a[],i nt n) { for (int i=0; i<n; i++) cout << a[i] << " "; }

10 Printing the Memory Location of an Array This program prints the value of the address stored in an array name. int main() { int a[] = { 22,44, 66,88 }; cout << "a = " << a; // the address of a[0] }

11 THE LINEAR SEARCH ALGORITHM Computers are probably used more for the storage and retrieval of information than for any other purpose. Data is often stored in a sequential structure such as an array. The simplest way to find an object in an array is start at the beginning and inspect each element, one after the other,until the object is found. This method is called the Linear Search algorithm.

12 EXAMPLE The Linear Search This program tests a function that implements the Linear Search algorithm: int index(int,int[],int); int main() { int a[] = { 22,44, 66,88,44,66,55 }; cout << "index(44,a,7) = " << index(44,a,7) << endl; cout << "index(50,a,7) = " << index(50,a,7) << endl; } int index(int x,int a[],int n) { for (int i=0; i<n; i++) if (a[i] == x) return i; return n; // x not found }

13 MULTIDIMENSIONAL ARRAYS The arrays we have used previously have all been one- dimensional. This means that they are linear; i.e., sequential. But the element type of an array can be almost any type, including an array type. An array of arrays is called a multidimensional array. A one- dimensional array of one-dimensional arrays is called a two-dimensional array; a one-dimensional array of two- dimensional arrays is called a three-dimensional array; etc. The simplest way to declare a multidimensional array is like this: double a[32][10][4]; This is a three-dimensional array with dimensions 32, 10, and 4. The statement a[25][8][3] = 99.99 would assign the value 99.99 to the element identified by the multi-index (25,8,3).

14 Reading and Printing a Two- Dimensional Array This program shows how a two- dimensional array can be processed: void read(int a[][5]); void print(cont int a[][5]); int main() { int a[3][5]; read(a); print(a); } void read(int a[][5]) { cout << "Enter 15 integers,5 per row:\n”; for (int i=0; i<3; i++) { cout << "Row " << i << ": "; for (int j=0; j<5; j++) cin >> a[i][j]; } void print(const int a[][5]) { for (int i=0; i<3; i++) { for (int j=0; j<5; j++) cout << " " << a[i][j]; cout << endl; }


Download ppt "Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively."

Similar presentations


Ads by Google