Download presentation
Presentation is loading. Please wait.
Published byHester Conley Modified over 8 years ago
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
Rules of declaring 1D arrays which type of data are the elements - int? float? or char? how many data or elements? – size of array. how to instruct the computer to remember an array in question? – name of the array. examine an array to be declared below, float a[3]; –type: float –size: 3, which means the array contains 3 elements. –name: a[]. Note that square brackets are used.
3
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; }
4
Further explanation of the rules Don’t miss “ ;” in array declaration. int a(10) is wrong. 1 st element index is a[0] instead of a[1]. int a[3]; is different from a[3];. size of arrays is an integer constant, never a variable, e.g., int n=3; a[n]; is wrong. However const int n=3; a[n]; is right.
5
Address of an array #include main() { float a[3]; cout << "&a[0]= " << &a[0] <<endl; cout << "&a= " << &a <<endl; cout << "a= " << a <<endl; } The addresses of array elements can be obtained by adding a sign & to the element, e.g., &a[0]. Run the following codes we can get the result: a=&a = &a[0].
6
Initialization of an array Method 1- full initialization when declaring an array – int a[4] = {3, 1, 0, 9}; –It gives the result of initialization, a[0]=3, a[1]=1, a[2]=0, a[3]=9. Method 2- partial initialization when declaring the array – int a[4] = {3, 1}; –It gives a[0]=3, a[1]=1, a[2]=0, a[3]=0. –This means that the missed element values are assigned with zeros automatically.
7
Initialization of an array Method 3- hidden initialization – int a[4]; –In this case all the elements will be automatically assigned with zeros. So the elements hold values as follows, –a[0]=0, a[1]=0, a[2]=0, a[3]=0. Method 4- initialization with the hidden array size –int a[] = {1,3,5,10}; –this array declaration is equivalent to –int a[4] = {1,3,5,10}; –This means the size of the array is determined by the compiler according to the actual number of elements.
8
Initialization of an array { float sale[7], key_in; int i; for (i=0; i < 7; i++) { cout <<"key_in= "; cin >> key_in; sale[i] = key_in; cout << "sale[" << i << "]= " << key_in <<endl; } } Method 5- initialization with a loop statement for…
9
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.
10
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; }
11
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).
12
Initialization of 2D arrays Method 1: – a[2][3]={{2, 4, 3},{7, 8, 1}}; method 2: – a[2][3]={2, 4, 3, 7, 8, 1};
13
Rules of declaring 2-D arrays Three basic issues: –which type of data - int? float? or char? –how many data? – sizes of array, the number of rows and the number of columns. –how to make the computer identify an array in question? - name float a[2][3]; –This array has 2 rows and 3 columns
14
A string as an array of characters The text bad! in the last example can be assigned, as a string “bad!”, to an array ex1[]. This method is more straightforward than the last one. Only thing to remember is that an additional character \0 is implicitly added to the string to terminate the string. So the array size should be one more. For this example, the text size is 4 and hence the array size is 5. { char ex1[5]="bad!";; cout<<ex1<<endl; }
15
Declaration of a 1D array for a string You may have found that the array name can also represent and output the string. Another string array sample without the declaration of size is as follows. In this case, the size of the array is automatically calculated, 11 (text size is 10). char ex2 [ ] = “30/09/2004”;
16
Input strings from the keyboard Input a string containing no spaces { char name[10]; cin >> name; } Input a string containing spaces { char name[10]; cin.get(name, n); // n is the number of characters }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.