1 / 29

CS111 Computer Programming

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.

sofiaw
Download Presentation

CS111 Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS111Computer Programming Array of more than one dimension

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

  3. 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}};

  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 N 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 } • /* 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 }

  9. Memory Mapping (Conceptual) int abc[2][2] = {1, 2, 3 ,4 };

  10. Memory Mapping (Actual) int abc[2][2] = {1, 2, 3 ,4 };

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

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

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

  14. intmyInt[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; myPtr 10

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

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

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

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

More Related