290 likes | 305 Views
Learn about arrays with more than one dimension, including two-dimensional arrays and matrix operations. Understand how to declare, initialize, and perform operations on multi-dimensional arrays. Also, explore dynamic memory allocation and ways to pass 2D arrays to functions.
E N D
CS111Computer Programming Array of more than one dimension
A[4][3] 0 0 0 1 1 1 2 2 2 0 0 1 1 2 2 3 3 Multi-dimensional Arrays B[2][4][3] • Arrays with two or more dimensions can be defined 0 1
A[4][3] 0 1 2 0 1 2 3 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}};
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 N 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 } • /* Valid declaration*/ • int abc[][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 };
Memory Mapping (Actual) int abc[2][2] = {1, 2, 3 ,4 };
2D Matrix - Recap • Declaring a 2D Array • 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 intmyInt[3][4]; myInt[0] myInt[1] myInt[2]
Memory map intmyInt[3][4]; Dynamic Memory Allocation How much memory (bytes) required? Sufficient for 12 Integers !!
intmyInt[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; myPtr 10
Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *(myPtr+1) = 20; myPtr+1 20
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+4 myPtr+5 myPtr+3 myPtr+8 myPtr+6 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 intmyInt[3][4]; • Pointer to AN INTEGER • Pointer to Array of INTEGERs void display(?); void display ( int (*myPtr)[4], int row, int col); void display ( int *myPtr, int row, int col); void display (intmyPtr[][4], int row, int col);
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);