Spring 99, CSE 428: Solution of Assignment 3 Exercise #1: 1 (call by value) the output is 2 2 (call by reference) the output is 3 3)(call by value-result) the output is 1 Exercise #2 1) (static scope) the output is 2 2) (dynamic scope) the output is 1 Exercise #3 The best solution is the following: /* Solution 1 */ void search_exit(int i, int j, int *found){ if (A[i][j] == '*') *found = 0; else if (i==0 || i==m || j==0 || j==n) *found = 1; else { A[i][j] = '*'; search_exit(i-1,j,found); if (! *found){ search_exit(i,j+1,found); if (! *found){ search_exit(i+1,j,found); if (! *found) search_exit(i,j-1,found); } } } if (*found) A[i][j]='o'; /* new instruction (wrt the LN) */ } Another possibility is the following /* Solution 2 */ void search_exit(int i, int j, int *found){ if (A[i][j] == '*') *found = 0; else if (i==0 || i==m || j==0 || j==n) { *found = 1; A[i][j] = 'o'; /* new instruction */ } else { A[i][j] = '*'; search_exit(i-1,j,found); if (*found) A[i][j] = 'o'; /* new instruction */ else { search_exit(i,j+1,found); if (*found) A[i][j] = 'o'; /* new instruction */ else { search_exit(i+1,j,found); if (*found) A[i][j] = 'o'; /* new instruction */ else { search_exit(i,j-1,found); if (*found) A[i][j] = 'o'; /* new instruction */ } } } } } This second solution is equivalent to the first (although redundant). Finally, there is another possibility: /* Solution 3 */ void search_exit(int i, int j, int *found){ if (A[i][j] == '*') *found = 0; else if (i==0 || i==m || j==0 || j==n) *found = 1; else { A[i][j] = '*'; search_exit(i-1,j,found); if (*found) A[i-1][j] = 'o'; /* new instruction */ else { search_exit(i,j+1,found); if (*found) A[i][j+1] = 'o'; /* new instruction */ else { search_exit(i+1,j,found); if (*found) A[i+1][j] = 'o'; /* new instruction */ else { search_exit(i,j-1,found); if (*found) A[i][j-1] = 'o'; /* new instruction */ } } } } } This third solution is "almost" correct: the difference with the other two solutions is that it does not put an 'o' in the starting place.