CmSc150 Fundamentals of Computing I

Homework 03 due 09/17 name_________________

This assignment consists of 2 parts – problem solving and programming. Download this document, save it as your_nameHW03.doc. Solve the problems in Part 1, implement the two programs in Part 2, create a folder named your_nameHW03, copy (or move) there the three files – the WORD file, and the two Python programs, zip the folder and upload in Scholar.

Part 1: Problems

1. Write the if-statements(s) to perform the following:

If x is even and y is odd then print their product, otherwise if x is odd print the sum of x and y, and assign x to y, i.e. store the value of x into the variable y .

2. Find the syntax and logical errors in the following statements and correct them:

a. The intended action is: increment x if a is less than 3

if a < 3 x = x+1

b. The intended action is: increment a and b if a is neither 3 nor 4, otherwise decrement a and b

if (a != 3 or 4)

a = a+1

b = b+1

else:

a = a-1

b = b-1

3. Solve this problem first on paper and then check your solutions in Python.

What would be output of the following statements:

if x < 6 or y > 4:

print(“A”)

if x >= 8 and y < 6:

print(“B”)

elif x < 10:

print(“C”)

else:

print(“D”)

if the following assignments are in effect:

a. x = 4, y = 7

b. x = 9, y = 3

c. x = 9, y = 10

d. x = 10, y = 6

Part 2: Programming

You have to implement two programs – salary.py and scores.py.

General guidelines for the two programs:

· The first line in the program has to be a comment identifying you and the assignment, e.g. # John Smith, cmsc 150, HW03

· Next you have to write several lines as comments, describing what the program does, based on the text of the problem.

· Start the executable code with a welcome message saying briefly what the program does

· Write your name in front of the program name e.g. John_salary.py

Your work will be evaluated with respect to:

· A running program, that meets the specified functional requirements (the dialogue should look the way it is specified )

· Quality of the comments in the source code: a comment should be relevant to the piece of code it stands next to. It also should be meaningful. Since the code of these programs is quite obvious, the most important comments are the comments describing what the programs do, written at the top of the program.

Problem 1:

A company is giving raises to all its employees. Those who earn less than $1000 a month get raise of $100 a month; those with salaries greater than or equal to $1000 but less than $3000 a month receive raise of $200 a month; and those with salaries higher than $3000 get raise of $300 a month. Read a person's current monthly salary and print the new salary. Implement in the program using a while loop in order to be able to run multiple times.

A sample run of the program should be:

(user input is underlined)

This program computes salary raise

Enter current monthly salary: $5300

New monthly salary: $5600

Another run? (y/n) y

Enter current monthly salary: $1200

New monthly salary: $1400

Another run? (y/n) y

Enter current monthly salary: $800

New monthly salary: $900

Another run? (y/n) n

Press ENTER to exit the program

Guidelines:

Implement the program using only two variables:

run – to control the while loop

salary – to get the current salary and to hold the updated salary

Don't forget to initialize properly the loop variable before the loop starts, and to update it inside the loop body.

Make sure the results are printed in the form given in the example above.

Problem 2:

Read three integers representing scores in variables first, second and third, and arrange them so that the largest score is in variable first, the next largest one is in variable second, and the smallest is in variable third.

Print the values of the variables first, second and third. Allow the user to run the program multiple times without exiting the program, using a while loop.

There are 6 possible orderings of 3 numbers, test your program with all possible orderings. At the end of the program the tests that you have used in comments, e.g.

#Test 1: 15, 68, 23

#Test 2: …

# …

Sample program run:

(user input is underlined)

This program orders three integers representing scores in descending order

Enter first score: 34

Enter second score: 12

Enter third score: 60

Scores ordered:

first: 60

second: 34

third: 12

Another run (y/n)? y

Enter first score: 14

Enter second score: 22

Enter third score: 15

Scores ordered:

first: 22

second: 15

third: 14

Another run (y/n)? n

Press ENTER to exit the program

Guidelines:

For this program you will need to recall how the content of two variables can be swapped using a third variable.