/*array.c */ /*an multi-dimensional array example */ /*Author: Weiwei Chen */ /*Date: Nov. 7. 2010 */ #include int findmax(int p[3][2]); int findmin(int p[3][2]); void modify_pv(int p, int newvalue); void modify_pr(int p[3][2], int i, int j, int newvalue); int main() { int i, j, max, min; int ma[3][2] = {{1, 2}, {3, 4}, {5, 6}}; /* display the content of the array */ for(i = 0; i < 3; i ++) { for(j = 0; j < 2; j ++) { printf("ma[%d][%d] = %d \n", i, j, ma[i][j]); } } /* find the maximum and minimum value in the array*/ max = findmax(ma); min = findmin(ma); printf("the maximum value is: %d \n", max); printf("the minimum value is: %d \n", min); /* modify the content of the element ma[1][1] by passing as value*/ printf("the value of ma[%d][%d] before modification is %d \n", 1, 1, ma[1][1]); modify_pv(ma[1][1], 100); printf("the value of ma[%d][%d] after modification is %d \n", 1, 1, ma[1][1]); /* modify the content of the element ma[1][1] by passing as referenc*/ printf("the value of ma[%d][%d] before modification is %d \n", 1, 1, ma[1][1]); modify_pr(ma, 1, 1, 100); printf("the value of ma[%d][%d] after modification is %d \n", 1, 1, ma[1][1]); return 0; } int findmax(int p[3][2]) { int max = p[0][0]; int i, j; for(i = 0; i < 3; i ++) { for(j = 0; j < 2; j ++) { if(max < p[i][j]) { max = p[i][j]; } } } return max; } int findmin(int p[3][2]) { int min = p[0][0]; int i, j; for(i = 0; i < 3; i ++) { for(j = 0; j < 2; j ++) { if(min > p[i][j]) { min = p[i][j]; } } } return min; } void modify_pv(int p, int newvalue) { printf("the original value of p is %d \n", p); p = newvalue; printf("the new value of p is %d \n", p); } void modify_pr(int p[3][2], int i, int j, int newvalue) { printf("the original value of p[%d][%d] is %d \n", i, j, p[i][j]); p[i][j] = newvalue; printf("the new value of p[%d][%d] is %d \n", i, j, p[i][j]); }