Contents

Recommended problems

1: Write a function called nearEnough, which takes two floating point numbers as input. The output is 1, if they distance is less then 0.001, 0 otherwise.

function ki=nearEnough(a,b)

if abs(a-b)<0.001
    ki=1;
 else 
    ki=0;
end

2. Write a function called numInt, which takes a vector as input. The output is the number of integers amongst the elements of v.

function ki=numInt(v)

ki=sum(v==ceil(v));

3. Write a function called nDiff, which takes a vector as input. The output is a vector, which elements are the differences between the consecutive elements of v.

function ki=nDiff(v)

ki=v(2:end)-v(1:end-1);

4. Write a function called midTerm, which takes a integer between 0 and 100 as input. The output is your midterm mark, if the input is the sum of point you collected during the semester.

function ki=midTerm(n)

if n<50 
    ki=1;
elseif n<60 
    ki=2;
elseif n<70
    ki=3;
elseif n<80
    ki=4;
else
    ki=5;
end

5. Write a function called modif, which takes a vector as input. The output vector is the same as the input except all negative or greater than 10 elements are removed, and the rest of the elements are doubled.

function out1=modif(v)

out1=v(v>=0 & v<=10);

6. Write a function called beforeAfter, which takes four numbers as input. These four number are the dates of two events: the first one's year and month, and the second one's year and month. Your function have to decide which one happened first, and print this on the screen. The function should not have an output.

function beforeAfter(year1, month1, year2, month2)

if (year1<year2) || (year1==year2 && month1<month2)
    fprintf('The first event happend first \n')
elseif (year1==year2) && (month1==month2)
    fprintf('They happened the same time \n')
else
    fprintf('The second event happend first \n')
end