CS112: Lab 6 :: Nested Loops (and fprintf)

Open the file calendar.m in your Assign5_exercises folder. In this part of lab, you will
  • practice writing nested loops
  • learn how to use fprintf

Task: Calendar printing

Your calendar.m file contains two variables: monthNames, a cell array of month names and daysInMonths, a vector of the number of days in each month.

Task 1. Write a for loop that produces this output

jan has 31 days
feb has 28 days
mar has 31 days
apr has 30 days
may has 31 days
jun has 30 days
jul has 31 days
aug has 31 days
sep has 30 days
oct has 31 days
nov has 30 days
dec has 31 days

Task 2a. Use fprintf instead of disp

Below are some fprintf examples (you can also check MATLAB's help about fprintf). In a nutshell, fprintf uses % as placeholders that are filled in with the provided values, in order from left to right. Some things to note:
  • '\n' is a newline
  • %d is an integer placeholder
  • %s is a string placeholder
  • %f is a floating point (decimal) number placeholder
  • %1.2f floating point (decimal) with one digit to the left of the decimal point and two digits of precision to the right of the decimal point
fprintf('%s %s %s\n','bees', 'make','honey')
bees make honey 

fprintf('%d %s make %f pots of honey \n', 12,'bees',17.4)
12 bees make 17.400000 pots of honey 

fprintf('%d %s make %2.3f pots of honey \n', 12,'humans', 17.4264)
12 humans make 17.426 pots of honey 

We can use fprintf within a for loop to keep output on the same line. See examples below.
for i=1:5
    disp(i)
end
1
2
3
4
5
for i=1:5
    fprintf(' %d',i)
end
fprintf('\n')
 1  2  3  4  5
for i=1:5
    fprintf('%5d',i)
end
fprintf('\n')
    1     2     3     4     5

Task 2b. Print a calendar with each month on one line

Make sure to use fprintf and nested loops. The outer loop controls the month, the inner loop controls the day of the month.

Month: jan 
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Month: feb 
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Month: mar 
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Month: apr 
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Month: may 
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Month: jun 
...

Task 2c. Print a calendar with weeks

We know the calendar below is not realistic, since each month does not always start on the first day of the week, but for the sake of simplicity, this is our goal today. As in Task 2b above, this will need nested loops in addition to starting every 7th day on a new line.
Month: jan 
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31
Month: feb 
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28
Month: mar 
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31
Month: apr 
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30
  ...