COMP 1012


COMP 1012 – Computer Programming for Scientists and Engineers
Assignment 4 – Due Monday August 2, 2019 at 4:30 pm
Each program must start with a multi-line comment as shown below. For
Assignment 4 question 1 replace x with 4 and y with 1. Replace each occurrence of
DentStew and Stew Dent with your name. Replace yyyy/mm/dd with the date you
completed the program.
# DentStewAxQy.py
#
# Course: COMP 1012
# Instructor: Amir Hosseinmemar/ Ramin Soltanzadeh
# Assignement: x Question y
# Author: Stew Dent
# Version: yyyy/mm/dd
#
# Purpose: The purpose of the program.
Begin each program with the following statement.
from time import ctime
The name of the file for each program should be of the form:
LastnameFirstnameAnQm.py
If your name is Stew Dent and the program is for assignment 4 question 1 then the
name of the file for the program should be:
DentStewA4Q1.py
Submit your solution to the allocated dropbox on the course website. No email
submission will be accepted, zero will be granted otherwise as the mark for
the assignment.
Do NOT use zip, rar or any other packaging of your files!
Page 1 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
Question 1
The purpose of this question is to write a complete Python program that represents

代做COMP 1012作业、代做Computer Programming作业、代写Python程序作业
some trash cans using a two dimensional array. Each column of the array represents
a trash can. Each row of the array represents bags of trash in the cans. Each trash
can holds the same number of bags of trash. When one can is filled with bags of
trash a message is displayed and the program terminates.
Write a function that begins with the following header:
def getPosInt(prompt):
Call this function to get a positive integer from the user. Valid input is an int greater
than zero. The function displays the prompt to tell the user what to enter. If the user
does not enter valid input display the appropriate error message as described below
and display the prompt again asking for more input from the user. Repeat this until
the user enters valid input. When the user enters valid input return that input.
Error situations:
If the user presses enter/return without entering anything display the
message 'Missing input!'
If the input causes an exception when passed to eval display the value of the
input and the message 'is not valid!'
If the input is not an int display the value of the input and the message 'is not
an integer!'
If the input is not greater than 0 display the value of the input and the
message 'is not greater than 0!'
Write a function that begins with the following header:
def getBoundedInt(prompt, lowerBound, upperBound):
Call this function to get an integer between lowerBound and upperBound from the
user. Valid input is an int between lowerBound and upperBound inclusive. The
function displays the prompt to tell the user what to enter. If the user does not enter
valid input display the appropriate error message as described below and display
the prompt again asking for more input from the user. Repeat this until the user
enters valid input. When the user enters valid input return that input.
Error situations:
If the user presses enter/return without entering anything display the
message 'Missing input!'
If the input causes an exception when passed to eval display the value of the
input and the message 'is not valid!'
If the input is not an int display the value of the input and the message 'is not
an integer!'
Page 2 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
If the input is not between the lower bound and the upper bound display the
value of the input followed by ' is not between ' followed by the value of the
lower bound followed by ' and ' followed by the value of the upper bound
followed by '!'
Write a function that begins with the following header:
def addTrashToCan(trashCans, can, counters):
This function is given the array representing the trash cans, the index of the can to
which a bag is to be added and the array of counters of the number of bags in each
trash can. For example if there are 3 trash cans and counters is [1, 0, 2] then trash
can 0 contains 1 bag of trash, trash can 1 does not contain any bags of trash and
trash can 2 contains 2 bags of trash. In this example if can has the value 0, add a bag
of trash to trashCans where the column is the value of can and the row is the value of
counters[can]. The value to assign to this location is the new number of bags in the
trash can. Increment the counter for the number of bags in the trash can.
Return True if the trash can is full and False if the trash can is not full. A trash can is
full if its counter is equal to the number of rows in trashCans.
Write a function that begins with the following header:
def displayTrashCans(trashCans):
This function is given an array that represents the trash cans. Display the array
values row by row, starting with the last row. The output should be similar to the
following:
| | | | | |
| | | 3 | | |
| | | 2 | 2 | 2 |
| 1 | 1 | 1 | 1 | 1 |
--- --- --- --- ---
for 5 partially filled trash cans that can hold 4 bags of trash each. Trash can 0 is
shown on the left and trash can 4 is shown on the right.
Write a function that begins with the following header:
def main():
This function calls getPosInt twice, once to get the number of trash cans and once to
get the number of bags per trash can. Each trash can holds the same number of bags
as the other trash cans. Using this information create two arrays, one that
represents the trash cans where the number of rows is the number of bags per trash
can and the number of columns is the number of trash cans, and another array that
Page 3 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
holds the number of bags in each trash can. Initialize each element of these arrays to
zero. Zero represents an array element that does not hold a bag of trash.
Repeatedly request the number of a trash can to which a bag of trash is to be added
by calling getBoundedInt. Call addTrashToCan to add a bag of trash to the can. After
each bag of trash is added to a trash can display the contents of the trash cans by
calling displayTrashCans. If addTrashToCan returns True display a message
indicating which can is full and that the trash should be picked up for disposal, and
terminate the program.
Before it ends this function must display the termination message.
The main program should contain the function definitions, any imports needed by
the functions and a call to main.
For 5 trash cans that can each hold three bags of trash the output from the program
might be:
-----------------------------------------------------------
Enter the number of trash cans (> 0): 5
Enter the number of bags per trash can (> 0): 3
Enter a trash can number between 0 and 4: 1
| | | | | |
| | | | | |
| | 1 | | | |
--- --- --- --- ---
Enter a trash can number between 0 and 4: 2
| | | | | |
| | | | | |
| | 1 | 1 | | |
--- --- --- --- ---
Enter a trash can number between 0 and 4: 3
| | | | | |
| | | | | |
| | 1 | 1 | 1 | |
--- --- --- --- ---
Page 4 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
Enter a trash can number between 0 and 4: 1
| | | | | |
| | 2 | | | |
| | 1 | 1 | 1 | |
--- --- --- --- ---
Enter a trash can number between 0 and 4: 3
| | | | | |
| | 2 | | 2 | |
| | 1 | 1 | 1 | |
--- --- --- --- ---
Enter a trash can number between 0 and 4: 4
| | | | | |
| | 2 | | 2 | |
| | 1 | 1 | 1 | 1 |
--- --- --- --- ---
Enter a trash can number between 0 and 4: 1
Trash can 1 is full! Time to have the trash picked up.
| | 3 | | | |
| | 2 | | 2 | |
| | 1 | 1 | 1 | 1 |
--- --- --- --- ---
Programmed by Stew Dent.
Date: Mon Jul 15 09:45:01 2019.
End of processing.
Hand in your complete program.
Page 5 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
Question 2
The purpose of this question is to write a complete Python program that finds the
area of an ellipse using Simpson's Rule and the Monte Carlo technique.
Write a function that begins with the following header:
def getPosNumber(prompt):
Call this function to get a positive number from the user. Valid input is an int or a
float greater than zero. The function displays the prompt to tell the user what to
enter. If the user does not enter valid input display the appropriate error message as
described below and display the prompt again asking for more input from the user.
Repeat this until the user enters valid input. When the user enters valid input return
that input.
Error situations:
If the user presses enter/return without entering anything display the
message 'Missing input!'
If the input causes an exception when passed to eval display the value of the
input and the message 'is not valid!'
If the input is not an int or a float display the value of the input and the
message 'is not a number!'
If the input is not greater than 0 display the value of the input and the
message 'is not greater than 0!'
Write a function that begins with the following header:
def ellipse(x, a, b):
This function is given an array of x values x, the lengths of the major a and minor b
axes of an ellipse and returns an array of y values that lie on the boundary of the
ellipse. Given a value for x the corresponding value for y is computed as follows:
You must compute the array of y values from the x values using vector arithmetic.
You must not use any loops in this function.
Page 6 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
Write a function that begins with the following header:
def simpson(fx, a, b, major, minor, intervals):
This function is given the name of a function fx to call, the lower bound a for the x
values, the upper bound b for the x values, the length of the major and minor axes of
an ellipse and the number of intervals. Use Simpson's rule to find the area under the
curve for the function fx by calling fx with the arguments xValues, a and b. fx will
return an array of yValues. xValues is an array whose values are determined by
using the values of a, b and intervals as is h, which is the size of an interval. Return
the area under the curve. Simpson's rule follows, see the week 7 notes for more
information.
f(a)+f(b)+4 f(a+(2i-1)h)+2 f(a+2ih)
You must not use any loops in this function. All arithmetic operations performed on
the arrays must use vector arithmetic.
Write a function that begins with the following header:
def monteCarlo(fx, major, minor, points):
This function is given the name fx of a function to call, the length of the major and
minor axes of an ellipse and the number of points to create. Use the random function
from the numpy.random module to create the random points whose x values are
stored in one array and whose y values are stored in a second array. You also need
to compute an array of y values on the boundary of the ellipse for the array of x
values. Do this by calling fx with the arguments: the array of x values, the length of
the major axes and the length of the minor axes of the ellipse.
Return the area under the curve using the Monte Carlo technique discussed in class.
You must not use any loops in this function.
Write a function that begins with the following header:
def main():
This function calls getPosNumber three times, once to get the number of intervals /
points, once to get the length of the major axis of an ellipse and once to get the
length of the minor axis of the ellipse. It computes the actual area of the ellipse using
the formula: area = πab, where a is the length of the major axis and b is the length of
the minor axis of the ellipse. Call simpson and monteCarlo to compute ? of the area
of the ellipse. The output from the program should be as shown below.
Page 7 of 8
COMP 1012 – Computer Programming for Scientists and Engineers
The main program should contain the function definitions, any imports needed by
the functions and a call to main.
Sample output from the program:
--------------------------------------------
Enter the number of intervals / points: 10000000
Enter the length of the major axis: 5
Enter the length of the minor axis: 3
Area computed using Simpson's rule.
-----------------------------------
Area from x=0.00 to x=5.00 is 11.780972950906419
Approximate area of ellipse is 47.123891803625675
Actual area of the ellipse is 47.123889803846893
The error is 1.999779e-06
Area computed using the Monte Carlo technique.
----------------------------------------------
Area from x=0.00 to x=5.00 is 11.784292500000001
Approximate area of ellipse is 47.137170000000005
Actual area of the ellipse is 47.123889803846893
The error is 1.328020e-02
Programmed by Stew Dent.
Date: Mon Jul 15 10:28:47 2019.
End of processing.
Hand in your complete program.

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com

微信:codehelp

上一篇:Linux-运维-实现回收站功能


下一篇:linux – 如何禁用.Trash-1000文件夹的创建?