/* * programming_arrays2d_fns.c * * Created on: Apr 18, 2023 * Author: johnsontimoj */ //////////////////// // // practice with 2d arrays and functions // // inputs - user // outputs - print // ////////////////// #include void print_ary(int rows, int cols, const int array[][cols]); void read_ary(int rows, int cols, int array[][cols]); void multary(int rows, int cols,int array[][cols], int mult); void copyary(int rows, int cols, const int srcary[][cols], int destary[][cols]); void swapary(int rows, int cols, int array[][cols]); void identity(int rows, int cols, int array[][cols]); void invidentity(int rows, int cols, int array[][cols]); void print_row(int cols, const int array[]); void print_ary_3d(int x, int y, int z, const int array[][y][z]); int main(void){ setbuf(stdout, NULL); int nrows; int ncols; int ary1[4][4]; int ary2[3][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} }; // print an array - garbage print_ary(4, 4, ary1); // Create and fill an array printf("Please enter the dimensions of your 2d array: "); scanf("%i %i", &nrows, &ncols); int ary3[nrows][ncols]; read_ary(nrows, ncols, ary3); print_ary(nrows, ncols, ary3); // multiply the values in an array multary(3, 5, ary2, 2); print_ary(3, 5, ary2); // swap the values in the rows of an array swapary(3, 5, ary2); print_ary(3, 5, ary2); // create an identity matrix identity(4, 4, ary1); print_ary(4, 4, ary1); // create a reverse identity matrix invidentity(4, 4, ary1); print_ary(4, 4, ary1); // print a single row of an array print_row(5, ary2[2]); print_row(4, ary2[0]); // create and print a 3d array int ary3d[3][2][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}}}; print_ary_3d(3,2,4,ary3d); return 0; } ////////////////////////////// // print_ary() // // print a 2d array - matrix format // // inputs: num rows, num cols, pointer to the array // outputs: prints the array ////////////////////////////// void print_ary(int rows, int cols, const int array[][cols]){ int r; int c; for(r = 0; r < rows; r++){ for(c = 0; c < cols; c++) printf("%i\t", array[r][c]); printf("\n"); } printf("-------------------\n"); return; }// end print_ary ////////////////////////////// // read_ary() // // function to read in a 2D array // // inputs: num rows, num cols, pointer to the array // outputs: array modified via pointer ////////////////////////////// void read_ary(int rows, int cols, int array[][cols]){ int r; int c; printf("Please enter array values: "); for(r = 0; r < rows; r++) for(c = 0; c < cols; c++) scanf("%i", &array[r][c]); return; }// end readary ////////////////////////////// // multary() // // function to multiply the values in an array by a value // // inputs: num rows, num cols, pointer to the array, multiplier // outputs: array modified via pointer ////////////////////////////// void multary(int rows, int cols,int array[][cols], int mult){ int r; int c; for(r = 0; r < rows; r++) for(c = 0; c < cols; c++) array[r][c] *= mult; return; }// end multary ////////////////////////////// // copyary() // // function to copy a 2D array // // inputs: num rows, num cols, pointer to the src and dest arrays // outputs: array modified via pointer ////////////////////////////// void copyary(int rows, int cols, const int srcary[][cols], int destary[][cols]){ int r; int c; for(r = 0; r < rows; r++) for(c = 0; c < cols; c++) destary[r][c] = srcary[r][c]; return; }// end copyary ////////////////////////////// // swapary() // // function to swap the values in the rows of a 2D array // // inputs: num rows, num cols, pointer to the array // outputs: array modified via pointer ////////////////////////////// void swapary(int rows, int cols, int array[][cols]){ int r; int c; int tmpary[3][5]; copyary(3, 5, array, tmpary); for(r = 0; r < rows; r++) for(c = 0; c < cols; c++) array[r][c] = tmpary[r][(cols - 1) - c]; return; }// end swapary ////////////////////////////// // identity() // // function to create a 2D identity array // // inputs: num rows, num cols, pointer to the array // outputs: array modified via pointer ////////////////////////////// void identity(int rows, int cols, int array[][cols]){ int r; int c; for(r = 0; r < rows; r++) for(c = 0; c < cols; c++){ if(r == c) array[r][c] = 1; else array[r][c] = 0; }// end inner return; } // end identity ////////////////////////////// // invidentity() // // function to create a 2D reverse identity array // // inputs: num rows, num cols, pointer to the array // outputs: array modified via pointer ////////////////////////////// void invidentity(int rows, int cols, int array[][cols]){ int r; int c; for(r = 0; r < rows; r++) for(c = 0; c < cols; c++){ if(r == ((cols - 1) -c)) array[r][c] = 1; else array[r][c] = 0; } return; } // end invidentity ////////////////////////////// // print_row() // // function to print a row of a 2D array // // inputs: num rows, num cols, pointer to the array // outputs: prints a row ////////////////////////////// void print_row(int cols, const int array[]){ int i; for(i = 0; i< cols; i++) printf("%i\t", array[i]); printf("\n-----------------\n"); return; }// end print_row ////////////////////////////// // print_ary_3d() // // function to print a 3D array in matrix format // // inputs: num rows, num cols, pointer to the array // outputs: prints the array ////////////////////////////// void print_ary_3d(int x, int y, int z, const int array[][y][z]){ int i; int j; int k; for(j = 0; j < y; j++){ for(i = 0; i < x; i++){ for(k = 0; k < z; k++) printf("%i\t", array[i][j][k]); printf("\t\t"); } printf("\n"); } return; }// end print_ary_3d