Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.

Similar presentations


Presentation on theme: "C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is."— Presentation transcript:

1 C++ Arrays SarMag Trimester 31 C++ Arrays

2 C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element of the array. The contents of each element are of the same type. –Could be an array of int, double, char, … We can refer to individual elements by giving the position number (index) of the element in the array.

3 C++ Arrays SarMag Trimester 33 Memory and Arrays int foo[6]; foo[0] foo[1] foo[5] 4 bytes Each int is 4 bytes

4 C++ Arrays SarMag Trimester 34 C++ Arrays start at 0 !!!!!!! The first element is the 0 th element! If you declare an array of n elements, the last one is number n-1. If you try to access element number n it is an error! If only millenniums started at 0 …

5 C++ Arrays SarMag Trimester 35 Array Subscripts The element numbers are called subscripts. foo[i] Array name subscript A subscript can be any integer expression: These are all valid subscripts: foo[17] foo[i+3] foo[a+b+c]

6 C++ Arrays SarMag Trimester 36 Array Example int main(void) { int facs[10]; for (int i=0;i<10;i++) facs[i] = factorial(i); for (int i=0;i<10;i++) cout << "factorial(" << i << ") is " << facs[i] << endl; }

7 C++ Arrays SarMag Trimester 37 Declaring An Array element_type array_name[number_of_elements]; element_type can be any C++ variable type. array_name can be any valid variable name. number_of_elements can be an expression.

8 C++ Arrays SarMag Trimester 38 Initialization You can initialize an array when you declare it (just like with variables): int foo[5] = { 1,8,3,6,12}; double d[2] = { 0.707, 0.707}; char s[] = { 'R', 'P', 'I' }; You don’t need to specify a size when initializing, the compiler will count for you.

9 C++ Arrays SarMag Trimester 39 Arrays of char are special C++ provides a special way to deal with arrays of characters: char string1[] = "RPI without PI is like meat without eat"; char arrays can be initialized with string literals.

10 C++ Arrays SarMag Trimester 310 Arrays of Arrays You can create an array of arrays: int a[2][2]; for (int i=0;i<2;i++) for (int j=0;j<2;j++) a[i][j] = i+j;

11 C++ Arrays SarMag Trimester 311 2-D Array: int A[3][4] Col 0Col 1Col 2Col 3 Row 0 A[0][0]A[0][1]A[0][2]A[0][3] Row 1 A[1][0]A[1][1]A[1][2]A[1][3] Row 2 A[2][0]A[2][1]A[2][2]A[2][3]

12 C++ Arrays SarMag Trimester 312 2-D Memory Organization char A[4][3]; A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] A[0] A[1] A[2] A[3] { { { { A is an array of size 4. Each element of A is an array of 3 chars

13 C++ Arrays SarMag Trimester 313 C++ does not have bounds checking This is the array This is something else Memory a[0] a[1] a[2] a[3] a[4] a[5] foo int a[6]; int foo;

14 C++ Arrays SarMag Trimester 314 Multidimentional Arrays SHINTA P STMIK MDP 2008

15 C++ Arrays SarMag Trimester 315 Introduction This chapter focuses on –Row- and column-major mapping and representations of multidimensional arrays

16 One Dimentional Array Representation 1-dimensional array x = [a, b, c, d] map into contiguous memory locations location(x[i]) = start + i Memory abcd start

17 Space Overhead space overhead = 4 bytes for start (excludes space needed for the elements of x) Memory abcd start

18 2D Arrays The elements of a 2-dimensional array a declared as: int a[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

19 Rows of a 2D Array a[0][0] a[0][1] a[0][2] a[0][3] row 0 a[1][0] a[1][1] a[1][2] a[1][3] row 1 a[2][0] a[2][1] a[2][2] a[2][3] row 2

20 Columns of a 2D Array a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] column 0column 1column 2column 3

21 2D Array Representation view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l

22 2D Array Representation abcd efgh ijkl x[] 4 separate 1-dimensional arrays space overhead = overhead for 4 1D arrays = 4 * 4 bytes = 16 bytes = (number of rows + 1) x 4 bytes

23 Array Representation This representation is called the array-of-arrays representation. Requires contiguous memory of size 3, 4, 4, and 4 for the 4 1D arrays. 1 memory block of size number of rows and number of columns abcd efgh ijkl x[]

24 Row-Major Order (RMO) Mapping Example 3 x 4 array: a b c d e f g h i j k l Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} row 0 row 1row 2…row i

25 Locating Element x[i][j] assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] x[i][j] is mapped to position ic + j of the 1D array row 0 row 1row 2…row i 0c2c3cic

26 Space Overhead 4 bytes for start of 1D array + 4 bytes for c (number of columns) = 8 bytes Note that we need contiguous memory of size rc. row 0row 1row 2…row i

27 Column-Major Order (CMO) Mapping a b c d e f g h i j k l Convert into 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. We get y = {a, e, i, b, f, j, c, g, k, d, h, l}

28 C++ Arrays SarMag Trimester 328 Row- and Column-Major Order Mappings 2D Array int a[3][6]; a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[0][5] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] a[1][5] a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] a[2][5]

29 C++ Arrays SarMag Trimester 329 Next Lecture STACK  2010


Download ppt "C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is."

Similar presentations


Ads by Google