You are on page 1of 15

CLB 10502 Jan 2011

Answer

Tutorial Chapter 1: Introduction to Programming


Exercises 1.1 1. Define the following terms: a) Computer program a self-contained set of instructions and data used to operate a computer to produce a specific result b) Programming the process of developing and writing a program c) Programming language the set of data and instructions that can be used to construct a program d) High-level language languages in which the instructions resemble human languages, such as English, and can be run on a variety of computer types e) Low-level language language that uses instructions that are directly tied to one type of computer f) Machine language a programming language consisting of the binary codes the can be executed by a computer g) Assembly language a programming language that uses symbolic names for operations and memory addresses h) Source program program written in a computer language i) Compiler program which translates high-level languages as a complete unit before any individual statement is executed j) Interpreter a program that converts, or translates, individual source program statements, one at a time, into executable statements which are executed immediately after translation

2.

a)

b)

Describe the difference between high- and low-level languages. A high-level language can be translated to run on a variety of computer types, while a low-level language is directly tied to one type of computer Describe the difference between procedure- and object- oriented languages. Procedure-oriented languages create logically consistent sets of instructions, or procedures, to produce a specific result while object-oriented languages create and manipulate objects to produce specific results.

3. Describe the similarities and differences between assemblers, interpreters, and compilers. The similarity is that they are a program that translates or converts certain computer language into machine language. The differences between assemblers, interpreters, and compilers is how they do the translation: i) Compiler - translates high-level languages as a complete unit before any individual statement is executed ii) Interpreter translates individual source program statements, one at a time, into executable statements which are executed immediately after translation
Prepared by: Miss Azlina Din Page 1

CLB 10502 Jan 2011 4. a) Given the following operation codes, 11000000 10100000 11110000 11010000 means means means means add the 1st operand to the 2nd operand subtract the 1st operand from the 2nd operand multiply the 2nd operand by the 1st operand divide the 2nd operand by the 1st operand

Answer

translate the following instructions into English: opcode 11000000 10100000 11110000 11010000 Address of 1st operand 000000000001 000000000010 000000000100 000000000101 Address of 2nd operand 000000000010 000000000011 000000000011 000000000011

This is assuming the following: Address Location 00000000001 1 00000000010 2 00000000011 3 00000000100 4 00000000101 5 Add the data in memory location 1 to the data in memory location 2. Subtract the data in memory location 2 from the data in memory location 3. Multiply the data in memory location 3 by the data in memory location 4. Divide the data in memory location 3 by the data in memory location 5.

b) Assuming the following locations contain the given data, determine the result produced by the instructions listed in exercises 4.a). For this exercise, assume each instruction is executed independently of any instruction. Address 000000000001 000000000010 000000000011 000000000100 000000000101 Initial value (in decimal) Stored at this address 5 3 6 14 4

5 6 6 6

+ * /

3 = 8 3 = 3 14 = 84 4 = 1

<-- consider integer operation

5. Rewrite the machine-level instructions listed in exercise 4.a) using Assembly-language notation. Use symbolic names ADD, SUB, MUL, and DIV for addition, subtraction, multiplication, and division operations, respectively. In writing the instructions use decimal values for the addresses.

ADD SUB MUL DIV

1, 2, 4, 5,

2 3 3 3
Page 2

Prepared by: Miss Azlina Din

CLB 10502 Jan 2011 Exercises 1.2

Answer

1. Determine a step-by-step procedure (list the steps) to do the following tasks. (Note: There is no one single correct answer for each of these tasks. The exercise is designed is to give you practice in converting heuristic-type commands into equivalent algorithms and making the shift between the thought processes involved in the two types of thinking.) a) Fix a flat tire Make sure the car is parked, the engine if off, and the key is out of the ignition switch. Go to the trunk. Put the correct key into the keyhole in the trunk and turn. Open the trunk. Remove the spare tire and the jack. Put the jack under the carand so on.

b) Make a telephone call Go to a phone. Remove that handset from the phone. Wait for the dial tone. Take out the correct change for the call. Put the correct change into the phone. Dial the number.

c) Log in to a computer Turn on the computer. When the computer boots, enter your username. Enter your password. Either press the ENTER key or click OK (depends on the system you are using).

d) Roast a turkey Prepare the turkey. Preheat the oven. Open the oven door. Put the turkey in the oven. Close the oven door. Wait the appropriate amount of time for the turkey to cook.

2. Determine and write an algorithm to interchange the contents of two cups of liquid. Assume that a third cup is available to hold the contents of either cup temporarily. Each cup should be rinsed before any new liquid is poured into it. Step Step Step Step Step 1: 2: 3: 4: 5: Pour the contents of the first cup into the third cup. Rinse out the first cup. Pour the contents of the second up into the first cup. Rinse out the second cup. Pour the contents of the third cup into the second cup.

