Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

Rock paper scissors game you will program in 
NASA, Public Domain, Wikimedia Commons

Rock paper scissors game you will program in Exercise 2
NASA, Public Domain, Wikimedia Commons

Before we begin with the class:

Test

(20 min)

Accept the assignment

Learning goals

Preparation Review

Teach your peers

VIC CVUT, CC BY 3.0, via Wikimedia Commons

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:

  1. (Optional) Find a talking stick and give it to the first instructor.

  2. 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

  3. 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.

  4. Pass the stick to the next person and repeat until everyone has been the instructor.

  5. Which concept or question do you still have problems with? Add one or two keywords to 📊 Live poll.

E: Teach your peers

Question pool:

  1. Why should you split a 100-line program into functions? Give two benefits.

  2. What does divide and conquer mean?

  3. Why should we use the C standard library?

  4. Which function is required in every program?

  5. 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 n as a parameter and print n times Hello.

    • Take no parameters and print Welcome.

    • Take x and n as parameters and calculate xnx^n.

  6. Write a function that calculates x3x^3.

  7. Which one(s) lets you write total = f(3) + f(4)?

    1. int f(int x) { return x + 1; }
    2. int f() { return 1; }
    3. int f(int x) { printf("%d", x+1); }
    4. #include <math.h> 
      int f(int x) { sqrt(x); }
    5. #include <math.h> 
      int f(int x) { printf("%d", sqrt(x)); }
  8. 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 a double like in square(2)?

  9. 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?

  10. There are three functions in the following code. In which order are they called?

    ...
    int main() {
      printf("%d square is: %d", a, square(a));
    }
  11. What is the value of a at the end of the following code?:

    int a = 0;
    int inc(int n) {return n+1;}
    inc(a);
  12. Which line simulates a six-sided die?

    • rand() % 6

    • rand() / 6

    • rand(6)

    • rand(6) % 6

    • INT_MAX % 6

    • INT_MAX / 6

  13. 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 of CONTINUE etc

  14. You 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, b in f(a), and b in g? Which concept (called s...) makes this possible?

  15. 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


## 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:

Footnotes