Search Program on this blog

Friday 6 November 2015

Write a function called older that takes as its input arguments six positive scalar integers: y1, m1, d1, y2, m2, d2, in that order, representing the birthdates of two persons. The variables that start with y stand for the year, m for the month and d for the day. The variables that end in 1 correspond to the first person, while those that end in 2 correspond to the second person. The function returns 1 if the first person is older, 0 if they have the same age, and -1 if the first person is younger. You do not need to check whether the inputs have appropriate values. For example, you may assume that both m1 and m2 are positive integers that are less than 13 and that the day numbers fit with their months.

function out = older(y1,m1,d1,y2,m2,d2)
if m2>0 && m2<13
    if m2==1 || m2==3 || m2==5 || m2==7 || m2==8 || m2==10 || m2==12
        if d2>0 && d2<32
            flag=1;
        else
            flag=0;
        end
    elseif m2==4 || m2==6 || m2==9 || m2==11
        if d2>0 && d2<31
            flag=1;
        else
            flag=0;
        end
    elseif m2==2
        if mod(y2,4)==0 && (mod(y2,400)==0 || mod(y2,100)~=0) && d2<=29 && d2>0
            flag=1;
        elseif d2>0 && d2<=28
            flag=1;
        else
            flag=0;
        end
    end
else
    return;
end
if m1>0 && m1<13
    if m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12
        if d1>0 && d1<32
            f=1;
        else
            f=0;
        end
    elseif m1==4 || m1==6 || m1==9 || m1==11
        if d1>0 && d1<31
            f=1;
        else
            f=0;
        end
    elseif m1==2
        if mod(y1,4)==0 && (mod(y1,400)==0 || mod(y1,100)~=0) && d1<=29 && d1>0
            f=1;
        elseif d2>0 && d2<=28
            f=1;
        else
            f=0;
        end
    end
else
    return;
end
if y1<y2
    out=1;
elseif y1==y2
    if m1<m2
        out=1;
    elseif m1==m2
        if d1<d2
            out=1;
        elseif d1==d2
            out=0;
        else
            out=-1;
        end
    else
        out=-1;
    end
else
    out=-1;
end

No comments:

Post a Comment