Welcome 🤗! In the next ~4h we will cover:
Black dots represent prime numbers. Diagonal, vertical, and horizontal lines with a high density of prime numbers are clearly visible. (Ulam Spiral) Morn, CC0, via Wikimedia Commons, Source
Before we begin with the class:
reglerne til afleveringer: din aflevering skal give mening, ellers mister du også et forsøg.
DTU Findit giver dig gratis adgang til masser af bøger.
Du kan se yderligere bøger på Course text and further resources
Brug Findit for at spare penge, også anbefal til din underviser, hvis der er alternative gratis bøger
Answer the poll in 📊 Live poll: How much time did you spent for homework and preparation?
Test¶
(20 min)
Review¶
Differentiate the behavior of the operators
==and=
E: Misconceptions from homework Exercise 1¶
What is the output of the following code, if the input is 42 and in another run 0?
1 2 3 4 5 6 7 8 9 10 11 12 13#include <stdio.h> int main() { int score; printf("Enter your score: "); scanf("%d", &score); if (score = 42) { puts("42!"); } else { puts("NOT 42."); } }42!, thenNOT 42..NOT 42.then42!.42!in both cases.NOT 42.in both cases.I don’t know.
Credits: Xingjia for the misconception 😼
E: Reading doubts?¶
Stepwise refinement & while¶
Develop a program using top-down stepwise refinement
Use
ifandif-elseto select an actionUse a
whileto implement an iteration
Idea:
Example process¶
As electric vehicles (EV) become more common, some homeowners allow a public EV charging station to be installed on their property. In return, the charging station operator pays the homeowner a base payment of 200 EUR plus 9% of the revenue generated by the charging station.
For example, if the charging station generates 5000 EUR in revenue during a week, the homeowner receives: .
Write a program that inputs the weekly revenue and displays the homeowner’s earnings. Process one charging station at a time. Continue accepting input until the user enters -1.
Example interaction:
Enter revenue (-1 to end): 100
Earnings are: 209.00 EUR
Enter revenue (-1 to end): -1
Top-down refinement cycles¶
Step 1¶
Not really implementable, we need to refine.
Step 2¶
Refinement:
Unfortunately, this structure does not follow structured program development rules. A while requires that we loop back to the condition again after a sequence of statements as follows:
What if don’t follow the structured programming development rules?
Actually we can implement the previous flowchart using goto:
Code:
#include <stdio.h>
int main() {
double revenue = 0;
prompt_for_revenue:
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
if (revenue != -1) {
printf("Earnings:\n\n");
goto prompt_for_revenue;
}
}Test:
Enter revenue (-1 to end): 1
Earnings:
Enter revenue (-1 to end): -1Works, however goto typically leads to unreadable spaghetti code. We prefer blocks that belong together in a block statement { ... }. We should not use it 👎.
Step 3¶
We should read the input before we can run the condition in the diamond. At the same time we need to read input as the last statement in whiles iteration body.
Code:
#include <stdio.h>
int main() {
double revenue = 0;
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
while (revenue != -1) {
printf("Earning:\n\n");
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
}
}Test:
Enter revenue (-1 to end): 1
Earning:
Enter revenue (-1 to end): -1init[revenue = 0] --> while{revenue != -1} --true --> welcome[Prompt for revenue] --> if{revenue != -1} --true --> calc[Output earning] --> while
while --false --> _e(end) if --false -->while
```c
#include <stdio.h>
int main() {
double revenue = 0;
while (revenue != -1) {
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
if (revenue != -1)
printf("Earning\n\n");
}
}Test:
Enter revenue (-1 to end): 100
Earning
Enter revenue (-1 to end): -1-->
Step 5¶
Refine earning:
Code:
#include <stdio.h>
double BASE_PAYMENT = 200; // in EUR
double COMMISSION_RATE = .09;
int main() {
double revenue = 0;
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
while (revenue != -1) {
double earnings = BASE_PAYMENT + COMMISSION_RATE * revenue;
printf("Earnings are: %.2f EUR\n\n", earnings);
printf("Enter revenue (-1 to end): ");
scanf("%lf", &revenue);
}
}Test:
Enter revenue (-1 to end): 100
Earnings are: 209.00 EUR
Enter revenue (-1 to end): -1
Would you like to avoid the repetitive welcome prompt?
In programming we pay attention to not repeat code. However we did not introduce functions until now, which help to avoid repetitive code. We could also avoid with a trick:
#include <stdio.h>
double BASE_PAYMENT = 200; // in EUR
double COMMISSION_RATE = .09;
int main() {
double revenue = 0;
while (printf("Enter revenue (-1 to end): "), scanf("%lf", &revenue),
revenue != -1) {
double earnings = BASE_PAYMENT + COMMISSION_RATE * revenue;
printf("Earnings are: %.2f EUR\n\n", earnings);
}
}However, putting multiple statements in a while condition is less readable.
E: Monthly salary calculator¶
Do you need support?¶
Answer the following in 📊 Live poll:
Do you need more support for the exercises in the class than what the teacher (and TA) supports you?
Yes, most of the time I need more support than I currently get.
Sometimes — depends on the exercise.
No, the current support is enough and/or I rarely need help.
I don’t know.
Do you have time and energy to support your fellows, including those sitting further from you?
Yes, most of the time.
Sometimes – depends on the exercise.
No.
I don’t know.
Pre- and post-increment¶
Use pre- or post-increment
E: ++i vs i++¶
E: Prime number checker¶
Reflection¶

Of course you always get 42 in the previous misconception. Julian Hammer, CC BY-SA 3.0, via Wikimedia Commons Source