% Solution to Review question 1 % calculates the TAX to be paid by a citizen of Timbuktu % defines income limits limit1 = 30000; limit2 = 60000; income = input('Please enter your income: '); if (income < limit1) % low income tax = 0.25*income; % tax = 25% of income; elseif (income >= limit1) && (income < limit2) % intermediate % income tax = 0.25*limit1 + 0.30*(income - limit1); elseif (income >= limit2) tax =0.25*limit1 + 0.30*(limit2-limit1) + 0.40*(income -limit2); end fprintf('You should pay $%10.2f in tax.\n',tax); fprintf('Thank you for living in Timbuktu\n'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solution to review question 2 % ask for three numbers and print the maximum of the three % No arrays nor built in functions other than input n1 = input('enter the first number : '); n2 = input('enter the second number : '); n3 = input('enter the third number : '); if (n1 >= n2) && ( n1>= n3) % n1 is the maximum max = n1; elseif (n2>= n1) && (n2>= n3) % n2 is the maximum max = n2; else % n3 must be the maximum max = n3; end %prints out the maximum max %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Alternate solutions to review question 2 x = input('enter the first number : '); y = input('enter the second number : '); z = input('enter the third number : '); if (x > y) if (x > z) fprintf('The maximum of numbers %d, %d, %d is %d',x,y,z,x); else fprintf('The maximum of numbers %d, %d, %d is %d',x,y,z,z); end else if (y > z) fprintf('The maximum of numbers %d, %d, %d is %d',x,y,z,y); else fprintf('The maximum of numbers %d, %d, %d is %d',x,y,z,z); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solution to review question 3 % Prints all integers between 1 and 1000 % that are divisible by 2 and 5 but not by 7 % % remember that the expression % mod(n,d)==0 % evaluates to true if (n IS divisible by d) % otherwise it evaluates to false. % % Thus, the English sentence % "n is divisible by 2 and 5 but not by 7" % which is equivalent to the bad but simple sentence % "(n is div. by 2) and (n is div. by 5) and (n is not div. by 7)" % can be translated into Matlab as % ( mod(n,2)==0 ) && (mod(n,5)==0 ) && ( mod(n,7)~=0 ) % the program is now simple to write for n=1:1000 % make n take all integer values between and 1000 if ( mod(n,2)==0 ) && (mod(n,5)==0 ) && ( mod(n,7)~=0 ) % if n is div by 2 and 5 but not by 7 print it out fprintf('%d ',n); end % the following block is just to get nice format if (mod (n,50) ==0) fprintf('\n'); % break the line once in a while end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solution to the review question 4 % The program simulates coin tossing. It prints "H" for heads % and "T" for tails. The simulation cointinues until the % first run of 6 heads occurs. At the end the total number % of coin tosses in the simulation is printed. run = 0; % contains the number of heads in the current run numtosses = 0; % total number of toses desiredlength = 6; % length of run to stop while (run < desiredlength) % Toss a coin toss = floor(rand*2); % produces 0 or 1 with equal probabilities numtosses = numtosses +1 ; if (toss ==0) % this was tails fprintf('T'); run= 0; % the previous run of heads (if any) is over else % the toss was heads fprintf('H'); run= run+1; % the run of heads continues (or starts) end % The following block is for nice format if (mod(numtosses,50)==0) fprintf('\n') % breaks the lines once in a while end end fprintf('\nTotal number of tosses %d\n', numtosses); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solution to Review question 5 % Translate a time input in seconds % to the format hours:minutes:seconds secinmin = 60; secinhour = 3600; seconds = input('Introduce a time in seconds : '); hours= floor(seconds/secinhour); % the integer number of hours remainder = seconds - hours*secinhour; % the time (in secs) remaining after discounting the hours minutes= floor(remainder/secinmin); sec = remainder - minutes*secinmin; % seconds remaining after discounting the minutes fprintf('The time corresponds to %d:%d:%d\n',hours,minutes,sec);