Download presentation
Presentation is loading. Please wait.
Published bySabina Garrett Modified over 6 years ago
1
2-D arrays a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32
int a[4][3]; … a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a00 a01 a02 a10 a11 a12 a20 a21 a22 a30 a31 a32 row 0 row 1 row 2 stored in memory in row-major order in C row 4
2
2-D arrays When a 2-D array is passed as a parameter to a function, we must provide the second dimension. e.g., given the following declaration: int matrix[5][7]; and a function, fun1, that takes a two-dimensional array as an argument, the prototype for fun1 can be either of the following: void fun1(int matrix[][7]); void fun1(int (*mat)[7]); The compiler must know the sizes of the second dimension in order to evaluate subscripts, thus the prototype must declare these dimensions.
3
2-D arrays In fun1(), we can access an element of matrix by using either of the following: matrix[i][j] (*matrix)[j]
4
2-D arrays as parameters
#include <stdio.h> // function prototype int add_elements(int a[ ][3], int r, int c); int main() { int a[4][3] = {{1, 3, 5}, {2, 4, 6}, {1, 5, 9}, {4, 8, 12}}; int sum; int r; int c; printf("\narray elements:\n"); for(r = 0; r < 4; r++) for(c = 0; c < 3; c++) printf("%4d", a[r][c]); printf("\n"); } sum = add_elements(a, 4, 3); printf("\nsum = %d\n\n", sum); return 0;
5
2-D arrays as parameters
int add_elements(int a[][3], int r, int c) { int sum = 0; int rndx; int cndx; for(rndx = 0; rndx < r; rndx++) for(cndx = 0; cndx < c; cndx++) sum += a[rndx][cndx]; return sum; }
6
2-D arrays The name of a two-dimensional (or multidimensional) array is a pointer to a one-dimensional array -- the first row. In a two-dimensional array, the following two expressions are equivalent *(*(a + i) + j) and a[i][j]
7
Dynamic allocation of 2-D array
array is a pointer to a pointer to an int
8
Dynamic allocation of 2-D array
#include <stdlib.h> int **array; array = malloc(nrows * sizeof(int *)); if(array == NULL) { fprintf(stderr, "out of memory\n"); exit(1); } for(i = 0; i < nrows; i++) array[i] = malloc(ncolumns * sizeof(int)); if(array[i] == NULL)
9
Freeing an allocated 2-D array
// free memory for (i = 0; i < nrows; i++) free(array[i]); free(array);
10
Ragged 2-D array array
11
Ragged Arrays #include <stdio.h> #include <stdlib.h> #include <assert.h> // prototypes int **buildRagged(int rowMax); void fillRagged(int **ragged, int colMax, FILE *in); void printRagged(int **ragged, int colMax);
12
Ragged Arrays int main(int argc, char *argv[]) { int **ragged; int rowMax; int i; FILE *input; assert(argc > 1); input = fopen(argv[1], "r"); assert(input !=NULL); printf("Enter number of rows: "); scanf("%d", &rowMax); assert(rowMax > 0); ragged = buildRagged(rowMax); fillRagged(ragged, rowMax, input); printRagged(ragged, rowMax);
13
Ragged Arrays free(ragged[i]); free(ragged); // main (cont'd)
// close file fclose(input); // free array for (i = 0; i < rowMax; i++) free(ragged[i]); free(ragged); return 0; }
14
Ragged Arrays int **buildRagged(int rowMax) { int colMax; int **ragged; int row; ragged = (int **)malloc(rowMax * sizeof(int *)); for(row = 0; row < rowMax; row++) printf("Enter number of elements in row %d: ", row); scanf("%d", &colMax); ragged[row] = (int *)malloc((colMax + 1) *sizeof(int)); ragged[row][0] = colMax + 1; } return ragged;
15
Ragged Arrays void fillRagged(int **ragged, int rowMax, FILE *in) { int row; int col; int colMax; for(row = 0; row < rowMax; row++) colMax = ragged[row][0]; for(col = 1; col < colMax; col++) fscanf(in, "%d", ( *(ragged + row) + col)); }
16
Ragged Arrays void printRagged(int **ragged, int rowMax) { int row; int col; int colMax; int columns; printf("\nPrinting ragged array:\n"); for(row= 0; row < rowMax; row++) { colMax = *(*(ragged + row)); columns = colMax - 1; printf("row %d: size %2d: ", row, columns ); for(col = 1; col < colMax; col++) { printf("%4d", *(*(ragged + row) + col)); } printf("\n");
17
Ragged Arrays Sample Run: (red is user input) Enter number of rows: 4 Enter number of elements in row 0: 3 Enter number of elements in row 1: 4 Enter number of elements in row 2: 5 Enter number of elements in row 3: 2 Printing ragged array: row 0: size 3: row 1: size 4: row 2: size 5: row 3: size 2: 7 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.