CS111 Computer Programming Array of more than one dimension
Multi-dimensional Arrays 1 2 3 A[4][3] B[2][4][3] 1 2 1 2 1 2 3 1 Arrays with two or more dimensions can be defined
Two Dimensional Arrays Declaration: int A[4][3] : 4 rows and 3 columns, 4 × 3 array Elements: A[i][j] - element in row i and column j of array A Note: rows/columns numbered from 0 Storage: row-major ordering elements of row 0, elements of row 1, etc Initialization: int B[2][3]={{4,5,6},{0,3,5}}; 1 2 3 A[4][3]
Matrix Operations An m-by-n matrix: M: m rows and n columns Rows : 1, 2, … , m and Columns : 1, 2, … , n M(i,j) : element in ith row, jth column, 1 ≤ i ≤ m, 1 ≤ j ≤ n Array indexes in C start with 0.
Matrix multiplication Multiply two numbers N Sum of N products
Using Matrix Operations main(){ int a[11][11], b[11][11], c[11][11]; / * max size 10 by 10 */ int aRows, aCols, bRows, bCols, cRows, cCols; int i, j, k; scanf("%d%d", &aRows, &aCols); for(int i = 1; i <= arows; i++) for(int j = 1; j <= acols; j++) scanf("%d", &a[i][j]); scanf("%d%d", &bRows, &bCols); for(int i = 1; i <= brows; i++) for(int j = 1; j <= bcols; j++) scanf("%d", &b[i][j]); … Remember bRows=aCols
cRows = aRows; cCols = bCols; for(int i = 1; i <= crows; i++) for(int j = 1; j <= ccols; j++) { c[i][j]=0; for(int k = 1; k <= aCols; k++) c[i][j] += a[i][k]*b[k][j]; } for(int i = 1; i <= crows; i++){ for(int j = 1; j <= ccols; j++) /* print a row */ printf("%d ", c[i][j]); /* notice missing \n */ printf("\n"); /* print a newline at the end a row */
Initialization /* Valid declaration*/ int abc[2][2] = {1, 2, 3 ,4 } /* Invalid declaration – you must specify 2nd dimension*/ int abc[][] = {1, 2, 3 ,4 } /* Invalid */ int abc[2][] = {1, 2, 3 ,4 }
Memory Mapping (Conceptual) int abc[2][2] = {1, 2, 3 ,4 }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 … 1 2 3 4
Memory Mapping (Actual) int abc[2][2] = {1, 2, 3 ,4 }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 … 1 2 3 4
2D Matrix - Recap Declaring a 2D Array Memory allocated int myInt[3][4]; float myFloat[5][7]; char myChar[4][10]; Memory allocated sizeOf(Type of variable) * number of 1D array* number elements in one 1D array
Memory map int myInt[3][4]; myInt[0] myInt[1] myInt[2]
How much memory (bytes) required? Memory map int myInt[3][4]; Dynamic Memory Allocation How much memory (bytes) required? Sufficient for 12 Integers !!
int myInt[3][4]; One integer variable’s size = 4 bytes. So total memory = 12 x 4 = 48 bytes. Syntax: int *myPtr = (int*)malloc(48);
Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *myPtr = 10;
Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *(myPtr+1) = 20;
Dynamic allocation: 2D myPtr+i*4+j = myPtr + 2*4+1 = myPtr + 9 int *myPtr = (int*)malloc(48); Dynamic allocation: 2D scanf("%d",myPtr+i*4+j); myPtr+i*4+j = myPtr + 2*4+1 = myPtr + 9 myPtr+1 myPtr+2 myPtr myPtr myPtr+3 myPtr+4 myPtr+5 myPtr+6 myPtr+8 myPtr+7 myPtr+9
2D Array as collection of 1D Array: Pointers myPtr1[0] myPtr1[1] myPtr1[2]
Function and 2D Array Passing 1D Array to a function Always pass by reference Need to pass the size of the array Passing 2D Array to a function Pass by reference Need to pass the size of individual arrays Can be accessed via pointers in the function Operations (if any) is on the actual array locations Need not return an array
Ways of Passing 2D Array (E.g. Integer) to a Function int myInt[3][4]; void display(?); Pointer to AN INTEGER Pointer to Array of INTEGERs void display ( int (*myPtr)[4], int row, int col); void display ( int *myPtr, int row, int col); void display (int myPtr[][4], int row, int col);
Changing size dynamically
Changing size dynamically realloc() void *realloc( void *ptr, size_t new_size ) Defined in header <stdlib.h>
Reallocation procedure Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with a call to free or realloc. The reallocation is done by either: Expanding or contracting the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. Allocating a new memory block of size new_size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block. If there is not enough memory, the old memory block is not freed and null pointer is returned.
Changing size dynamically myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);
Changing size dynamically ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);
Changing size dynamically myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);
Changing size dynamically myPtr ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);
Changing size dynamically ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3); Content of first two element remain same !!
Free allocated memory Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space. free(ptr);