Prepared by: Miss Azlina Din

Page 3

CLB 10502 Jan 2011 Answer 3. Write a set of detailed, step-by-step instructions, in English, to find the smallest number in a group of three integer numbers. Input: 3 values (A, B, C ) Process: comparison ( smaller than ) Output: smallest values Algorithm : Begin Prompt Read A Prompt Read B Prompt Read C Pseudocode "Please enter one integer number: "Please enter one integer number: "Please enter one integer number:

If A smaller than B then If A smaller than C then Assign A to smallest Else Assign C to smallest Else if B smaller than C then Assign B to smallest Else Assign C to smallest Display smallest value End 4. Write a set of detailed, step-by-step instructions, in English, to calculate the fewest number of dollar bills needed to pay a bill of amount TOTAL. For example, if TOTAL were $97 the bills would consist of one $50 bill, two $20 bills, one $5 bill and two $1 bills. (For this exercise, assume that only $100, $50, $20, $10, $5, and $1 bills are available.) Input: TOTAL value of purchase Process: addition, division ( Divide and modulus ) Output: fewest noOfDollar bill Algorithm : Pseudocode Begin Prompt "Please enter your TOTAL purchase value: Read TOTAL Divide Divide Divide Divide Divide Divide Divide Divide Divide Divide TOTAL TOTAL TOTAL TOTAL TOTAL TOTAL TOTAL TOTAL TOTAL TOTAL value value value value value value value value value value by by by by by by by by by by value value value value value value value value value value 100 and assign the result to Dollar100 100 and assign the remainder to TOTAL 50 and assign the result to Dollar50 50 and assign the remainder to TOTAL 20 and assign the result to Dollar20 20 and assign the remainder to TOTAL 10 and assign the result to Dollar10 10 and assign the remainder to TOTAL 5 and assign the result to Dollar5 5 and assign the remainder to Dollar1

Add Dollar100 value to Dollar50 value to Dollar20 value to Dollar10 value to Dollar5 value to Dollar1 and assign the result to noOfDollar Display noOfDollar End
Prepared by: Miss Azlina Din Page 4

CLB 10502 Jan 2011

Answer

5. Determine and write an algorithm to sort four numbers into ascending (from lowest to highest) order. Input: 4 values (A, B, C, D ) Process: comparison ( greater than ) Output: ascending order of A, B, C, D Algorithm : Begin Prompt Read A Prompt Read B Prompt Read C Prompt Read D Pseudocode "Please enter one integer number: "Please enter one integer number: "Please enter one integer number: "Please enter one integer number:

While A greater than B or B greater than C or C greater than D do If A greater than B then Assign value A to X Assign value B to A Assign value X to B If B greater than Assign value Assign value Assign value C B C X then to X to B to C

If C greater than D then Assign value C to X Assign value D to C Assign value X to D End while Display A, B, C, D values End

Prepared by: Miss Azlina Din

Page 5

CLB 10502 Jan 2011

Answer

6. Determine and write an algorithm that prompts the user to enter two integer values. Store these values in int variables named val1 and val2. Write your algorithm to determine the smallest, largest, sum, difference, product, and ratio of these values and report them to the user. Input: 2 integer values ( val1 & val2 ) Process: arithmetic addition, subtraction, multiplication, division Logic smaller than Output: smallest value, largest value, sum value, difference value, product value, and ratio value Algorithm : Pseudocode Begin Prompt "Please enter one integer values: " Read val1 Prompt "Please enter one integer values: " Read val2 If val1 value is smaller than val2 value then Display val1 value together with word " is smallest " Display val2 value together with word " is biggest " Else if val2 value is smaller than val1 value then Display val2 value together with word " is smallest " Display val1 value together with word " is biggest " Else Display val1 value, word " and ", val2 value and word " are equal" Add val1 value to val2 value and assign the result to sum Display word "Sum : " and sum value Multiply val1 value by val2 value and assign the result to product Display word "Product : " and product value If val2 equal to zero Display message cannot divide by zero Else Divide val1 value by val2 value and assign the result to ratio Display word "ratio (val1/val2): " and ratio value Subtract val2 value from val1 value and assign the result to difference Display word "Difference (val1 val2) : " and difference value End

Prepared by: Miss Azlina Din

Page 6

CLB 10502 Jan 2011

Answer

