Professional Documents
Culture Documents
Tentamen DVA127 DVA126
Tentamen DVA127 DVA126
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.
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”
}
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.
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 50cm2, så läggs ytterligare en fast avgift på 100 kr till portot. En skrymmande vara
(t.ex en vara med storlek 60cm2) 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.
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.
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”
}
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.
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 50cm2, an additional fee of
100 SEK is added to the shipping price. So a package of 0.5kg which is large (say 60cm2)
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).
int x, *y;
float z;
char str[10];
Expressions:
a) z + 5.0
b) &x
c) str[0]
d) 0.5
e) *y