תרגול: מבוא ל־C
20 שאלות: אלגוריתמים לכתוב ולתקן, שכבות השפה והמהדר, ותוכניות ב־Visual Studio לבנות, להריץ ולתקן. כל תשובה מוסתרת עד שפותחים אותה.
השיעור הזה כתוב באנגלית בלבד.
How to use this page
The questions follow the three lessons of unit 1: what programming is, what C is, and the first program. Questions 1 to 6 are written on paper, 7 to 12 are short answers, and 13 to 20 are done in Visual Studio.
Try each question before you open the answer. For the Visual Studio questions, a correct answer means three things: the file is saved, the build succeeds, and the output is exactly right.
Algorithms
1. Prepare a cup of tea
Write the steps for "prepare a cup of tea" for someone who has never made tea. Number the steps. Include at least one check and a clear end.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
2. Draw a simple house
Write instructions that a classmate, who cannot see the picture, can follow to draw a house: a square, a triangle roof, a door and two windows with crosses. Your classmate draws exactly what you write, nothing more.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
3. Send a text message
Write the steps for sending the message "Hello There" to a classmate from your phone. Mark every step that is a check.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
4. Make it exact
Rewrite each instruction so a computer could follow it exactly.
- Wait a bit.
- Put some water in the kettle.
- Charge the battery for a while.
- Turn the multimeter to volts.
- Check that it works.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
5. Measure a battery
Write an algorithm: measure the voltage of a 9 V battery with a multimeter, for someone who has never used one. Number the steps, give exact values, add at least one check, and end clearly.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
6. Find the problems
What is wrong with this algorithm? Name three problems.
- Plug in the kettle and switch it on.
- Wait.
- Pour the water into the mug.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
From machine code to C
7. Which layer?
Write the layer for each line: machine code, assembly, or high-level (C).
ADD R0, R0, #3total = price * quantity;10110000 01100001printf("Hello, C!\n");MOV R0, #5
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
8. Be the processor
What number is in register R0 after these three lines run?
MOV R0, #5
ADD R0, R0, #3
ADD R0, R0, #3נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
9. The portability problem
A company has a large program written in the assembly language of one processor. It buys new computers with a different processor. Explain in two sentences what happens, and how writing the program in C would help.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
10. Order the chain
Put these in order, from the person to the result: machine code, programmer, compiler, high-level code, the computer runs it.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
11. The history of C
- Who created C, where, and in what year?
- Put these events in order: Unix is rewritten in C; Ken Thompson starts Unix on a PDP-7 in assembly; C is created.
- Why did Unix need a new language?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
12. Why C for a microcontroller?
A smartwatch has a small microcontroller with little memory. Give two reasons why its firmware is often written in C.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Visual Studio and the first program
13. The working loop
Put the six stages of the working loop in order: BUILD, CREATE, CHECK, EDIT, RUN, SAVE. What do you do when CHECK fails?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
14. Predict the output
What does each program print? Write the exact output, line by line.
#include <stdio.h>
int main(void)
{
printf("Status: ");
printf("READY\n");
return 0;
}#include <stdio.h>
int main(void)
{
printf("Hello,\nC!\n");
return 0;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
15. Team display
Create a new project 02_TeamDisplay and write a program whose console shows exactly:
Console Display
Status: READY
Team: <your team name>נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
16. Sensor console, with a fourth line
Change the program from the lesson so the console shows the device status report and, as a fourth line, the name of one sensor:
Sensor Console
Status: READY
Team: Alpha
Sensor: temperatureנסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
17. Boot sequence
You joined a firmware team. Every device prints a boot sequence when it is switched on. Write a program whose console shows exactly:
=== DEVICE BOOT v1.0 ===
Power rails ......... UP
Sensor bus .......... READY
Firmware ............ 1.0.3
Self-test ........... PASS
STATUS: OK(In the full version, the device also asks for a unit ID. Reading input comes with scanf in unit 3.)
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
18. Fix the bugs
This program has two bugs. Find them, say what Visual Studio reports, and fix them.
#include <stdio.h>
int main(void)
{
printf("Status: READY\n")
print("Team: Alpha\n");
return 0;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
19. House style check
This program builds and runs, but it breaks the house style. Find four problems and rewrite it.
#include <stdio.h>
void main() {
printf("Status: READY\n");
printf("Team: Alpha\n");
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
20. From memory
Close Visual Studio. Open it again, open your 01_HelloC solution with the .sln file, build and run it. Then create a new project 03_FromMemory and type the Hello, C! program without looking. How long did it take from Create to a successful run?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.