King Fahd University of Petroleum and Minerals
Department of Information and Computer Science
ICS 313: Fundamentals of Programming Languages
Fall 1999-2000
Date: 16th September 1999	 	Assignment # 3: Imperative Paradigm	Due date: 25th September 1999

1. Written Exercises [Problems from Chapter 6 of the Textbook]:
Solve the following problems from pages 275-277 of the textbook.

Problems	Topic	
7	Non-Commutative Add Operation	
8	Non-Associative Add Operation	
10	Order of Evaluation	
15	Effect of Evaluation Order	
21	Order of Evaluation	

Q. 7. (pp. 275) 
Suppose that a function FUN(x) is defined in such a way that it changes its arguments (x) as part of its code, i.e. x will have a different value after FUN(x) terminates. Then for an expression such as A + FUN(A), + will not be commutative since A+ FUN(A)  FUN(A) + A. {See page 258 in the textbook}

Q. 8. (pp. 275) 
Assume that FUN(A) returns the value 4 and will change the value of A, regardless, to 7. Also assume that expressions containing the function FUN will always evaluate it first.. Then if for example A = 3, C=5 then:
A + (FUN(A) + C) =	3 + (4 + 5) = 3 + 9 = 12	(A + FUN(A)) + C =	(7 + 4) + 5 = 11 + 15 = 16

Q. 10. (pp. 276)
(a)	( ( ( a * b )1 - 1 )2 + c )3
(b)	( ( ( a * ( b - 1 )1 )2 / c )3 mod d )4
(c)	( ( ( a - b )1 / c )2 & ( ( ( d * e )3 / a )4 - 3 )5 )6
(d)	( ( - a )1 or (( c = d )2 and e)3 )4
(e)	( ( ( a > b )1 xor c) 2 or ( d <= 17 )3 )4
(f)	( - ( a + b )1 )2

Q. 15. (pp. 277) 
The value of sum1 is 48 and the value of sum2 is 48. The reason why both answers are the same is that the C compiler evaluates the operand that is a function call first, regardless of whether it is the left or right operand in an expression.

Q. 21. (pp. 277) 
a)	Left to right evaluation:
fun(&x) will be evaluated second. This will result in returning the value 4 plus changing x from 3 to 8. However, the addition will be performed between 4, which the value of fun(&x) and 3, which is the value of x resulting in assigning x the new value of 7.

b)	Right to left evaluation:
fun(&x) will be evaluated first. This will result in returning the value 4 plus changing x from 3 to 8. Now the addition will be performed between 4, which the value of fun(&x) and 8, which is the value of x resulting in assigning x the new value of 12. 

2. Written Exercises [Problems from Chapter 7 of the Textbook]:
	Solve the following problems from pages 319-321 of the textbook.

Problems	Topic	
10	Loops (using Java language only)	
12	Multiway-Selection (using C language only)	
16	Eliminating gotos from programs.	

Q. 10. (pp. 319) 
(e)	Java code for a loop structure:

for  (k = (j + 13) / 27; k <= 10; i = 3 * (++k) - 1)

Q. 12. (pp. 320) 
(d)	C code for multiple selections structure:

switch (k)
{
case 1: case 2:
	j = 2 * k - 1;
	break;
case 3: case 5:
	j = 3 * k + 1;
	break;
case 4:
	j = 4 * k - 1;
	break;
case 6: case 7: case 8:
	j = k - 2;
	break;
default:
	printf("Error in switch, k =%d\n", k);
}

Q. 16. (pp. 321)
Rewriting a C program using no gotos or breaks:

j = 3;
for (i = 0; i < 3; i++){
   key = j + 2
   if  ((key == 3) || (key == 2))
       j--;
   else if  (key == 0)
       j += 2;
   else  j = 0;
   if  (j > 0)   i = 3
   else  j = 3 - i;
}

3. Written Exercises [Problems from Chapter 8 of the Textbook]:
	Solve the following problems from pages 365-366 of the textbook.

Problems	Topic	
9	Illustration of different parameter passing techniques	
10	(More on) Parameter passing techniques	

