Final Examination - December 1996
APS105F - Computer Fundamentals
Examiner - Paul Chow
1 [10] -10pt(100,25)
2 [20] -10pt(100,25)
3 [15] -10pt(100,25)
4 [15] -10pt(100,25)
5 [10] -10pt(100,25)
6 [30] -10pt(100,25)
Total [100] -10pt(100,25)
Print:
First Name:Last Name:
Login ID:
Student Number:
#include <stdio.h> int mystery(int, int, int); main() { int a, b, c; do { printf("Enter three integers: "); scanf("%d%d%d", &a, &b, &c); printf("The result is: %d\n",mystery(a,b,c)); } while (a > 0); } int mystery(int x, int y, int z) { int this = x; if (y > this) this = y; if (z > this) this = z; return this; }
What does the program do?
Continued...
Question 2 continued...
Write an equivalent OOT program.
procedure Order (var X, Y : real) var temp : real if X > Y then temp := X X := Y Y := temp end if end Order var a, b : real for i : 1 .. 10 put "Enter a pair of numbers: ".. get a, b Order(a,b) put "In order, the numbers are: ",a," ",b end for
int ivar, jvar; int *iptr; iptr = &ivar; ivar = 5; jvar = 20; /* A */ *iptr = jvar + ivar; /* B */
Assume that the variables are stored in memory at the following addresses:
What are the values of the variables at points A and B?
Question 3 continued...
Question 3 continued...
You have been asked to lead the development of a new software product. For a status report that you are writing, you need to include a few paragraphs describing the programming language that you have chosen to use and the reasons for selecting it. Write those paragraphs here, and include in your discussion at least two arguments for and two against the use of strongly-typed languages such as Turing versus C. Finally give your choice and the reasons.
Marks will be given for both the technical content and the style of presentation.
Consider the following program:
#include <stdio.h> #define SIZE 5 void someFunction(int b[ ],int size); main() { int a[SIZE] = {32,27,64,18,95}; printf("Here is the output:\n"); someFunction(a,SIZE); printf("\n"); return 0; } void someFunction(int b[ ], int size) { /* Enter the values of b[0] and size at this point */ if (size > 0) { someFunction(&b[1], size-1); printf("%d ",b[0]); } }
Continued on next page...
Question 5 continued...
In this question you will develop part of a program that implements a part of a language called Logo. Logo was famous for the concept called turtle graphics. Imagine a mechanical turtle that walks around the room under the control of a Turing program. The turtle holds a pen in either the up position or the down position. While the pen is down, the turtle draws on the surface as it moves; while the pen is up, the turtle moves without writing anything. The program being developed will simulate the operation of the turtle and create a computerized drawing.
Assume the turtle moves about an array called World that is initialized to be all zeroes. The size of the world is determined by two constants called EndOfWorldX and EndOfWorldY. Commands are read from the terminal as a series of integer numbers. The program keeps track of the position of the turtle using the variables CurX and CurY. Whenever the turtle hits the edge of the world, it will stop at the edge. (Otherwise, we will lose too many turtles due to bad driving!)
There are four variables (N, S, E, W) that determine the direction of travel, corresponding to north (up), south (down) , east (right), and west (left).
The turtle is initially placed approximately at the middle of the World with the pen up. The variable PenIsDown is used to keep track of the position of the pen. When the turtle moves with the pen down, the appropriate elements of the World array are set to 1.
When the print command is given, the array is printed on the terminal. Wherever a 1 is encountered in the array, print an `x', otherwise print a blank.
The set of commands to be processed by the turtle are:
Continued...
Question 6 continued...
In the following example, the turtle is assumed to be near the middle of the world. The commands will cause it to draw a 6 by 6 square and print the output.
2 5 6 3 5 6 3 5 6 3 5 6 1 6 9
The skeleton of the program you are to complete is shown in Appendix A on page . Study this code to see how it works.
Question 6 continued...
Continued...
Question 6 continued...
The resulting figure would look something like this:
var PenIsDown : boolean % true means pen is down, false means up var CurX : int % X,Y coordinate of the turtle var CurY : int const EndOfWorldX := 20 const EndOfWorldY := 20 var World : array 1 .. EndOfWorldX, 1 .. EndOfWorldY of int var N,S,E,W : boolean % direction variables. Only one can be true. procedure PenUp % Your code here end PenUp procedure PenDown % Your code here end PenDown procedure RightTurn % Your code here end RightTurn procedure LeftTurn % Has been done, not shown here. end LeftTurn procedure Forward(Distance : int) % Your code here end Forward procedure PrintArray % Your code here end PrintArray function ParseCommands : int var Command : int var Distance : int put "Instruction? ".. get Command if Command = 1 then PenUp result 1 elsif Command = 2 then PenDown result 1 elsif Command = 3 then RightTurn result 1 elsif Command = 4 then LeftTurn result 1 elsif Command = 5 then get Distance Forward(Distance) result 1 elsif Command = 6 then PrintArray result 1 elsif Command = 9 then result 0 else put "Illegal instruction: ",Command result -1 end if end ParseCommands % Main program CurX := EndOfWorldX div 2 % start in the middle CurY := EndOfWorldY div 2 PenIsDown := false % start with a blank world for row : 1..EndOfWorldX for col : 1..EndOfWorldY World(row,col) := 0 end for end for N := true S := false E := false W := false loop var ret_code : int ret_code := ParseCommands if ret_code = 0 then exit end if end loop
Continued...
Continued...