Skip to end of metadata
Go to start of metadata

a)

function name = getMonthName( month )
    switch (month) 
        case 1 
            name = 'Januar';
        case 2 
            name = 'Februar';
        case 3 
            name = 'Mars';
        case 4 
            name = 'April';
        case 5 
            name = 'Mai';
        case 6 
            name = 'Juni';
        case 7 
            name = 'Juli';
        case 8 
            name = 'August';
        case 9 
            name = 'September';
        case 10 
            name = 'Oktober';
        case 11 
            name = 'November';
        case 12 
            name = 'Desember';
        otherwise
            name = 'Ikke en gyldig maaned';
    end  
end

 

b) Bare å kopiere fra oppgavebeskrivelsen.

c)

Her holder det at studenten skriver end de første 12 iterasjonene. Poenget er å vise forståelse for hva som skjer (hvordan variablene week, day og date) endres.

Iterasjon
week
day
date
1110
2120
3131
4142
5153
6164
7175
8216
9227

10

238
11249
122510
132611
142712
153113
163214
173315
183416
193517
203618
213719
224120
234221
244322
254423
264524
274625
284726
295127
305228
315329
325430
335531
34560
35570
36610
37620
38630
39640
40650
41660
42670

d)

function day = getYearStartDay ( year )
	day = 1;
	for i = 1900:( year -1)
		if isLeapYear (i)
			day = day + 2;
		else
			day = day + 1;
		end
 
		if day >= 8
			day = day - 7;
		end
	end
end

e)

function day = getMonthStartDay ( year , month )
	if month > 0 && month < 13
		day = getYearStartDay ( year );
		for i = 1 : month -1
			day = day + daysInMonth (i , year);
		end

		while ( day > 7)
			day = day - 7;
		end
	else
		day = 0;
	end
end

 

f)

function printDays ( days )
	for week = 1:6
		for day = 1:7
			if days (week , day ) == 0
				fprintf ('   ');
			else
				if days (week , day ) < 10
					fprintf (' ');
				end
				fprintf ('%d ', days (week , day ));
			end
		end
	fprintf ('\n');
	end
end

g)

function printCalendar ( year , month )
	monthName = getMonthName ( month );
	days = getCalendar ( year , month );
	fprintf (' %s %d\n', monthName , year );
	fprintf ('ma ti on to fr lo so\n');
	printDays ( days );
end

h)

Nye funksjoner:

function weekNumber = getWeekNumber(year, month, day)
daysSoFar = day;
for i = 1 : month-1 
    daysSoFar = daysSoFar + daysInMonth(i, year);
end
dayOfWeek = mod(getMonthStartDay(year, month) + day - 1, 7);
if dayOfWeek == 0
    dayOfWeek = 7;
end
weekNumber = floor((daysSoFar - dayOfWeek + 10) / 7);
if weekNumber < 1
    weekNumber = getWeekNumber(year - 1, 12, 31);
end
end

Endrede funksjoner:

function printDays (days, month, year)
    for week = 1:6
        day = 1 + (week - 1) * 7;
        weekNumber = getWeekNumber(year, month, day);
        fprintf('%i\t', weekNumber);
        for day = 1:7
            if days (week , day ) == 0
                fprintf ('   ');
            else
                if days(week, day) < 10
                    fprintf (' ')
                end
                fprintf ('%d ', days (week , day ));
            end
        end
    fprintf ('\n');
    end
endend
function printCalendar ( year , month )
    monthName = getMonthName ( month );
    days = getCalendar ( year , month );
    fprintf ('\t%s %d\n', monthName , year );
    fprintf ('Uke\tma ti on to fr lo so\n');
    printDays (days, month, year);
end

  • No labels