CSCI 150, Spring 2003
Home | | Course Schedule | | Assignments | | Lecture NotesThe following topics have been covered in the first part of the course. Each of the following topics may appear on the exam.
1. Introduction to Computer Science Imperative vs. Declarative knowledge Definition of an Algorithm Decision Trees 2. Pascal programs General form of Pascal program Input and Output (readln(), read(), writeln(), write()) Variables Variable types (integer, string, real, char) Rules for naming variables Assigning values to variables 4. Programming Decision Trees General form of conditional statement Using if ... then....else if ....then ....else Booleans Logical Expressions Relational operators (<, >, etc.) Logical operators (and, or, not) 5. Text manipulation Concatenating strings Finding the length of a string Copying a substring from a string 6. Arithmetic expressions Precedence rules for +, - , * , / 7. Loops while loops count-controlled loops for loops 8. Arrays Declaring an array Assigning values to an array Using for loops with arrays
Practice Problems
The following problems are intended to help you study for the exam.
1)Given these boolean variables: x = true, y = false, z = true What is the value (true/false) of each of the following? i) x and (y or z) ii) (not x) or (not(y and z)) iii) y or (x and not z) 2)Write a program that does the following: Prompt the user to input a number. Input the number from the keyboard If the number is less than 100, write: XX is a low number. where XX refers to the number inputted by the user. Otherwise, write: XX is a high number. 3) Compute the value of the following expressions: a) 6 / 3 * 2 b) (17 - 11) / 3 + 3 c) Add parentheses to part b so that the addition is performed before the division. d) What is the value of c) ?
4) a) What is the value of sum after the execution of the following code?
sum := 0; for counter := 1 to 4 do begin sum := sum + 2 * counter; end;
5) What is the output of the following code fragment (mySentence and partial are both strings)?
mySentence := 'It was a dark and stormy night'; partial := copy(mySentence, 8, 6); writeln(partial);6) What is wrong with the following fragments of Pascal?
program p1; var p2: integer begin p2 := 1; end.
program s1; var s2: string; begin s2 := '1'; end.
program r1; begin if true then writeln('Truth is beauty.'); end; end.
program t1; var circumference: real; radius: real; begin circumference := 3.14159265 radius * radius; end.
7) Consider the following while loop: sum := 0; count := 5; while count <= 10 do begin sum := sum + count; count := count + 1; end;
a) Fill in the following table for the values of count and sum each time through the loop (there may be more rows than you need in the table):
b)Write a for loop that does the equivalent of the above loop.
8. Consider the following pascal code:
program z3; var sum,i: integer; begin sum := 0; for i :=1 to 4 do begin sum := sum + i; end; writeln(sum); end.What is value of sum printed on the screen?
9. Fill in/write the necessary code for the following decision tree.
program catCheck (input, output); var answer : string; begin {Your code goes here} end. {end of main program catCheck}
10. Fill in the necessary code to fill in the depositList array with deposit amounts entered by the user. You should keep track of the sum of all the deposits made, so that the print statement at the end of the program makes sense. You can assume there are exactly 1000 deposits made.
program deposit; type realArray : array [1..1000] of real; var loop : integer; sum : real; depositList : realArray begin sum := 0; writeln ('The sum of the deposits: $', sum:5:2); end;