Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Tentamen DVA127/DVA126

Programmeringens grunder 2019-01-10

Hjälpmedel:​ valfritt icke-elektroniskt material (böcker, anteckningar, utskrifter). Läsplattor


utan internettillgång är också tillåtna.
Lärare:​ Stefan Bygde. Kan nås via telefon om man frågar tentavakterna.
Betygsgränser:​ Betyg 3: ​18p​ Betyg 4: ​25p​ Betyg 5: ​31p​. Totalt antal poäng: ​35p

Allmänt:
● Skriv tydligt. Oläsliga svar rättas ej.
● Läs igenom varje uppgift ordentligt för att se till att du förstår den innan du börjar.
● Enbart svar behövs. Inga utläggningar eller beräkningar.
● Om du gör ett antagande, skriv ner det tydligt.
● Globala variabler. Statiska variabler, rekursion eller ​goto ​får ej användas.

Lycka till!!​ Mvh/Stefan

Uppgift 1 : Kodförståelse: funktioner och pekare (6p)


Betrakta följande funktioner.

void ​foo(​int​ z, ​int​ *ptr)


{
*ptr = *ptr + z;
}

int ​test(​int​ a, ​int​ b)


{
int ​i;
for​(i=0; i<b; i++)
{
foo(i, &a);
}
return​ a;
}

Vilket värde returneras från följande anrop till ​test()​? Skriv inga förklaringar, enbart svar
behövs.
● a) test(2,5) ​(3p)
● b) test(1,3) ​(3p)
Uppgift 2: Arrayer och problemlösning (6p)
Skriv en funktion ​fillSpaces() ​som tar en sträng som argument och ersätter alla
mellanslag i strängen med ​kolon (‘:’)​. Nedan är ett exempel på hur funktionen ska kunna
användas, notera dock att funktionen ska fungera för ​alla​ möjliga strängar, inte bara den i
exemplet.

int​ main(​void​)
{
char​ test[] = ​“this is a test”​;
fillSpaces(test);
printf(​“%s”​, test); ​// Prints the text “this:is:a:test”
}

Uppgift 3: Felsökning (6p)


Nedan finner du kod skriven av en nybörjare i programmering. Tanken är att koden ska
returnera det största talet i en given array av flyttal.

int ​returnMaxFromArray(​float​ *arr, ​int ​arraySize)


{
float​ max = arr[0];
for​(i=0; i < arraySize; i++)
if​(arr[i] > max)
max = i;
return ​max;
}

Tyvärr har det smugit sig in några fel i koden, närmare bestämt tre stycken. Ett av felen ger
kompileringsfel, ett fel är ett typfel som ger en varning och det sista felet är rent logiskt:
funktionen returnerar inte maxtalet ur arrayen utan något annat. Svara ​kort​ (c:a en rad text)
på var och en av dessa frågor.

a) Vilket är kompileringsfelet? Hur kan vi rätta till det? (2p)


b) Vilket är typfelet som ger en varning? Hur kan vi rätta till det? (2p)
c) Vilket är det logiska felet? Vad behöver vi ändra till för att det ska bli rätt? (2p)
Uppgift 4: Structer, problemlösning & Funktioner (12p)
Vi tänker oss att det finns ett fiktivt postbolag som kallar sig “Skickligt”. De har ett program
för att hantera sina paket, och i det programmet använder de sig av följande struct:

struct ​package
{
char ​content[50]; ​/* en sträng som beskriver paketets innehåll */
int ​weight; ​/* paketets vikt i gram */
float​ sizeCm2; ​/* paketets storlek i kvadratcentimeter */
};

“Skickligt” har en egen variant för sina porton. Priset för att skicka en vara med Skickligt är
en tiondel av varans vikt i gram. Till exempel så skulle alltså en vara som väger 100g kosta
10kr att skicka med Skickligt. Om varan är “skrymmande”, vilket Skickligt definierar som
större än 50cm​2​, så läggs ytterligare en fast avgift på 100 kr till portot. En skrymmande vara
(t.ex en vara med storlek 60cm​2​) som väger 0.5kg skulle alltså totalt kosta 150 kr att skicka
med Skickligt (50kr för vikten + fast kostnad på 100kr för att det är skrymmande).

Din uppgift är att skriva en funktion ​computeShippingCost() ​som tar ett paket av structen
ovan som argument och returnerar Skickligts porto enligt reglerna beskrivna ovan.
Naturligtvis ska funktionen fungera för alla möjliga paket, inte bara de i exemplen.

