In: Computer Science
Homework – Zeller’s Algorithm Pt2
Zeller’s algorithm computes the day of the week on which a given date will fall (or fell). You are provided this algorithm in CANVAS. In this task, you must transform the provided solution to a “Defined Function”
Function Name
zelleralg
Input Parameters |
• month • day |
Return Values |
The result from the algorithm. This is a number (0-6) that corresponds to the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is Saturday. |
Information collected from the input NONE (all user inputs from
MAIN script) command
Output to the screen NONE (all outputs printed from MAIN
script)
On the main script, welcome the user, ask for his/her name, and DOB (date of birth). Then call the “defined function” and print-out a formatted output.
USER INPUT
USER INPUT
FORMATTED OUTPUT
Finally ask if the user if he/she wants to RERUN. If yes, loop your code all-over. If not, just display a goodbye message.
]
(Below is the solution for the code. you just have to develop the user defined function and the rest of the instructions are above. when finished it should look like the larger text)
clear
clc
% Welcome message
fprintf('Welcome. This program uses the Zeller''s algorithm to compute\n')
fprintf('the day of the week for a given date. Outputs are given as\n')
fprintf('numbers between 0 - 6:\n')
fprintf('(0) - Sun \t(1)- Mon \t(2)- Tue \t(3)- Wed \t(4)- Thu \t(5)- Fri')
fprintf('\t(6)- Sat\n')
fprintf('\nINPUT THE DATE:\n')
% Input section
Month = input('Month: ');
Day = input('Day: ');
Year = input('Year: ');
% A = 1 plus the remainder of (the month number plus 9) divided by 12
A = 1+mod(Month+9,12);
% B = the day of the month
B = Day;
% C = the year of the century PLUS the ROUND DOWN from the equation: (0.09*Month-
0.27).
C = (mod( Year , 1000 ))+floor(0.09*Month-0.27);
%D = the century
D = floor( Year / 100 );
% Additional integers necessary for the calculation
W = floor((13*A-1)/5);
X = floor(C/4);
Y = floor(D/4);
Z = W+X+Y+B+C-2*D;
% R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is
Saturday
R = mod( Z, 7 );
fprintf('\nOUTPUT:\n')
fprintf('%d/%d/%d = %d\n', Month, Day, Year, R );
% zelleralg function
function R = zelleralg(Month, Day, Year)
% A = 1 plus the remainder of (the month number plus 9) divided by
12
A = 1+mod(Month+9,12);
% B = the day of the month
B = Day;
% C = the year of the century PLUS the ROUND DOWN from the
equation: (0.09*Month-0.27).
C = (mod( Year , 1000 ))+floor(0.09*Month-0.27);
%D = the century
D = floor( Year / 100 );
% Additional integers necessary for the calculation
W = floor((13*A-1)/5);
X = floor(C/4);
Y = floor(D/4);
Z = W+X+Y+B+C-2*D;
% R is the day of the week, where 0 represents Sunday, 1 is
Monday, . . ., 6 is Saturday
R = mod( Z, 7 );
end
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
% MAIN script
clear
clc
% Welcome message
fprintf('Welcome. This program uses the Zeller''s algorithm to
compute\n')
fprintf('the day of the week for a given date. Outputs are given
as\n')
fprintf('numbers between 0 - 6:\n')
fprintf('(0) - Sun \t(1)- Mon \t(2)- Tue \t(3)- Wed \t(4)- Thu
\t(5)- Fri')
fprintf('\t(6)- Sat\n')
fprintf('\nINPUT THE DATE:\n')
% Input section
Month = input('Month: ');
Day = input('Day: ');
Year = input('Year: ');
% function zelleralg called
R = zelleralg(Month, Day, Year)
% print the output
fprintf('\nOUTPUT:\n')
fprintf('%d/%d/%d = %d\n', Month, Day, Year, R );
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Output:
Welcome. This program uses the Zeller's algorithm to
compute
the day of the week for a given date. Outputs are given as
numbers between 0 - 6:
(0) - Sun (1)- Mon (2)- Tue (3)- Wed (4)- Thu (5)- Fri (6)- Sat
INPUT THE DATE:
Month: 3
Day: 31
Year: 2020
R = 2
OUTPUT:
3/31/2020 = 2