140 likes | 160 Views
Learn about declaration, initialization, and accessing of multi-dimensional arrays in programming for engineering applications. Includes examples and syntax.
E N D
Computer Programming forEngineering Applications ECE 175 Intro to Programming
Multi-dimensional Arrays Declaration of arrays with more than one dimension Accessing arrays with multiple dimensions Syntax: data type array_name[size][size] Example: inttwo_dimensions[5][7]; doublemany_dimensions[10][20][30][50]; Example: inttwo_dimensions[5][7]; x = two_dimensions[1][4]; y = two_dimensions[2][4] + two_dimensions[3][1];
Subscripts of Two Dimensional Arrays i: 0 1 2 3 4 j: 01 2 3 456 Example: inttwo_dimensions[7][5]; x = two_dimensions[1][4]; // x = ? y = two_dimensions[2][4] + two_dimensions[0][1]; // y = ?
Initializing arrays with more than one dimension Nested for loops Declare two indexes that would run through the size of each dimension Write two for nested for loops Example: inttwo_dimensions[5][7]; int, i, j; for(i=0; i<5; i++) { for(j=0; j<7; j++) two_dimensions[i][j]=0; }
Initializing Multi-dimensional Arrays at Declaration You can initialize multi-dimensional arrays during declaration You can omit only the first dimension of the array Syntax: data typearray_name[size][size]={ {v1, v2, v3 ..vn}, {x1, x2, x3...xn},...,{z1, z2, ... zn}}; Example: int scores[3][4]={ {0,0,0,0},{0,0,0,0}, {0,0,0,0} }; float cells[][2]={ {0.0, 1}, {1.5, 2}, {0.3, 2}, {0, 0}}
Accessing a subset of table elements Use if conditions on the indexes to the array elements Example: inttable[4][4]; int, i,j; for(i=0; i<4; i++) { for (j=0; j<4; j++) { if (i >= j) table[i][j]=(i+1)*(j+1); } }
Passing multidimensional arrays to functions Only the first dimension can be omitted, when arrays are passed As in the case of one dimension, a pointer to the first element of the array is passed to the function There is a way to use pointers, but it is somewhat confusing. Stick to this notation Syntax: data type fun_name(intar_name[][size], intvar} Examples: void addition(int x[][5], int y[][5], int rows) voidthreeD(float z[][6][10])
Adding two matrices Write a C program that adds two two-dimensional matrices and prints out the result Program should have two functions, one that facilitates adding matrices, and one that facilitates printing.
Body of main #include<stdio.h> #define ROW 5 #define COL 5 voidprint_array(int x[][COL]); voidadd_array(int x[][COL], int y[][COL], int z[][COL]); int main(void) {int mat1[ROW][COL]={{1,1,1,1,1}}; int mat2[ROW][COL]; intresult[ROW][COL]; inti,j; for (i=0; i<ROW; i++) { for (j=0; j<COL; j++) { mat1[i][j]=i+j; mat2[i][j]=(i+1)*(j+1); }} add_array(mat1,mat2,result); print_array(mat1); printf("----------------------\n"); print_array(mat2); printf("----------------------\n"); print_array(result); return(0); }
Function for adding two 2-dim arrays voidadd_array(int x[][COL], int y[][COL], int z[][COL]) { int i, j; for (i=0; i<ROW; i++) { for (j=0; j<COL; j++) z[i][j]=x[i][j]+y[i][j]; } }
Function for printing 2-dim array Prints each row, then print a new line. voidprint_array(int x[][COL]) { inti,j; for (i=0; i<ROW; i++) { for (j=0; j<COL; j++) { printf("%4d",x[i][j]); } printf("\n"); } }
Game Fun – Let’s Play Tic-Tac-Toe Write a program that checks who won on a tic-tac-toe game
Body of main #include<stdio.h> #define P1 0 #define P2 1 #define TIE 2 intcheck_fun(int x[][3]); // Checking the winner of a tic-tac-toe board int main(void){ inti,j; // indexes for accessing rows and columns int board[3][3]; // the tic-tac-toe board FILE *inp=fopen("game1.dat", "r"); if(inp==NULL) // if file does not exist printf("Unable to load game file"); else{ for(i=0; i<3; i++){ for(j=0; j<3; j++) fscanf(inp, "%d", &board[i][j]); } // storing everyting on board if(check_fun(board)==P1) // calling check_fun function printf("The Xs Won!\n"); elseif (check_fun(board)==P2) printf("The Os Won!\n"); else printf("This is a Tie!\n"); } return 0; }
The Checking Function intcheck_fun(int x[][3]) { int i; intoutcome; // returned value for (i=0; i<3; i++) { // row sum or column sum or diagonal sum is equal to 3 if (x[i][0]+x[i][1]+x[i][2]==3 || x[0][i]+x[1][i]+x[2][i]==3 || x[0][0]+x[1][1]+x[2][2]==3 || x[0][2]+x[1][1]+x[2][0]==3) { outcome = P1; break;} elseif (x[i][0]+x[i][1]+x[i][2]==0 || x[0][i]+x[1][i]+x[2][i]==0 || x[0][0]+x[1][1]+x[2][2]==0 || x[0][2]+x[1][1]+x[2][0]==0) { outcome = P2; break; } else outcome=TIE; } returnoutcome; }