Welcome 🤗! In the next ~4h we will cover:

Rock paper scissors game you will program in Exercise 2
NASA, Public Domain, Wikimedia Commons
Before we begin with the class:
Hvordan skal Xingjia bruge sin TA-tid? Baggrund: Xingjia giver i øjeblikket skriftlig feedback på dine afleveringer. Alternativt kunne hun bruge tiden på at holde en lektiecafé, hvor du får personlig støtte ansigt til ansigt. Svar 📊 Live poll:
Jeg foretrækker at beholde den skriftlige feedback på mine afleveringer.
Jeg foretrækker en lektiecafé i stedet for skriftlig feedback.
Jeg har ingen præference.
Jeg foretrækker noget andet end skriftlig feedback eller en lektiecafé.
Undervisningssprog 📊 Live poll
Midtvejsevaluering: https://
evaluering .dtu .dk => 62713
Test¶
(20 min)
Learning goals¶
Write void and value-returning functions that take parameters, and call them with arguments
Explain the difference between returning a value and printing it
Differentiate between local and global scope
Use the random function
randby includingstdlib.hand calling itDefine enumeration variables using
enum
Preparation Review¶
Teach your peers¶

VIC CVUT, CC BY 3.0, via Wikimedia Commons
Explaining something is one of the best ways to find out what you don’t understand yet[1].
Make groups of 3-4. One of the students will be the instructor and the others will be listeners.
Steps:
(Optional) Find a talking stick and give it to the first instructor.
Each person takes one minute to pick a different question and agrees on this with others. Everyone takes individually one minute to think about how you’d explain it.
(optional) you can pick the question you found hardest, so the others can help you
The instructor explains the question to the others (max 3 min).
Each listener should ask at least one question.
If you get stuck, you can pass the stick to someone else for help.
Pass the stick to the next person and repeat until everyone has been the instructor.
Which concept or question do you still have problems with? Add one or two keywords to 📊 Live poll.
E: Teach your peers¶
Question pool:¶
Why should you split a 100-line program into functions? Give two benefits.
What does divide and conquer mean?
Why should we use the C standard library?
Which function is required in every program?
Which task would you naturally write as a value-returning function, i.e., not void?
Take two strings as a parameter and produce their concatenation.
Take
nas a parameter and printntimesHello.Take no parameters and print
Welcome.Take
xandnas parameters and calculate .
Write a function that calculates .
Which one(s) lets you write
total = f(3) + f(4)?int f(int x) { return x + 1; }int f() { return 1; }int f(int x) { printf("%d", x+1); }#include <math.h> int f(int x) { sqrt(x); }#include <math.h> int f(int x) { printf("%d", sqrt(x)); }
You have a function
double square(double x)which calculates the square of a number. Does the function work, if we provide an integer instead of adoublelike insquare(2)?The following code crashes with the error
SIGSEGV: address not mapped....double square(double x) { return square(x + 1); } int main() { square(1); }What is the reason and what is the relation to the stack frame?
There are three functions in the following code. In which order are they called?
... int main() { printf("%d square is: %d", a, square(a)); }What is the value of
aat the end of the following code?:int a = 0; int inc(int n) {return n+1;} inc(a);Which line simulates a six-sided die?
rand() % 6rand() / 6rand(6)rand(6) % 6INT_MAX % 6INT_MAX / 6
The following code shows an enumeration:
enum GameStatus {CONTINUE, GAME_WON, GAME_LOST}; enum GameStatus game_status = CONTINUE;What does the code mean? What is
Gamestatus? What are the values ofCONTINUEetcYou have the following code:
int b = 0; int f(int a) { int b = a + 1; return b; } int g(int c) { int b = c + 1; return b; }Is there a difference between the global
b,binf(a), andbing? Which concept (calleds...) makes this possible?What does the following code output? Why? How is this behavior related to the fact that in C, all arguments are passed by value?
#include <math.h>
#include <stdio.h>
double a = 4, square_root_of_a = 0;
void square_root(double x, double result) { result = sqrt(x); }
int main() {
square_root(a, square_root_of_a);
printf("%f", square_root_of_a);
}Coding¶
If you are searching for a partner to work in a group, come to the front.
E: Shipping calculator¶
E: Rounding numbers¶
This is called round half up and can be achieved by adding 0.5 to the number and then using the floor from math.h:
floor(x + .5)Write a program that rounds half up numbers and outputs the result:
Enter the number to round to the nearest integer: 1.2
1.200000 -> 1.000000
Enter the number to round to the nearest integer: -1.2
-1.200000 -> -1.000000
Enter the number to round to the nearest integer: 1.7
1.700000 -> 2.000000
Enter the number to round to the nearest integer: 1.5
1.500000 -> 2.000000
Enter the number to round to the nearest integer: -1.5
-1.500000 -> -1.000000
Enter the number to round to the nearest integer: .49
0.490000 -> 0.000000
Enter the number to round to the nearest integer: .51
0.510000 -> 1.000000
Enter the number to round to the nearest integer: 42
42.000000 -> 42.000000
Enter the number to round to the nearest integer: x
Bye 👋
Example interaction
🛟 Compiler errors?
If you cannot compile your program after including a math.h function, make sure you have -lm as a compiler flag. -lm additionally links the math library. -lm is already integrated in the template that you use since Running programs in the editor.
On Windows, the math library is always linked and does not require the -lm flag. On Linux, it is needed.
## E: Rounding numbers to varying decimals
:::{exercise} Rounding numbers to varying decimals
:label: rounding-numbers-to-varying-decimals
Similar to @rounding-numbers, we can also round numbers to a specified number of decimal places, e.g.,:
```c
floor(x * 10 + .5) / 10 // rounds to the tenths
floor(x * 100 + .5) / 100 // rounds to the tenths
```
Create the following four functions and augment your output from @rounding-numbers:
1. `round_to_int`
1. `round_to_tenths`
1. `round_to_hundreds`
1. `round_to_thousandths`
```{literalinclude} /functions/inc/rounding_numbers_to_varying_decimals.transcript.txt
:filename: false
:caption: Example interaction
```E: Random integers¶
E: Coffee machine¶
E: Reflection¶
Let us revisit our learning goals:
Write void and value-returning functions that take parameters, and call them with arguments
Explain the difference between returning a value and printing it
Differentiate between local and global scope
Use the random function
randby includingstdlib.hand calling itDefine enumeration variables using
enum
(1 min)
What is one thing you got stuck on today, and what got you unstuck? Your group partner, the chat, the docs, or trial and error?
An activity based on learning by teaching or Protege effect.