Lab14_Tasks

advertisement
Lab 14
Due Wednesday, 4:00 AM
Task 1
Prompt the user for a number. Determine, and print out if that number is a prime
number. (Note, MATLAB has a built-in function for checking if a number is prime,
but for this assignment, you are not allowed to use it).
Sample Output:
Enter a number: 141
141 is not a prime number
Enter a number: 139
139 is a prime number
Task 2
Prompt the user for how many prime numbers they would like printed out. Modify
your program to print out that many prime numbers, starting with 2.
How many prime numbers would you like found: 10
1: 2
2: 3
3: 5
4: 7
5: 11
6: 13
7: 17
8: 19
9: 23
10: 29
Task 3
Create a MATLAB script that does the following items:
 Creates a row vector in a variable named row_space containing the numbers
from 1 to 5 using spaces
 Creates a row vector in a variable named row_comma containing the
numbers from 1 to 5 using commas
 Creates a column vector in a variable named col_semi containing the
numbers from 1 to 5 using semicolons
 Creates a row vector in a variable named col_trans containing the numbers
from 1 to 5 using the transpose command
 Creates a 2x3 matrix in a variable named matrix23 that looks like the
following:
1 2 3
4 5 6
(Note, this program is pretty meaningless, but you should learn to create arrays.)
Task 4
Zeller’s algorithm computes the day of the week on which a given date will fall (or
fell). In this task, you will write a MATLAB script to run Zeller’s algorithm. The
program should use the algorithm outlined below to compute the day of the week
for a given date and print the result to the screen.
Prompt the user for the month, day, and 4-digit year. Like all programs, you should
check if the data being entered is valid (including making sure that the DATE is a
valid date – including handling leap years, described on the next page). If the user
enters invalid data, you should print an appropriate error message and stop. If the
user enters valid data, you should calculate and print out the original date and the
corresponding day of the week.
Zeller’s algorithm is defined as follows:
Let A, B, C, D denote integers that have the following values:
A = For January & February: the month of the year + 10, and you must also
subtract 1 from C below. For March – December: the month of the year – 2,
and there is no need to adjust C.
B = the day of the month (1, 2, 3, . . . , 30, 31)
C = the year of the century (e.g. C = 89 for the year 1989) (Note, don’t forget the -1
for January & February from A above.)
D = the century (e.g. D = 19 for the year 1989)
Let W, X, Y, Z, R also denote integers. Compute their values in the following order
using integer arithmetic:
𝑊=⌊
13𝐴 − 1
⌋
5
𝐶
𝑋=⌊ ⌋
4
𝐷
𝑍 = 𝑊 + 𝑋 + 𝑌 + 𝐵 + 𝐶 − 2𝐷
𝑌=⌊ ⌋
4
R = the remainder when Z is divided by 7
𝑍
(Even if you know mod, this should be calculated as 𝑅 = 𝑍 − 7 ⌊7⌋)
The value of R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6
is Saturday.
Leap Year:
There are 29 days in February during a leap year, otherwise there are 28 days. A
leap year occurs when the following is true:
If it is not a centennial year (i.e., 1900, 2000, 2100, …), it is a leap year when the 4
digit year can evenly be divided by 4.
If it IS a centennial year (i.e., 1900, 2000, 2100, … ), it is a leap year only if the
remainder after (century / 900) is either 200 or 600. For example, in the year 2000,
2000 / 900 equals 2 remainder 200, making the year 2000 a leap year. The year
1900 is 2, remainder 100, making 1900 not a leap year.
Some examples/test cases:
1/1/2012 - Sunday
2/29/2012 - Wednesday
9/25/2012 - Tuesday
2/30/2012 - Invalid Date
4/30/2013 - Tuesday
2/31/2012 - Invalid Date
4/31/2013 - Invalid Date 3/1/2012 - Thursday
2/28/2013 - Thursday
2/29/2013 - Invalid Date
2/30/2013 - Invalid Date
3/1/2013 - Friday
Download