Funktionen får inte skriva ut något på skärmen eller läsa in något från användaren (får alltså
inte använda ​printf()/scanf()​). Funktionen bör också bli kort, inte längre än 10 rader
kod.

Uppgift 5: Uttryck och Typer (5p)


Betrakta följande deklarationer, ange sedan vilken ​typ ​(t.ex ​char​-pekare, ​int​, ​float​-array)
uttrycken har. Varje rätt svar är värt 1p.

int​ x, *y;
float ​z;
char ​str[10];

Uttryck:
a) z + 5.0
b) &x
c) str[0]
d) 0.5
e) *y
Exam DVA127/DVA126
Fundamentals of Programming 2019-01-10

Aid:​ Any non-electronic material (books, notes, printouts ​etc)​ . E-readers without internet are
also allowed.
Examiner:​ Stefan Bygde. Can be reached on phone if you ask the staff.
Grades:​ Grade 3: ​18p​ Grade 4: ​25p​ Grade 5: ​31p​. Total points: ​35p

Rules:
● Write clearly. Illegible solutions will not be corrected.
● Read the assignments carefully to make sure you understand them.
● Write only answers, no explanations are necessary.
● If you make an assumption, write it down clearly.
● You may not use global variables, static variables, recursion or the ​goto​-statement in
this exam.

Good luck!!​ /Stefan

Assignment 1 : Understanding Code (6p)


Consider the following functions:

void ​foo(​int​ z, ​int​ *ptr)


{
*ptr = *ptr + z;
}

int ​test(​int​ a, ​int​ b)


{
int ​i;
for​(i=0; i<b; i++)
{
foo(i, &a);
}
return​ a;
}

Which value is returned from the following calls to ​test()​? Write only the answer, no
explanations.
● a) test(2,5) ​(3p)
● b) test(1,3) ​(3p)
Assignment 2: Arrays and problem solving (6p)
Write a function ​fillSpaces()​ which takes a string as argument and replaces all spaces in
the string with colons (​‘:’​). Below is an example of how the function can be used, but note
that the function should work for ​any​ string, not just the one in the example.

int​ main(​void​)
{
char​ test[] = ​“this is a test”​;
fillSpaces(test);
printf(​“%s”​, test); ​// Prints the text “this:is:a:test”
}

Assignment 3: Finding Errors (6p)


Below is code written by a beginner in programming. The idea is that the code should return
the largest value in an array of floating point numbers.

int ​returnMaxFromArray(​float​ *arr, ​int ​arraySize)


{
float​ max = arr[0];
for​(i=0; i < arraySize; i++)
if​(arr[i] > max)
max = i;
return ​max;
}

Unfortunately the programmer have made some errors, three of them to be precise. One of
the errors is a compile error, the second error is a type error giving a warning and the third
one is a purely logical error; the function does not actually return the maximum number in
the array.

Give a short answer (approximately one sentence) to each of the following:

a) What is the compile error? How can it be fixed? (2p)


b) What is the type error that gives a warning? How can it be fixed? (2p)
c) What is the logical error? What do we need to change to fix it? (2p)
Assignment 4: Structs, Functions, Problem solving (12p)
Consider an imaginary post delivery company called “Poster”. They have made a program
for handling packages. Their program uses the following struct:

struct ​package
{
char ​content[50]; ​/* A description of the content of the package */
int ​weight; ​/* The weight of the package in grams */
float​ sizeCm2; ​/* the size of the package in square centimeters */
};

“Poster” have their own shipping costs. The price to send a package with Poster is a tenth of
the package’s weight in gram. A package weighing 100g would cost 10 SEK to send, for
instance. If the package is large, which Poster defines as over 50cm​2​, an additional fee of
100 SEK is added to the shipping price. So a package of 0.5kg which is large (say 60cm​2​)
would cost 150 SEK in total to send (50 SEK for the weight + 100 SEK because it’s large).

You task is to write a function ​computeShippingCost() ​which takes a package of the struct
above as argument and returns Poster’s shipping costs according to the rules outlined
above. Of course the function should work for any package, not just the ones listed as
example.

The function may not print anything on the screen and may not ask the user for any
information (that is, you may not use ​printf()​ or ​scanf()​ ). In addition, the function should
be quite short (not longer than 10 lines of code).

Assignment 5: Expressions and Types (5p)


Consider the following declarations, then write which ​type ​(e.g., ​char​-pointer,​ int​,
float​-array ​etc​) the expressions below have. Each correct answer is worth 1p.

int​ x, *y;
float ​z;
char ​str[10];

Expressions:
a) z + 5.0
b) &x
c) str[0]
d) 0.5
e) *y

You might also like