






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Programming & Problem Solving II; Subject: Computer Engr & Computer Sci; University: California State University - Long Beach; Term: Unknown 1989;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Carrano, Helman, Veroff, pp. A25-A
Defn : Arrays are indexed collections of components all of the same type. When using an array in C++, the dimensions and type of elements stored in the array must be declared. The dimensions of the array will always begin with 0. Example: const int CALENDAR = 12; double Income[CALENDAR]; This declaration will create an array called Income made up of twelve variables of type double. The indices of the array will be 0 to 11.
B. Memory and Allocation Basics Typical storage types for computers:
The size of the storage space limits the quantities that can be stored. Example: For INTEGERS: 1 Byte -128..127, since 2 8 = 2 Bytes -32,768..32767, since 2 16 = 4 Bytes -2,147,483,648.. 2,147,483,637, since 2 32 =4,294,967, For FLOATS: The more Bytes available, the more digits that can be stored after the decimal point.
typedef int ID[4]; ID X; X[0] X[1] X[2] X[3] 424 425 426 427 428 429 430 431 To compute the location of X[j]: 424 + (j - 0) * 2 In general, the formula for an array A is: &A[j] = Starting Address + (j - 1 st Index) * Component Size The compiler calculates this arithmetic formula so that the elements of the array can be easily accessed.
typedef int Data[8]; typedef Data Total[97]; Total S; If integers are each 2 Bytes, then the size of each component of S is: (8 * 2) = 16 = the full size of Data To find S[j]: &S[j] = Starting Address + (j - 1st Index of Total) * 16 To find S[j][k]: &S[j][k] = S[j] + (k - 1st Index of Data) * Component size of Data = Starting address + (j - 0) * 16 + (k - 0) * 2
In storing a 2-dimensional array (matrix), either the rows or columns can be grouped together. For simplicity, we will assume the rows are grouped together in row-major order. Example: typedef float Matrix[4][3], Matrix Z; In row-major order , the rows are stored together. The elements are grouped by the 1 st index. Z(0,-) Z(1,-) Z(2,-) Z(3,-) 100 111 112 123 124 135 136 147
Defn: An unconstrained array is an array in which the bounds of the array, and the indices of the array, are not specified when the type is declared. Unlike Ada, the C++ language does not have built-in unconstrained arrays. In C++, the indices of arrays must be declared, and all arrays begin with index 0. However, a programmer can create an array class that permits the equivalent of Ada's unconstrained arrays and beginning index value.
Example: A vector class for one-dimensional integer arrays: class vector { public: // vector constructor -- last_index = // first_index + vector_size - 1 // space is allocated dynamically vector(int vector_size, int first_index = 0); // … private: int * array; int size; int first; int last; }; To instantiate two one-dimensional arrays, one consisting of 5 elements beginning with index 0 and the other consisting of 10 elements beginning with index 1: vector array_A(5); vector array_B(10, 1);