7. Determine and write an algorithm to test an integer value to determine if it is odd or even. As always, make sure your output is clear and complete. In other words, don't just output "yes" or "no". Your output should stand alone, like The value 4 is an even number.. Input: integer number ( num ) Process: division ( modulus ), comparison ( equal to ) Output: integer number together with word even or odd Algorithm : Pseudocode Begin Prompt "Please enter one positive integer number: Read num Divide value num by value 2 and assign the remainder to divResult If divResult equal to zero then Display num value together with word is even number. Else Display num value together with word is odd number. End

8. Determine and write an algorithm that converts spelled out numbers such as "zero" and "two" into digits, such as 0 and 2. When the user inputs a number print out the corresponding digit. Do it for the values 0,1,2,3, and 4 and write out "not a number I know" if the user enters something that doesn't correspond, such as ``stupid computer!''. Input: a string of spelled out number ( spellNum ) Process: comparison ( equal to ) Output: value in digit Begin Prompt "Please enter an integer as a text string: " Read spellNum if spellNum equal to zero" then Display spellNum value and word " has the value 0" else if spellNum equal to "one" then Display spellNum value and word " has the value 1"; else if spellNum equal to "two" then Display spellNum value and word " has the value 2"; else if spellNum equal to "three" then Display spellNum value and word " has the value 3"; else if spellNum equal to "four" then Display spellNum value and word " has the value 4"; else Display spellNum value and word " not a number I know" End

Prepared by: Miss Azlina Din

Page 7

CLB 10502 Jan 2011

Answer

9. Determine and write an algorithm that calculate discount prices during a special sale at a store, where a 10% discount is taken on purchases over RM10.00. Write an algorithm that asks for the amount of purchases, then calculates the discounted price. The purchase amount will be input in cents (as an integer): Enter amount of purchases: 2000 Discounted price: 1800 Use integer arithmetic throughout the program. Input: amount of purchase ( amount ) Process: comparison ( greater than ), multiplication, subtraction Output: discounted price ( amount ) Algorithm : Pseudocode Begin Prompt "Please enter your amount of purchase (in cent): Read amount If amount greater than 1000 then discount <- amount x 10 / 100 amount <- amount - discount Display amount End

10. Determine and write an algorithm that reads in a number of cents. The algorithm will write out the number of RM and cents, like this: Input the cents: 324 That is 3 ringgit and 24 cents.

Input: number of cent ( numOfCent ) Process: division ( Divide and modulus ) Output: RM and cent value Algorithm : Pseudocode Begin Prompt "Input the cents: Read numOfCent Divide numOfCent value by value 100 and assign the result to RM Divide numOfCent value by value 100 and assign the remainder to cent Display RM and cent value End

Prepared by: Miss Azlina Din

Page 8

CLB 10502 Jan 2011

Answer

11. Determine and write an algorithm that performs as a very simple calculator. Your calculator should be able to handle the four basic math operations: add, subtract, multiply, and divide on two input values. Your program should prompt the user to enter three arguments: two double values and a character to represent an operation. If the entry arguments are 35.6, 24.1, and +; the program output should be "The sum of 35.6 and 24.1 is 59.7." Input: two double value and one character ( num1, num2 , operator ) Process: addition, multiplication, division, subtraction, comparison (equal to) Output: result based on operator Algorithm : Pseudocode Begin Prompt "Enter 2 double value and one character to represent operation separated by single space[ eg: 35.6 24.1 + ]: Read num1, num2, operator If operator equal to + symbol then Add num1 value to num2 value and assign the result to sum Display sum value together with num1 and num2 value Else if operator equal to * symbol then Multiply num1 value by num2 value and assign the result to product Display product value together with num1 and num2 value Else if operator equal to - symbol then Subtract num2 value from num1 value and assign the result to difference Display difference value together with num1 and num2 value Else if operator equal to / symbol then If num2 equal to zero Display message cannot divide by zero Else Divide num1 value by num2 value and assign the result to divVal Display divVal value together with num1 and num2 value Else Display invalid input End

Prepared by: Miss Azlina Din

Page 9

CLB 10502 Jan 2011

Answer

12. Determine and write an algorithm that that asks a user for their birth year encoded as two digits (like "62") and for the current year, also encoded as two digits (like "99"). The algorithm is to correctly write out the users age in years. Year of Birth: 62 Current year: 99 Your age: 37 ----- another run of the program -------Year of Birth: 62 Current year: 00 Your age: 38 Input: year of birth and current year in two digit representation ( birthYear , currentyear ) Process: addition, subtraction, comparison (less than or equal, greater than or equal, greater than) Output: age value Algorithm : Pseudocode Begin Prompt "Year of Birth: " Read birthYear Prompt "Current year: " Read currentYear If currentYear greater or equal to 00 and currentYear less or equal to 10 then If birthYear greater than 10 Subtract birthYear from 99 and then add the result to currentYear and then add one to the result. Finally, assign the result to age Else Subtract birthYear from currentYear and assign the result to age Else Subtract birthYear from currentYear and assign the result to age Display age End

