You are on page 1of 7

Programmering grundkurs, DT106G

Programmering grundkurs, teori

A001
4,5 högskolepoäng

Skriftlig tentamen
2023-01-13

Pascal Rebreyend 1 (7)


Programmering grundkurs, DT106G (A001)

Tillåtna hjälpmedel: penna, radergummi, engelska-svenska ordbok

Instruktioner:
 Läs igenom alla frågor noga.
 Ange tentamenskoden på svarsdokumentet.
 Du kan svara på Svenska eller Engelska.
 Skriv tydligt (gäller även för en digital tentamen).
 Detta är en individuell examination - alla misstankar om otillåtet samarbete
kommer att rapporteras.
 Ansvarig lärare finns tillgänglig via telefon fr.o.m. andra skrivtimmen.
 Skriv läsligt!
 förklara och motivera era svar

I slutet av detta dokument finns en lite kompendium om C.

Ansvarig lärare: Pascal Rebreyend, tel: 0702001422

För betyg G krävs 50% av total poäng (20 på 40)


(26 poång gav betyg 4, och 33 poäng gav betyg 5)

Lycka till!

Pascal Rebreyend 2 (7)


Part 1: Programming (25 points)
Question 1: (5 points)
Write a C program which:
1. Read 4 numbers from the user (a,b,c and d) (a, b and c are real numbers
while d is a positive integer)
2. compute and display √ 2 a+4 x +|c|−d!
2

3. check if d is a prime number. (A prime number is a number which can only be


divided by 1 and itself)

Question 2: (5 points).
The goal of this exercise is to write a program written in C to simulate how long a
loan (lån på svenska) will take to be repaid.
The program should satisfy the following constraints:
• The user should enter the amount of money borrowed, the annual interest
rate as well as how much he will pay every month (both to pay interests and
pay back the loan).
• The amount paid by the user will not change during the loan, except for the
last month.
Note: The interest paid every month are decreasing every month since every month the amount of
money borrowed is lower (every month the customer pay back a part of his loan).

You program should simulate the loan and print to the screen these two pieces of
information:
1. How many years and months are needed to paid back the entire loan.
2. What is the amount paid the last month.
If data entered are inconsistent, a warming message should be issued.

Question 3: (6 points)
Write the program which do, in the following order:
1. Ask the user a number. If this number is positive, this number should be
added to a link list aimed at storing all the numbers entered by the user and
then the program should go back to 1 and ask a new number. If the number is
strictly negative, the program goes to 2.
2. Create an array (using malloc or calloc) of the size of the list.
3. remove the element with the highest value from the list and put it in the array
in the first emply slot and repeat this until the list is empty and the array full-
4. display the contents of the array on the screen.

Pascal Rebreyend 3 (7)


Question 4: (3 points)
A teacher is grading an exam which is on 100 points. Students will get the following
ECTS grade according the table below:

Points Grade
90-100 A
80-89 B
70-78 C
60-69 D
50-59 E
0 -49 F

Write a program using the switch statement doing which prints for a number of points
(entered by the user), the corresponding grade.

Question 5: (6 points).
The compagny you are working in want to develop a software to do very specific
geometrical computations and tests. As a member of the project you have the
following task to write some function(s) dealing with points in a 3-D environment.
Each points is therefore represented by the 3 coordinates x,y and z.
Your first task is to create a function which reads a file containing a list of points. The
file has the following format: Each line represents a different point and has 4
member: The 3 values x,y and z for this point and then the name of the point. (A set
of characters, no more than 50 characters). Once all the points are read, your
function should:
1. Check if 2 or more points with different coordinates have the same name and
then issue a warning message.
2. write a new file containing all the points from the new files except the ones for
which a warning has been issued. If two points with different names have the
same location, then only one line is written and both names will be stored on
the line

Example:
If the input file is (4 lines) :
3.4 5.3 3.5 Home
2.3 5.6 9.4 Work
3.4 5.3 3.5 Shop
2 4 6 Work
the program should display ”Warning: 2 points with the same name: Work”
and the output file should be (1 line):
3.4 5.3 3.5 Home Shop

Which tests will you perform if you have to test your code?

Pascal Rebreyend 4 (7)


Part 2: Code Analysis (15 points)

Question 6: (5 points)
What is the output of the following C-code when executed:

#include <stdio.h>

int max(int x, int y, int z)


{
int tmp = x;
if (tmp<y) tmp=y;
if (tmp<z) tmp=z;
return tmp;
}

int min(int *x, int *y, int *z)


{
int tmp = *x;
if (*y<*z)
{tmp=*y;}
else
{tmp=*z;}
*z=*x-*y;
return(tmp);
}

int main()
{
int x = 2, y = 4, z = 9;
int a,b;
a=max(x, y, z);
printf("The max of %d %d %d is %d\n", x, y, z,a);
b=min(&x, &y, &z);
printf("The min of %d %d %d is %d\n", x, y, z,b);
return 0;
}

Pascal Rebreyend 5 (7)


Question 7: ( 5 points)
What the following code is doing?

#include <stdio.h>

int l=0;
void f(int a,int b)
{
int i,j;
if (a==0)
{
printf("%d\n",b);
l++;
}
else
{
j=b%10;
for (i=j+1;i<10;i++)
f(a-1,b*10+i);
}
}

void main()
{
int c;
printf("Enter a number: ");
scanf("%d",&c);
f(c,0);
printf("l=%d\n",l);
}

Pascal Rebreyend 6 (7)


Question 8 (5 points)
Is the following program is correctly written? If not, what are the errors and what this
code is supposed to do?

#include <stdlib.h>
#include <stdio.h>

struct Test{
int payload;
struct Test *yes;
};

int df(struct Test *T,int *R)


{
int i=0;
struct Test *p;
p=T;
while (p!=NULL)
{
R[i]= p->payload;
i++;
p=p->yes;
}
return i;
}

void main()
{
struct Test *Start,*p;
int i,nb,c;
int *n;
scanf("%d",nb);
Start=malloc(sizeof(struct Test));
Start->yes=NULL;
Start->payload=rand()%1000;
for (i=0;i<nb;i++)
{
p=malloc(sizeof(struct Test));
p->payload=rand()%1000;
if (p->payload=13) printf("WIN\n");
p->yes=Start;
Start=&p;
}
n=calloc(nb,sizeof(int));
c=df(Start,n);
for (i=0;i<c;i++)
printf("V=%d\n",n[i]);
free(n);
}

Pascal Rebreyend 7 (7)

You might also like