Q. 9. (pp. 366) 
Assume that the address of the actual parameter is computed at subprogram entry, and is not recomputed at subprogram termination.
a)	Value:	After the call of the procedure sub:	LIST[1] = 3,  LIST[2] = 1
This because no change is done to the parameter since it is passed by value All the assignments were made to the local variable PARM while GLOBAL is changed to 2.
b)	Reference:	After the call of the procedure sub:	LIST[1] = 5,  LIST[2] = 1
This because the change to PARM is done actually in the variable LIST[1]. LIST [2] is not changed, remains 1, while GLOBAL is changed to 2.
c)	Name:	After the call of the procedure sub:	LIST[1] = 3,  LIST[2] = 5
Since each occurrence of PARM is replaced by LIST[GLOBAL] first LIST[1] will change to 3 then after changing GLOBAL to 2 LIST[2] will change to 5.
d)	Value-result:	After the call of the procedure sub:	LIST[1] = 5,  LIST[2] = 1
This because the change to PARM is copied back into LIST[1]. LIST [2] is not changed, remains 1, while GLOBAL is changed to 2.

Q. 10. (pp. 366) 
Assume the calls are not accumulative; that is, they are always called with the initialized values of the variables, so their effects are not accumulative.
e)	Value:	After the first call of the procedure swap:	value = 1,  list[0] = 3,  list[1] = 5
	After the second call of the procedure swap:	value = 1,  list[0] = 3,  list[1] = 5
This because no change is done to the parameters value and list[0] since they are passed by value All the swapping was made to the local variables a and b.
f)	Reference:	After the first call of the procedure swap:	value = 3,  list[0] = 1,  list[1] = 5
	After the second call of the procedure swap:	value = 3,  list[0] = 5,  list[1] = 1
This resulted from passing the address of value, list[0] and list[1] for a and b in the two calls and therefore, the swap actually took place on the memory location of the parameters.
g)	Name:	After the first call of the procedure swap:	value = 3,  list[0] = 1,  list[1] = 5
	After the second call of the procedure swap:	value = 3,  list[0] = 5,  list[1] = 1
This resulted from passing the address of value, list[0] and list[1] for a and b in the two calls and therefore, the swap actually took place on the memory location of the parameters.
h)	Value-result:	After the first call of the procedure swap:	value = 3,  list[0] = 1,  list[1] = 5
	After the second call of the procedure swap:	value = 3,  list[0] = 5,  list[1] = 1
This resulted from using the names of value, list[0] and list[1] replacing a and b in the tow calls. The swap actually took place between the parameters value, list[0] and list[1]

4. Written Exercises [Problems from Chapter 9 of the Textbook]:
	Solve the following problems from pages 401-403 of the textbook.

Problems	Topic	
3	Stack of Activation Records	
6	Display	

Q. 3. (pp. 401) 

	dynamic link	
ARI for B	static link	
	return (to C)	
	dynamic link	
ARI for C	static link	
	return (to A)	
	dynamic link	
ARI for A	static link	
	return (to BIGSUB)	
	dynamic link	
ARI for BIGSUB	static link	
	return	
	.	
	.	
	stack	

Q. 6. (pp. 402) 

				
ARI for D				
ARI for C				
ARI for A				
ARI for B			2	
ARI for A			1	
ARI for BIGSUB			0	
				
stack		display		

5. Programming Exercises in Java [Problems based on Java Lectures]:
	After skimming through the material made available as part of the Java lectures, arrange the different tutorials, programs, slides, and reference material in their appropriate folders in your ics313 folder. Examine the annotated sessions in the folder: JavaSlidePrograms. In particular, study the programs CountFile, Parameters, Simultaneous, and StringOperations. Develop a utility program copy that can copy a given source file into a given destination (you may start from the CountFile example).

6. Programming Exercises in Java [Optional Extra Credit Question]:
	Modify Copy (of exercise 5) so that it can make a recursive copy if the source is a directory.

7. Programming Exercises in Java [Optional Extra Credit Question  can be a substitute for a missed assignment when fully done with all documentation]:
	Develop a Java applet, so that it can be embedded on your home page (or may be in a page linked from your homepage). The applet reads a text file called, schedule.txt, located in your public_html folder, containing one or more lines of  text of the following form: 
day	from	to	place		description
As an example:
Saturday	9	10	22-161		ics 313
Note that all the fields are separated by a tab. You may make reasonable assumptions about the format if needed.
The applet, on being launched in a browser by whoever it is that is visiting your page, connects back to your web-server, reads the content of schedule.txt from your public_html folder and displays your schedule.