Question

In: Computer Science

Matlab code problems I have to write a code using functions to find out if a...

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

Solutions

Expert Solution

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


Related Solutions

Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
I'm new in MATLAB and I have to write a code in MATLAB which converts a...
I'm new in MATLAB and I have to write a code in MATLAB which converts a number from one base to another without using base2base, etc
write a matlab code to find the following: initial position, initial velocity, and acceleration using the...
write a matlab code to find the following: initial position, initial velocity, and acceleration using the algorithm and information below time(seconds). height(m) velocity(m/s) 0. 0.2. 2.95 algorithm: 1. Enter data in to arrays. 2. Fit the height data to a 2nd order polynomial. 3. Evaluate the polynomial at enough points to get a smooth curve. 4. Find the velocity model by taking derivative of the height polynomial. 5. Evaluate the velocity polynomial at enough times to get a smooth curve
I want the code for the 2D Ising model using Matlab
I want the code for the 2D Ising model using Matlab
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
Write a MATLAB code for importing an image and then being able to find all the...
Write a MATLAB code for importing an image and then being able to find all the coordinates that would draw that image on a plot column by column and row by row with a continuous line.
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only...
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only explain the steps of designing filter
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT