Sale!

CSC 1015F Assignment 2: Control (if, while) solved

Original price was: $35.00.Current price is: $30.00. $25.50

Category:

Description

5/5 - (9 votes)

Assignment Instructions
This assignment involves constructing Python programs that use input and output statements, ‘if’
and ‘if-else’ control flow statements, ‘while’ statements, and statements that perform
numerical manipulation.
Question 1 [30 marks]
We’ve all been there: You dropped your cupcake on the ground. Did it land icing up, or down? Can
you just scrape off the icing? How many hours have you lost trying to decide? The following
flowchart can be used to determine whether or not dropped food can be eaten:
On the Vula page for this assignment you will find a program called ‘cupcake.py’. The program is
supposed to implement the flowchart, asking a series of questions to determine if you should eat
the food or not.
CONTINUED
Sample I/O:
Welcome to the 30 Second Rule Expert
————————————
Answer the following questions by selecting from among the options.
Did anyone see you? (yes/no)
yes
Was it a boss/lover/parent? (yes/no)
yes
Was it expensive? (yes/no)
yes
Can you cut off the part that touched the floor? (yes/no)
no
Decision: Your call.
This type of program is a simple variant of artificial intelligence known as an expert system and the
flowchart is known as a decision tree.
Unfortunately, though the program consists of suitable statements, they are (i) in the wrong order,
and (ii) not correctly indented.
Download the program and organise the statements so that it operates correctly.
Question 2 [35 marks]
Write a program called ‘firstday.py’ that asks the user to enter a year range and that prints out
the name of the day on which the 1st of January falls for each year in that range.
Sample IO:
Enter the first year:
2015
Enter the second year:
2020
The 1st of January 2015 falls on a Thursday.
The 1st of January 2016 falls on a Friday.
The 1st of January 2017 falls on a Sunday.
The 1st of January 2018 falls on a Monday.
The 1st of January 2019 falls on a Tuesday.
The 1st of January 2020 falls on a Wednesday.
Algorithm:
Given a 4 digit number representing a year, the day on which the 1st of January falls can be
calculated using the following formula (Gaus’s formula):
𝑑𝑎𝑦 = 𝑅(1 + 5𝑅(𝑌𝑒𝑎𝑟 − 1, 4) + 4𝑅(𝑌𝑒𝑎𝑟 − 1, 100) + 6𝑅(𝑌𝑒𝑎𝑟 − 1, 400), 7)
Where 𝑅(𝑛, 𝑚) is the remainder after dividing 𝑛 by 𝑚 e.g. 𝑅(10, 3) is 1.
The formula produces a number in the range 0. .6, where Sunday is ‘=0’, Monday is ‘1’, and so on.
CONTINUED
HINT: You might find it easier to accumulate the result by calculating the formula bit by bit. Let’s say,
for example, we wanted to calculate ‘𝑏 = 2𝑎
2 + 3𝑎 + 10’, we could write:
a=eval(input(‘Enter a value for a: ‘))
b=2*a*a
b=b+3*a
b=b+10
print(‘The value of b is: ‘,b)
Question 3 [35 marks]
Write a program called ‘pi.py’ that calculates the value of PI and then computes and displays the
area of a circle with radius entered by the user. PI must be approximated using the following
formula. Note that this formula has an infinite number of terms with increasing complexity, so you
must multiply additional terms until the size of the next term is 1!
Hint: This problem requires the use of a ‘while’ loop to accumulate each term. Also, use the
round function to display the computed values with 3 decimal places e.g. round(5.23517, 3)
is 5.235.
Sample I/O:
Approximation of pi: 3.142
Enter the radius:
2.5
Area: 19.635
Submission
Create and submit a Zip file called ‘ABCXYZ123.zip’ (where ABCXYZ123 is YOUR student number)
containing cupcake.py, firstday.py and pi.py.
END