תרגול: פונקציות
30 השאלות של יחידה 7: 14 בדיקות קצרות ו־16 תוכניות שלמות, מהזימון הראשון ועד בקר עמדת טעינה בדוק. כל תשובה מוסתרת עד שפותחים אותה.
השיעור הזה כתוב באנגלית בלבד.
How to use this page
Questions 1 to 14 are short checks: answer in a value, a sentence or a few lines of code. Questions 15 to 30 are complete programs. For each program, submit working code and three tests: a normal case, a boundary case, and an invalid or special case when there is one.
Try each question before you open the answer. The programs use scanf, so they start with #define _CRT_SECURE_NO_WARNINGS for Visual Studio (see the scanf lesson).
Prototypes and calls
1. Prototype
In int limit(int value, int high); state the function name, the return type, and the parameter types.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
2. Call result
Given int triple(int x) { return x * 3; }, what is the value of triple(4)? Show the value of x.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
3. Argument and parameter
In the call square(count), identify the argument. In int square(int value), identify the parameter. Answer in one or two sentences.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
4. void or value
Choose a return type for a function that only prints a separator line. Give one reason.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Copies and scope
5. Pass by value
Trace this code. What is a after the call?
void f(int x)
{
x = 9;
}int a = 3;
f(a);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
6. Local scope
A function declares int error = 0;. Can main use that variable by writing error?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Finding faults
7. Conflicting declaration
Find the fault: the prototype is int average(int, int); and the definition is double average(int a, int b). Write the corrected code and name the original fault.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
8. Lost fraction
Why does return (a + b) / 2; fail for 7 and 8 in a function that returns double? Repair it.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Contracts, tests and names
9. Function contract
Write a contract for is_valid_adc: the input range is 0 through 1023 inclusive; the output is 1 when valid and 0 otherwise.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
10. Boundary set
Choose the smallest useful set of tests for a function that accepts 10 through 20 inclusive. List the inputs and what each one checks.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
11. Clear name
Which name best states a returned result: check, thing, or is_temperature_safe?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Returns and program order
12. Early return
What does this function return for -8, 0 and 5?
int sign(int x)
{
if (x < 0) {
return -1;
}
if (x > 0) {
return 1;
}
return 0;
}נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
13. Caller responsibility
Explain the difference between value = clamp(value, 0, 100); and clamp(value, 0, 100);.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
14. Program order
Place these in a valid common order: function definition, include directive, prototype, main.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Functions with one value
15. Absolute value
Read one int and print its absolute value using int absolute_value(int x). Do not use a library function.
Input: one integer. Output: its absolute value.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
16. Minimum of three
Read three int values and print the smallest using int min3(int a, int b, int c).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
17. Even test
Read one int. Use int is_even(int x) and print EVEN or ODD.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
18. Clamp
Read value, low and high. If low is greater than high, print INVALID; otherwise print clamp(value, low, high).
Output: INVALID if low > high; otherwise the value moved inside the range.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
19. Temperature conversion
Read a temperature in Celsius as a double. Print it in Fahrenheit with exactly one decimal, using a function that returns the value.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Functions with loops
20. Digit count
Read a nonnegative int and print its number of decimal digits using int digit_count(int n). Zero has one digit.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
21. Digit sum
Read a nonnegative int and print the sum of its digits using int digit_sum(int n).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
22. Prime classifier
Read an int and print PRIME or NOT PRIME using int is_prime(int n).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
23. Greatest common divisor
Read two positive int values and print their greatest common divisor, using Euclid's algorithm in a function.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
24. Integer power
Read a base and an exponent from 0 through 12. Print the base raised to the exponent, using a loop inside int power_int(int base, int exponent).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Engineering functions
25. Voltage validity
Read millivolts and print VALID for 12000 through 14500 inclusive, otherwise INVALID. Put the rule in a function.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
26. Charge current
Write charge_current_ma: 2000 below 13500 mV, 1000 from 13500 through 14500, and 0 above 14500. Reject negative input.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
27. Triangle classifier
Read three positive side lengths. Use functions to check that they form a triangle, and print EQUILATERAL, ISOSCELES, SCALENE or INVALID.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
28. Sensor calibration
Read raw, offset, numerator and denominator. Print (raw + offset) * numerator / denominator with two decimals, using a function. A denominator of zero prints INVALID.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
29. Menu calculator
Read a double, an operator (+, -, * or /) and a second double. Use one function per operation and print the result with two decimals. Division by zero, or an unknown operator, prints INVALID.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
30. Charging station mission
Read voltage_mV and temperature_c. Print SAFE <current> when the voltage is 12000 to 14500 inclusive and the temperature is at most 45; otherwise print STOP 0. Use is_voltage_safe and charge_current_ma (question 26).
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.