/* * programming_arrays2d.c * * Created on: Apr 18, 2023 * Author: johnsontimoj */ //////////////////// // // practice with 2d arrays // // inputs - user // outputs - print // ////////////////// #include int main(void){ setbuf(stdout, NULL); int r; int c; /////////////////// // declare some arrays /////////////////// int ary1[3][5]; int ary2[3][5] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // int ary2[3][5] = {{1, 2, 3, 4, 5}, { 6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}; int ary3[3][5] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} }; int ary4[4][4]; char ary5[10][10]; // print the array values // remember - structure is [row][col] - [0][0], [0][1], [0][2], [1][0] ... // so the outside for loop controls the row, the inside for loop controls the col for(r = 0; r < 3; r++) for(c = 0; c < 5; c++) printf("%i ", ary1[r][c]); printf("\n"); for(r = 0; r < 3; r++) for(c = 0; c < 5; c++) printf("%i ", ary2[r][c]); printf("\n"); for(r = 0; r < 3; r++) for(c = 0; c < 5; c++) printf("%i ", ary3[r][c]); printf("\n"); for(r = 0; r < 3; r++) for(c = 0; c < 5; c++) printf("%p ", &ary3[r][c]); printf("\n"); // read in some values printf("Please enter the values for your 3x5 array in row-col order: "); for(r = 0; r < 3; r++) for(c = 0; c < 5; c++) scanf("%i", &ary2[r][c]); // print with row markers for(r = 0; r < 3; r++){ for(c = 0; c < 5; c++) printf("%i ", ary2[r][c]); printf("| "); } printf("\n"); // print a few specific values printf("ary2[2][2] = %i\n", ary2[2][2]); printf("ary2[1][5] = %i\n", ary2[1][5]); printf("ary2[3][2] = %i\n", ary2[3][2]); printf("ary2[2][7] = %i\n", ary2[2][7]); // create an identity matrix for(r = 0; r < 4; r++) for(c = 0; c < 4; c++) if(r == c) ary4[r][c] = 1; else ary4[r][c] = 0; // print in array format for(r = 0; r < 4; r++){ for(c = 0; c < 4; c++) printf("%i ", ary4[r][c]); printf("\n"); } printf("\n"); // set the sides of an array to |, top and bottom to -, everything else to space for(r = 0; r < 10; r++) for(c = 0; c < 10; c++) if((r == 0) || (r == 9)) ary5[r][c] = '-'; else if((c == 0) || (c == 9)) ary5[r][c] = '|'; else ary5[r][c] = ' '; // print in array format for(r = 0; r < 10; r++){ for(c = 0; c < 10; c++) printf("%c ", ary5[r][c]); printf("\n"); } printf("\n"); return 0; }// end main