In: Computer Science
Matlab code problems
I have to write a code using functions to find out if a year is a leap year. I'm on the write track i feel like but I keep getting an error message and matlab isnt helping to troubleshoot. The issue is on line 30. Here is my code:
function project_7_mfp()
% PROJECT_7_ABC project_7_abc() creates a
function that prompts the user
% enter a year after 1582 It then determines if it is a
leap year
%
% Name:
% Date:
% Class: CMPSC 200
%
% Print the splash screen using a
printHeader() function
% Prompt the user for the single data input for the
program. This
% is the year. You will need to write a
local function called getDataRange().
% Then call it here. The function will
return a single input. The
% getDataRange function must error check
the input to be sure that is at
% least 1582 when the Gregorian calenday
was enacted.
global debug
debug = 0;
getDataRange('Enter the year: ', 1582, inf);
if(debug)
fprintf('You entered %f\n', year);
end
% Call isLeapYear(year) to check if the year is a leap
year.
isLeapYear(year);
% Use printResults to print if the year is
a leap year or not
printResults(year, leap)
end
function getDataRange(prompt, a, b)
% GETDATARANGE getDataRange(prompt, a, b)
is a wrapper function to enter
% and error check that input. prompt is a variable
storing the prompt to be
% printed. a and b are the minimum and maximum values
for the error checking
%
% Name:
% Class: CMPSC 200
% Date:
%
global debug
year = input(prompt);
assignin('base','year',year)
if(debug)
fprintf('Calling the selection to check if the value is within
range\n');
end
%error check
if ((year < a) || (year > b))
if(debug)
fprintf('The value is out of range so throw an exception and
exit\n');
end
error('Entered value, %f, is outside of the range of %f to %f',
year, a, b);
end
if (mod(year,1) ~=0)
error('Entered value, %f, is not an integer',
year);
end
end
function leap = isLeapYear(year)
% ISLEAPYEAR isLeapYear(year) checks the
input to determine if it is a leap
% year. If it is, the function returns 1. If it is not
it returns 0
%
% Name:
% Class: CMPSC 200
% Date:
%
%failure
assignin('base','year',year)
if (mod(year,400)==0)
leap = 1;
elseif (mod(year,100)==0)
leap = 0;
elseif (mod(year,4)==0)
leap = 1;
else
leap = 1;
end
end
function printResults(year, leap)
% PRINTRESULTS printResults(year, leap)
prints a statement indicating
% if the year is a leap year or if it is not.
%
% Name:
% Class: CMPSC 200
% Date:
%
if leap == 1
fprintf('%f is a leap year', year);
elseif leap == 0
fprintf('%f is not a leap year', year);
end
end
So, total there are few changes that you need to do so as to remove error.
1. In project_7_mfp: You need to modify line 22 such that
year = getDataRange('Enter the year: ', 1582, inf);
And in getDataRange function modify line 1 as:
function year = getDataRange(prompt, a, b)
This will pass value of year user will type from getDataRange function to project_7_mfp function.
2. Modify line 30 to:
leap = isLeapYear(year);
This will pass value of leap from isLeapYear function to project_7_mfp function.
3. Finally, there is logical error in function isLeapYear. In line 20 of this function where inside else condition leap = 1, change it to leap =1 because when year is not divisible by 4, it is not a leap year.
FINAL UPDATED CODE
function project_7_mfp()
% PROJECT_7_ABC project_7_abc() creates a function that prompts the
user
% enter a year after 1582 It then determines if it is a leap
year
%
% Name:
% Date:
% Class: CMPSC 200
%
% Print the splash screen using a printHeader() function
% Prompt the user for the single data input for the program.
This
% is the year. You will need to write a local function called
getDataRange().
% Then call it here. The function will return a single input.
The
% getDataRange function must error check the input to be sure that
is at
% least 1582 when the Gregorian calenday was enacted.
global debug
debug = 0;
year = getDataRange('Enter the year: ', 1582, inf);
if(debug)
fprintf('You entered %f\n', year);
end
% Call isLeapYear(year) to check if the year is a leap year.
leap = isLeapYear(year);
% Use printResults to print if the year is a leap year or not
printResults(year, leap)
end
function year = getDataRange(prompt, a, b)
% GETDATARANGE getDataRange(prompt, a, b) is a wrapper function to
enter
% and error check that input. prompt is a variable storing the
prompt to be
% printed. a and b are the minimum and maximum values for the error
checking
%
% Name:
% Class: CMPSC 200
% Date:
%
global debug
year = input(prompt);
assignin('base','year',year)
if(debug)
fprintf('Calling the selection to check if the value is within
range\n');
end
%error check
if ((year < a) || (year > b))
if(debug)
fprintf('The value is out of range so throw an exception and
exit\n');
end
error('Entered value, %f, is outside of the range of %f to %f',
year, a, b);
end
if (mod(year,1) ~=0)
error('Entered value, %f, is not an integer', year);
end
end
function leap = isLeapYear(year)
% ISLEAPYEAR isLeapYear(year) checks the input to determine if it
is a leap
% year. If it is, the function returns 1. If it is not it returns
0
%
% Name:
% Class: CMPSC 200
% Date:
%
%failure
assignin('base','year',year)
if (mod(year,400)==0)
leap = 1;
elseif (mod(year,100)==0)
leap = 0;
elseif (mod(year,4)==0)
leap = 1;
else
leap = 0;
end
end
function printResults(year, leap)
% PRINTRESULTS printResults(year, leap) prints a statement
indicating
% if the year is a leap year or if it is not.
%
% Name:
% Class: CMPSC 200
% Date:
%
if leap == 1
fprintf('%f is a leap year', year);
elseif leap == 0
fprintf('%f is not a leap year', year);
end