תרגול: מערכים
שאלות 1 עד 30 של יחידה 8: 14 בדיקות קצרות ו־16 תוכניות שלמות על אינדקסים, גבולות, סכומים, ממוצעים, קיצון, ספירות ומעבדת עשרת הערוצים (ניסויים 10 ו־17). כל תשובה מוסתרת עד שפותחים אותה.
השיעור הזה כתוב באנגלית בלבד.
How to use this page
Questions 1 to 14 are short checks: answer with a value, a sentence or one line of C. Questions 15 to 30 are complete programs. For each program, submit working code and three tests: a normal case, a boundary case, and a special case (all values equal, negatives and zero, the target absent).
Try each question before you open the answer. The programs use scanf, so they start with #define _CRT_SECURE_NO_WARNINGS for Visual Studio. Every program stops with return 1 if the input is not a number (scanf did not return 1). Questions 31 to 46 are on the next page: Practice: traversal and strings.
Indexes and declarations
1. Valid indexes
For int data[6];, list every valid index.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
2. Element access
For int a[4] = {8, 3, 5, 9};, state a[0], a[2] and a[3].
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
3. Declaration
Write a declaration for an array named samples that holds 10 ints.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
4. Initialization
Initialize an array limits with 10, 20 and 30, and let the compiler determine the length.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
5. Off-by-one
The array a has length 5. Repair this loop and name the fault:
for (int i = 0; i <= 5; i++) printf("%d", a[i]);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Sums, averages and extremes
6. Accumulation
Trace the final sum of {2, -1, 4, 3} with a sum that starts at 0.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
7. Average type
sum and length are both int and a fractional result is needed. Repair this line and name the fault:
double avg = sum / length;נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
8. Extremes initialization
Why is minimum = data[0] usually better than minimum = 0 for an array that is not empty? Give one reason and one example that would fail otherwise.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
9. Occurrence count
How many times does 4 occur in {4, 1, 4, 4, 2}?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
10. scanf into an element
Repair this line and name the fault:
scanf("%d", data[i]);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Initialization, bounds and tests
11. Partial initializer
What values are stored by int a[5] = {7, 9};?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
12. Outside the array
What does C guarantee when a program reads a[5] from an array of length 5?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
13. All equal
Why is an array where all values are equal a valuable test for minimum, maximum and occurrence counts?
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
14. Reverse loop
Write a loop header that visits the indexes 5 down to 0, both included.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Programs: read, print and summarize
15. Read and print
Read 8 ints into an array and print them in input order on one line, separated by spaces.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
16. Sum and average
Read 10 ints. Print the sum and the average with three digits after the decimal point, on one line.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
17. Count even values
Read 12 ints and print how many are even. Zero counts as even.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
18. Minimum and maximum
Read 10 ints and print the minimum and the maximum on one line.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
19. Target occurrences
Read 10 ints and then a target. Print how many times the target occurs.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
20. Reverse output
Read 7 ints and print them in reverse order, separated by spaces, without changing the array.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
21. Copy an array
Initialize one array with the six values 3 1 4 1 5 9, copy each element into a second array, then print the copy on one line. There is no input.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Programs: compare, search and patterns
22. Compare two arrays
Read two arrays of length 5. Print EQUAL when the elements at every position match, otherwise DIFFERENT.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
23. First and last target
Read 10 ints and a target. Print the first and the last index of the target, or -1 -1 if it is absent.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
24. Nondecreasing test
Read 8 ints. Print SORTED when every value is at least the one before it, otherwise NOT SORTED.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
25. Longest positive run
Read 12 ints and print the length of the longest run of consecutive values greater than 0.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
26. Second distinct maximum
Read 10 ints. Print the second-largest distinct value, or NONE when there are fewer than two distinct values.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
27. ADC range report
Read 16 ADC samples. Valid values are 0 to 1023, both included. Line 1: the number of valid and invalid samples. Line 2: the average of the valid samples with three decimals, or NO VALID DATA when none are valid.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
Programs: the laboratory
28. Minimum and maximum counts (lab experiment 17)
Read 13 ints. Line 1: the minimum and how many times it appears. Line 2: the maximum and how many times it appears. Line 3: the average with three decimals.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
29. Ten-channel laboratory (lab experiment 10)
Read exactly 10 ADC values. Print four labeled lines: SUM, AVG with three decimals, MIN value and count, MAX value and count. Do not change the array.
נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.
30. Boundary repair
Repair a program that should read 10 ints and print their sum. It uses an uninitialized sum, starts at index 1, ends with i <= 10, and leaves out the & in scanf:
int data[10];
int sum;
for (int i = 1; i <= 10; i++) {
scanf("%d", data[i]);
sum += data[i];
}
printf("%d\n", sum);נסו לפתור לבד, ואז פתחו את הפתרון צעד אחר צעד.