Prepared by: Miss Azlina Din

Page 10

CLB 10502 Jan 2011

Answer

13. Determine and write an algorithm that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values. Enter Start: 5 Enter End: 9 5 6 7 8 9 Input: start value and end value ( startVal, endVal ) Process: addition Output: integer numbers from the start value until the end value Algorithm : Pseudocode Begin Prompt "Enter Start: " Read startVal Prompt "Enter End: " Read endVal While startVal less than or equal to endVal do Display startVal Increase startVal by one End while End

Prepared by: Miss Azlina Din

Page 11

CLB 10502 Jan 2011

Answer

14. Determine and write an algorithm that adds up integers that the user enters. First the algorithm asks how many numbers will be added up. Then the algorithm prompts the user for each number. Finally it prints the sum. How many integers will be added? 4 Enter an integer: 3 Enter an integer: 5 Enter an integer: -4 Enter an integer: -3 The sum is 1

Input: total numbers to be add and integer numbers ( totalNum, num ) Process: addition Output: sum value Algorithm : Pseudocode Begin Initialize counter to zero Initialize sum to zero Prompt "How many integers will be added? " Read totalNum While counter less than totalNum do Prompt "Enter an integer: " Read num Add num to sum and assign the result to sum Increase counter by one End while Display sum value End

Prepared by: Miss Azlina Din

Page 12

CLB 10502 Jan 2011

Answer

15. Determine and write an algorithm that computes XN where X is a floating point number and N is a positive integer. The program will inform the user that N must be positive if the user enters a negative value. Of course, XN = X * X * X * ... * X -------------------N times The user dialog will look something like this: Enter X 1.3 Enter N 5 1.3 raised to the power 5 is: ------Enter X 5.6 Enter N -3 N must be a positive integer. Input: one integer and one double value ( X, N ) Process: addition, multiplication and comparison ( greater than ) Output: product value Algorithm : Pseudocode Begin Initialize counter to zero Initialize product to one Prompt Enter X (double value) : Read X Prompt Enter N (integer value) : Read N if N greater than zero then while counter less than N do Multiply product by X value and assign the result to product Increase counter by one End while Display value X, N, and product Else Display message "N must be a positive integer." End 3.71293

Prepared by: Miss Azlina Din

Page 13

CLB 10502 Jan 2011

Answer

16. Write a set of detailed, step-by-step instructions, in English, to settle the following problem. If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This is essentially the definition of the / and % operators for integers.) Write a program that asks the user how many eggs she has and then tells the user how many dozen eggs she has and how many extra eggs are left over. A gross of eggs is equal to 144 eggs. Extend your program so that it will tell the user how many gross, how many dozen, and how many left over eggs she has. For example, if the user says that she has 1342 eggs, then your program would respond with Your number of eggs is 9 gross, 3 dozen, and 10 since 1342 is equal to 9*144 + 3*12 + 10.

Input: number of eggs ( totalEgg ) Process: division ( divide and modulus ) Output: number of gross, number of dozen and number of extra eggs Algorithm : Pseudocode Begin Prompt How many eggs do you have? Read totalEgg Divide Divide Divide Divide totalEgg totalEgg totalEgg totalEgg by by by by value value value value 144 and assign the result to gross 144 and assign the remainder to totalEgg 12 and assign the result to dozen 12 and assign the remainder to extra

Display gross value, dozen value and extra value End

Prepared by: Miss Azlina Din

Page 14

CLB 10502 Jan 2011 17. Develop a solution for the following problem: Given a classroom full of people: * * * * find the average height the number of people the height of the tallest person the height of the smallest person

Answer

All measurements should be in meters.

Input: number of people (integer value), height of persons (double value) Process: division, addition, comparison (greater than and smaller than) Output: smallest height, tallest height, number of people, average height Algorithm : Pseudocode Begin Initialize counter to one Initialize sum to zero Prompt Enter number of people Read numOfPeople Prompt Enter height in meter: Read height Assign height to smallest Assign height to tallest While counter less than numOfPeople do Prompt Enter height in meter: Read height Add height to sum and assign the result to sum Increase counter by one If height less than smallest then Assign height to smallest If height greater than tallest then Assign height to tallest End while Divide sum by numOfPeople and assign the result to average Display numOfPeople value, average value, tallest value and smallest value

End

Prepared by: Miss Azlina Din

Page 15

You might also like