Øving:
Oppgave 1 - Flervalgsoppgaver
...
Svar: Spesifiserer formatet på pakkene som sendes over nettet, og mekanismene for å sende pakker fra én datamaskin via rutere, til en annen.
Oppgave 2 - Dra&Slipp
Code Block |
---|
function all_about_me(name, age, study) fprintf("My name is %s",name) fprintf("\n") fprintf("I am %s years old,",age) fprintf(" and I study %s",study) end |
Oppgave 3 - Kodeforståelse
a)
Code Block |
---|
x = 3 y = 2 Funksjonen returnerer hvor mange ganger b går opp i a og resten av divisjon av a på b. |
b)
Code Block |
---|
m = JULENISSEN Funksjonen returnerer en streng med annenhver bokstav hentet fra de to inputstrengene, med første bokstav hentet fra andre inputstreng (y). |
c)
Code Block |
---|
ans = 6 f regner ut den rekursive tverrsummen (av tverrsummen, av ...tallet), helt til det er bare et siffer igjen. |
d)
Code Block |
---|
101011001 Konverterer heltall til binærrepresentasjon |
Oppgave 4
Code Block |
---|
function wordsWithPrefix = checkPrefix(words , prefix) wordsWithPrefix = {}; for i = 1: length(words); iffor word = length(words{i}) >= length(prefix) && strcmp(words{i}(1: length(prefix))words if startsWith(word, prefix) wordsWithPrefix cellArray = [wordsWithPrefix words{i}]; {cellArray, word}; end end end |
Oppgave 5
Code Block |
---|
function isMatch = match(observed , actual) isMatch = true; for pos = 1: length(observed) if (observed(pos) ~= '?' && observed(pos) ~= actual(pos)) isMatch = false; end end end |
Oppgave 6
a)
Code Block |
---|
function date = dateStruct( day , month , year ) date = struct('day', day , 'month', month , 'year', year); end |
b)
Code Block |
---|
function movie = movieStruct(name , director , premiereDate) movie = struct('name', name , 'director ', director , 'premiereDate ', premiereDate '); end |
c)
Code Block |
---|
function validMovies = getMovieStructs(movies , fromYear , toYear) validMovies = []; for movie = movies if movie.premiereDate.year > fromYear && movie.premiereDate.year < toYear validMovies = [validMovies movie]; end end end |
Oppgave 7
Code Block |
---|
function [avgPuls , minPuls , maxPuls] = pulsStatistikk(pulsData) minPuls = inf; maxPuls = -inf; sumPuls = 0; antall = 0; for puls = pulsData antall = antall + 1; if puls < minPuls minPuls = puls; end if puls > maxPuls maxPuls = puls; end sumPuls = sumPuls + puls; end avgPuls = sumPuls/antall; end |
Oppgave Quiz
a)
Code Block |
---|
function result = beginsWith(capitals, letter) result = {}; for city = capitals if city{1}(1)==letter result(end+1) = city; end end end |
b)
Code Block |
---|
function letter = randomLatinLetter(upper) if upper asciiValue = randi([65 90]); else asciiValue = randi([97 122]); end letter = char(asciiValue); end |
c)
Code Block |
---|
function [questions, solutions] = letterQuestion(capitals) nSolutions = 0; while nSolutions == 0 letter = randomLatinLetter(true); solutions = beginsWith(capitals, letter); nSolutions = length(solutions); end questions = sprintf('Capital starting with %s ?', letter); end |
d)
Code Block |
---|
function result = nLetters(city) result = length(city); for i = city if city(i) == ' ' result = result -1; end end end |
e)
Code Block |
---|
function withLength = hasLength(capitals, length)
withLength = {};
for city = capitals
if nLetters(city{1}) == length
withLength(end+1) = city;
end
end
end |
f)
Code Block |
---|
function [que, sol] = lengthQuestion(capitals)
okLengths = [];
for city = capitals
len = nLetters(city{1});
if sum(len == okLengths) == 0
okLengths(end + 1) = len;
end
end
randomLength = okLengths(randi(length(okLengths)));
que = sprintf("Capital with %i letters? ", randomLength);
sol = hasLength(capitals, randomLength);
end |
g)
Code Block |
---|
function [que, sol] = randomQuestion(capitals)
qType = randi([0,1]);
if qType == 0
[que, sol] = letterQuestion(capitals);
else
[que, sol] = lengthQuestion(capitals);
end
end |
h)
Code Block |
---|
function playQuiz(capitals): keepOn = True points = 0 while keepOn [quest, sol] = randomQuestion(capitals) city = input(quest, 's') if strcmp(city, 'q'): keepOn = False elseif ismember(city, sol): disp('Correct!') points = points + 1; else disp('Sorry, that was wrong!') end end end |
i)
Code Block |
---|
function main() disp('CAPITALS QUIZ') disp('Answer questions to score points.') disp('To quit, answer q') points = playQuiz(allCapitals) if points == 1 text = '1 point.\n' else text = sprintf('%i points.\n', points) end fprintf('Congrats! You scored %s', text) end |
Code Block |
---|
function triplets = pythagoreanTriplets(n) triplets = []; a = 2; while true [rows, ~] = size(triplets); if rows >= n return end a = a + 1; % Ingen vits å lete over denne maxSearch = a^2; for b = a : maxSearch c = sqrt(a^2 + b^2); if mod(c, 1) == 0 triplets = [triplets; a, b, c]; end end end end |
...