Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS111 Computer Programming

Similar presentations


Presentation on theme: "CS111 Computer Programming"— Presentation transcript:

1 CS111 Computer Programming
Array of more than one dimension

2 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

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}}; 1 2 3 A[4][3]

4 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.

5 Matrix multiplication
Multiply two numbers N Sum of N products

6 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

7 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 */

8 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 }

9 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

10 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

11 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

12 Memory map int myInt[3][4]; myInt[0] myInt[1] myInt[2]

13 How much memory (bytes) required?
Memory map int myInt[3][4]; Dynamic Memory Allocation How much memory (bytes) required? Sufficient for 12 Integers !!

14 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);

15 Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *myPtr = 10;

16 Dynamic allocation: 2D int *myPtr = (int*)malloc(48); *(myPtr+1) = 20;

17 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

18 2D Array as collection of 1D Array: Pointers
myPtr1[0] myPtr1[1] myPtr1[2]

19 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

20 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);

21 Changing size dynamically

22 Changing size dynamically
realloc() void *realloc( void *ptr, size_t new_size ) Defined in header <stdlib.h>

23 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.

24 Changing size dynamically
myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

25 Changing size dynamically
ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

26 Changing size dynamically
myPtr int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

27 Changing size dynamically
myPtr ptr_new int *myPtr = (int *)malloc(sizeof(int)*2); int ptr_new = (int *)realloc(ptr, sizeof(int)*3);

28 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 !!

29 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);


Download ppt "CS111 Computer Programming"

Similar presentations


Ads by Google