CPS 1 (Ramm) Page 1 of 5 22 February Spring Semester, 1995 Section _____ CLOSED BOOK Hour Test NAME_____________________ A. The following Pascal program has had most of its punctuation (5) marks removed. Restore all of these in their proper position. A _____ program greetings (5) var B _____ i, n: integer begin (10) writeln( Enter starting count ) C _____ readln( n ) i := n (15) while i > 0 do D _____ begin writeln( i ) (5) i := i - 1 E _____ end while i < n do (10) begin F _____ i := i + 1 writeln( i ) (5) end G _____ end (5) B. In the program above, circle all of the names and data fields H _____ that are the choice of the programmer. In other words, circle those words that are not part of the Pascal language. (10) I _____ (5) C. The following Pascal program was run. The user typed the J _____ following string as input: ABCQXYZ Show exactly what output the program produced. (Remember that (10) for these purposes, upper and lower case characters are different.) K _____ program tm1; OUTPUT(show next to writeln's) (10) var ---------------------------- L _____ stra, strb, strc: string; i, lng: integer; (15) begin M _____ readln(stra); --------- strb := copy(stra, 3, 2); writeln(strb); strc := strb + ' << ' + stra; writeln(strc); i := pos('X', stra); lng := length(stra); writeln(lng, ' ', i); strc := copy(stra, i+1, lng - i) + copy(stra, 1, i - 1); writeln(strc); writeln('stra + - + stra'); writeln(stra + '-' + stra); readln; end. Hour Test Page 2 of 5 D. Here are some simple rules defining Pascal objects. (Use the back of page 1 for your answer) #1 -> //a sequence of letters// (where // indicates italics) #2 -> '//any string of printable characters//' #3 -> readln() #4 -> writeln() #5 -> begin //a sequence of s each followed by a semicolon// end Show how these rules can be used to generate the following Pascal program fragment. Identify the rule used at each stage. begin writeln('Please enter number.'); readln(n); writeln('Thanks for that number.'); end Start with: #5 -> E. Trace the following program with four columns: one for the statement number, one for i, one for T/F (for the while statement), and one for output. The first four are done. Quit after 7 additional steps have been traced. {#} program again; stmt# i T/F output var 1 64 ? i: integer; 2 str: string; 3 T begin 4 || 64|| {1} i := 64; {2} str := '||'; {3} while i <> 0 do begin {4} writeln(str, i:4, str); {5} i := -1 * i div 2; end; end. F. On the back of the next page, write a complete Pascal program that reads in a positive integer N, and then prints a table of the integers from N+6 down to the smallest number greater than 0, by threes. Also prints their corresponding squares. For example, if your program reads in a 5, it should print out the following: 11 121 8 64 5 25 2 4 Hour Test Page 3 of 5 G. Below is the first part of a Pascal program. Assume that it reads in the numbers shown to the right of the program. Show the contents of array t by writing the numbers in the box below representing t in memory. DATA program ara; | 30 type | 40 t10 = array[1..10] of integer; | 10 var | 20 t: t10; | 80 i, j, n, inc, num: integer; | 55 heat, cool, total: integer; | 75 begin | 20 n := 10; | 85 i := 0; | 25 while i < n do | begin | i := i + 1; writeln('Enter T:'); readln(t[i]); end; 1 2 3 4 5 6 7 8 9 10 ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- t | | | | | | | | | | | ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- H. Assume that the program above continues with the code that follows. What output does it produce? inc := 2; i := 5; while i <= n do begin writeln(t[i]:5); i := i + inc; end; I. Next, the program concludes with the following code. What is the output? i := 0; cutoff := 70; total := 0; cool := 0; num := 0; while i < n do begin i := i + 1; if t[i] > cutoff then begin cool := cool + t[i]; num := num + 1; end; total := total + t[i]; end; writeln('sum =', total:6); writeln('cooling load of', cool:5, ' over', num:4, 'days'); readln; end. Hour Test Page 4 of 5 J. What OUTPUT will the following program print? NOTE: FORMATTING IS IMPORTANT OUTPUT --------------------------------- program one; var i: integer; c: string; begin i := 1; c := 'XX'; while i <= 3 do begin c := c + '*' + c; i := i + 1; writeln(i, ' ', c); end; writeln(i); end. K. What output does the following program produce? program multistr; var a, b: string; procedure putlines(var line:string); var i:integer; begin i := 1; while i <= 3 do begin writeln(copy(line,1, 7 - 2*i)); i := i + 1; end; end; begin a := '*****'; putlines(a); writeln('Wow!'); b := '+++++' putlines(b); readln; end. Hour Test Page 5 of 5 L. Irrational numbers are numbers which have an infinite number of digits after the decimal point with no repeating pattern. Pi is such a number. It starts with 3.14159265... but goes on forever. One third ( 0.333333... ) goes on for an infinite number of places but does not qualify because it has a repeating pattern. Prove that there are an uncountable infinity of irrational numbers by using arguments that are similar to those used in the text about functions of integers to integers. Assume we have a list claiming to include all of them, one after the other, as suggested below. (Note that Pi happens to be the first one, using only digits after the decimal point.) Prove that such an infinite list could not include all irrational numbers. col 1 col 2 col 3 col 4 ... infinity 1st # (Pi) 1 4 1 5 ... ? 2nd # 3 7 4 7 ... ? 3rd # 4 8 3 6 ... ? 4th # 7 3 0 6 ... ? ... ... ... ... ... ... ... infinity ? ? ? ? ... ? M. On the back of the previous page, write a complete Pascal program that prints out a table of integers, their negatives, their double, and their cubes. The number of rows in the table should be controlled by a number read in from the user of the program. Make sure the format is neat and that the user is prompted to enter the numbers setting the length of the table. The program should print a heading and "ruled lines" for the table. For example, if the user entered 4, the program would print out: N -N 2*N N*N*N =============================== | 1 -1 2 1 | |-----------------------------| | 2 -2 4 8 | |-----------------------------| | 3 -3 6 27 | |-----------------------------| | 4 -4 8 64 | |-----------------------------| ----------------------------------------------------------------------------- HONOR CODE I have not received or given any improper help on this exam. (your signature) -----------------------------------------------------------------------------