/* Define a 2-dimensional array and show modification of an element in the array by pass by value and pass by reference Emad Arasteh November 15, 2018 */ #include int find_min(int m[][5]); int find_max(int m[][5]); void modify_pv(int p, int update); void modify_pr(int p[][5], int r, int c, int update); int find_min(int m[][5]) { int min = m[0][0]; int r, c; for (r = 0; r < 4; r++) { for (c = 0; c < 5; c++) { if (m[r][c] < min) { min = m[r][c]; } } } return min; } int find_max(int m[][5]) { int max = m[0][0]; int r, c; for (r = 0; r < 4; r++) { for (c = 0; c < 5; c++) { if (m[r][c] > max) { max = m[r][c]; } } } return max; } void update_pv(int p, int update) { printf("The original value of p is %d \n", p); p = update; printf("The updated value of p is %d \n", p); } void update_pr(int p[][5], int r, int c, int update) { printf("The original value of p[%d][%d] is %d \n", r, c, p[r][c]); p[r][c] = update; printf("The updated value of p[%d][%d] is %d \n", r, c, p[r][c]); } int main() { int matrix[4][5] = {{10, 20, 30, 40, 50}, {0, -1, 20, 50, 5}, {24, 41, 55, 88, 56}, {32, 3, 10, 4, 2}}; int r, c; int min, max; for (r = 0; r < 4; r++) { for (c = 0; c < 5; c++) { printf("matrix[%d][%d]: %d \n", r, c, matrix[r][c]); } } min = find_min(matrix); max = find_max(matrix); printf("The min value in the matrix is %d \n", min); printf("The max value in the matrix is %d \n", max); /* Update content of the matrix[1][1] by pass by value */ printf("The value of matrix[1][1] before update is %d \n", matrix[1][1]); update_pv(matrix[1][1], 100); printf("The value of matrix[1][1] after update is %d \n", matrix[1][1]); /* Update content of the matrix[1][1] by pass by reference */ printf("The value of matrix[%d][%d] before update is %d \n", 1, 1, matrix[1][1]); update_pr(matrix, 1, 1, 100); printf("The value of matrix[%d][%d] after update is %d \n", 1, 1, matrix[1][1]); return 0; }