a)

date = struct ('day', 20, 'month', 5, 'year', 1992) ;

b)

function printDate ( date )
	fprintf ('%02d.%02d.%d', date.day , date.month , date.year );
end

c)

person = struct('name', 'Avogadro', 'phone',60221413, 'dateOfBirth', struct('day',9,'month',8,'year',1776));

d)

function printPerson ( person )
	fprintf ('%s, ', person.name );
	printDate(person.dateOfBirth);
	fprintf (', %d \n', person.phone);
end

e)

function person = promptPerson ()
	name = input ('Hva heter du? ', 's');
	day = input ('Hvilken dato er du født? ');
	month = input ('Hvilken måned er du født i? ');
	year = input ('Hvilket år er du født i? ');
	phone = input ('Hva er telefonnummeret ditt ? ');
	dateOfBirth = struct ('day ', day , 'month ', month , 'year ', year );
	person = struct ('name', name, 'dateOfBirth', dateOfBirth, 'phone', phone);
end

f)

function age = getAge ( person )
	day = person.dateOfBirth.day;
	month = person.dateOfBirth.month;
	year = person.dateOfBirth.year;
 
	[Y M D] = datevec(now);
	age = Y - year ;
 
	if M < month || (M == month && D < day )
		age = age - 1;
	end
end

g)

function listOfPersons = batchRegisterPersons()
	morepeople = true;
	counter=1;
	while morepeople
        listOfPersons(counter)= promptPerson();
        morepeople = strcmp(input('Skal du registrere flere personer (ja/nei)? ', 's'),'ja');
        counter=counter+1;
	end        
	clc
	fprintf('Registrert!\n');
end

h)

function listPersons ( listOfPersons )
	for i = 1: length ( listOfPersons )
	printPerson(listOfPersons(i));
	end
end
  • No labels