תרגול: לולאות
32 השאלות של טום ליחידה 6: בחירת לולאה, מעקב, השלמת קוד, תבניות זקיף, break ו־continue, לולאות מקוננות ודוגם הסוללה; ועוד ניסויי מעבדה 2, 4 ו־8.
השיעור הזה כתוב באנגלית בלבד.
Questions 1 to 14 are short exam-style checks. Questions 15 to 32 ask for a complete program or a complete repair. Try each question first, then open the answer.
Every complete program that reads input starts with #define _CRT_SECURE_NO_WARNINGS, so Visual Studio accepts scanf.
Choosing a loop
1. Choose: read until -1
Choose while, do...while or for: read measurements until the user enters -1. The number of measurements is unknown.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
2. Choose: exactly 12 samples
Choose while, do...while or for: read exactly 12 sensor samples.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
3. Choose: a menu
Choose while, do...while or for: display a menu once, then repeat it until the user chooses 0.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Tracing and completing loops
4. Trace while
Write the exact output and the number of body executions.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i += 2;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
5. Zero iterations
Write the exact output and explain why the body does not execute.
int x = 8;
while (x < 5) {
printf("%d", x);
x++;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
6. Trace for
Write the exact output, the number of body executions, and the first rejected value of i.
for (int i = 2; i <= 8; i += 2) {
printf("%d ", i);
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
7. Trace do...while
Write the exact output and the final value of x.
int x = 3;
do {
printf("%d ", x);
x--;
} while (x > 3);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
8. Complete a while loop
Fill the blanks so the program prints the integers 1 through 5.
int i = ___;
while (i ___ 5) {
printf("%d ", i);
i___;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
9. Complete a for loop
Fill the blanks so the program prints 10 8 6 4 2.
for (int i = ___; i ___ 2; i ___ 2) {
printf("%d ", i);
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
10. Complete validation
Complete the condition: repeat while level is outside the range 0 to 100 (both included).
do {
scanf("%d", &level);
} while (________________);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
11. Trace an accumulator
Write the value the program prints.
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
printf("%d", sum);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
12. Trace break
The input is 4 7 -1 9. Write the printed sum and say whether 9 is read.
int sum = 0;
for (int i = 0; i < 4; i++) {
scanf("%d", &x);
if (x == -1) break;
sum += x;
}
printf("%d", sum);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
13. Trace continue
The input is -2 3 -1 5. Write the final values of sum and valid.
int sum = 0, valid = 0;
for (int i = 0; i < 4; i++) {
scanf("%d", &x);
if (x < 0) continue;
sum += x;
valid++;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
14. Initialize a minimum
Explain why min should usually start from the first valid input and not from 0.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Counting loops
15. Print 1 through n
Input: one integer n, 1 ≤ n ≤ 100. Write a complete program, using while, that prints 1 through n on one line.
5
1 2 3 4 5נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
16. Sum 1 through n
Input: one integer n, 1 ≤ n ≤ 100. Write a complete program, using for, that prints the sum 1 + 2 + ... + n.
5
15נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
17. Countdown
Input: one integer n, 1 ≤ n ≤ 50. Print n down to 1.
4
4 3 2 1נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
18. Sum the even numbers
Input: one positive integer n. Print the sum of all the even integers from 2 through n.
7
12נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
19. Multiplication table
Input: one integer n. Print n multiplied by 1 through 10, one result per line, in the format n*i=result.
3
3*1=3
3*2=6
...
3*10=30נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
do...while
20. Range validation
Input: integers, one at a time. Use do...while to repeat until the user enters a value from 0 through 100. Then print accepted=<value>.
120
-3
75
accepted=75נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
21. Repeat a menu
Display 1=ON 2=OFF 0=EXIT. Read a choice and print ON or OFF for choices 1 or 2. Repeat until 0. Ignore other choices.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Sentinel loops and patterns
22. Sentinel count and sum
Input: nonnegative integers followed by -1. Print count=<count> sum=<sum>. Do not include the -1.
4 7 2 -1
count=3 sum=13נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
23. Average until a sentinel
Input: nonnegative integers followed by -1. Print the average with one decimal digit. If the first value is -1, print NO DATA.
4 7 1 -1
4.0נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
24. Count signs
Input: integers followed by 0. Count the positive and the negative values. The 0 at the end belongs to neither count.
4 -2 -7 3 0
positive=2 negative=2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
25. Maximum of five values
Input: exactly five integers. Print the maximum. Start max from the first input.
-8 -3 -11 -2 -6
-2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
26. Minimum until a sentinel
Input: positive integers followed by -1. Print the minimum. If the first value is -1, print NO DATA. Do not start min at 0.
9 4 7 -1
4נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
break and continue
27. Eight sensor samples
Input: exactly eight integers. The valid range is 0 through 1023, both included. Use continue to ignore invalid samples. Print the valid count, the invalid count and the sum of the valid samples.
100 -5 2000 50 1023 0 7 1024
valid=5 invalid=3 sum=1180נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
28. First multiple of 7
Input: exactly ten integers. Print the first input value divisible by 7 and stop reading. If there is none, print NOT FOUND.
5 12 21 8 14 1 2 3 4 6
21נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Nested loops and tables
29. Rectangle with nested loops
Input: two integers, rows and columns, each from 1 through 10. Print a rectangle of asterisks of that size.
3 4
****
****
****נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
30. Temperature conversion table
Input: two integers, start and end, with start ≤ end. For every Celsius value from start through end, print the Celsius value and the Fahrenheit value with one decimal digit. Use F = C × 9.0 / 5.0 + 32.0.
0 2
0 32.0
1 33.8
2 35.6נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Debugging
31. Repair an infinite loop
The required output is 0 1 2 4 5. Explain why this loop becomes infinite at 3. Then write a corrected complete program that uses continue.
int i = 0;
while (i <= 5) {
if (i == 3) continue;
printf("%d ", i);
i++;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Lab: the battery sampler
32. Battery sampler
Input: up to 12 integer readings in millivolts. -1 stops the input immediately. The valid range is 2500 through 4300, both included. Count invalid readings but leave them out of the statistics. Print the valid count, the invalid count, the average with one decimal digit, the minimum and the maximum. If there is no valid reading, print NO VALID DATA.
3000 5000 3200 -1
valid=2 invalid=1 avg=3100.0 min=3000 max=3200נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Lab experiments
These are from the Ministry's list of lab exam experiments. The numbers are the experiment numbers.
Lab 2. Largest and smallest
Read positive integers until a negative number. Print the largest and the smallest.
12 40 7 25 -1
max=40 min=7נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Lab 4. Grades until an impossible grade
Read grades until a grade below 0 or above 100. Print the average with one decimal and how many grades are above 90.
72 95 64 91 101
avg=80.5
above 90: 2נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Lab 8. Palindrome
Read a positive integer and print whether it is a palindrome.
12321
12321 is a palindromeנסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.