90 min

Practice: arrays

Questions 1 to 30 of unit 8: 14 short checks and 16 complete programs on indexes, bounds, sums, averages, extremes, counts and the ten-channel laboratory (lab experiments 10 and 17). Every answer is hidden until you open it.

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.

Answer

Try it yourself first, then open the solution step by step.

    2. Element access

    For int a[4] = {8, 3, 5, 9};, state a[0], a[2] and a[3].

    Answer

    Try it yourself first, then open the solution step by step.

      3. Declaration

      Write a declaration for an array named samples that holds 10 ints.

      Answer

      Try it yourself first, then open the solution step by step.

        4. Initialization

        Initialize an array limits with 10, 20 and 30, and let the compiler determine the length.

        Answer

        Try it yourself first, then open the solution step by step.

          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]);
          Answer

          Try it yourself first, then open the solution step by step.

            Sums, averages and extremes

            6. Accumulation

            Trace the final sum of {2, -1, 4, 3} with a sum that starts at 0.

            Answer

            Try it yourself first, then open the solution step by step.

              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;
              Answer

              Try it yourself first, then open the solution step by step.

                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.

                Answer

                Try it yourself first, then open the solution step by step.

                  9. Occurrence count

                  How many times does 4 occur in {4, 1, 4, 4, 2}?

                  Answer

                  Try it yourself first, then open the solution step by step.

                    10. scanf into an element

                    Repair this line and name the fault:

                    scanf("%d", data[i]);
                    Answer

                    Try it yourself first, then open the solution step by step.

                      Initialization, bounds and tests

                      11. Partial initializer

                      What values are stored by int a[5] = {7, 9};?

                      Answer

                      Try it yourself first, then open the solution step by step.

                        12. Outside the array

                        What does C guarantee when a program reads a[5] from an array of length 5?

                        Answer

                        Try it yourself first, then open the solution step by step.

                          13. All equal

                          Why is an array where all values are equal a valuable test for minimum, maximum and occurrence counts?

                          Answer

                          Try it yourself first, then open the solution step by step.

                            14. Reverse loop

                            Write a loop header that visits the indexes 5 down to 0, both included.

                            Answer

                            Try it yourself first, then open the solution step by step.

                              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.

                              Answer

                              Try it yourself first, then open the solution step by step.

                                16. Sum and average

                                Read 10 ints. Print the sum and the average with three digits after the decimal point, on one line.

                                Answer

                                Try it yourself first, then open the solution step by step.

                                  17. Count even values

                                  Read 12 ints and print how many are even. Zero counts as even.

                                  Answer

                                  Try it yourself first, then open the solution step by step.

                                    18. Minimum and maximum

                                    Read 10 ints and print the minimum and the maximum on one line.

                                    Answer

                                    Try it yourself first, then open the solution step by step.

                                      19. Target occurrences

                                      Read 10 ints and then a target. Print how many times the target occurs.

                                      Answer

                                      Try it yourself first, then open the solution step by step.

                                        20. Reverse output

                                        Read 7 ints and print them in reverse order, separated by spaces, without changing the array.

                                        Answer

                                        Try it yourself first, then open the solution step by step.

                                          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.

                                          Answer

                                          Try it yourself first, then open the solution step by step.

                                            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.

                                            Answer

                                            Try it yourself first, then open the solution step by step.

                                              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.

                                              Answer

                                              Try it yourself first, then open the solution step by step.

                                                24. Nondecreasing test

                                                Read 8 ints. Print SORTED when every value is at least the one before it, otherwise NOT SORTED.

                                                Answer

                                                Try it yourself first, then open the solution step by step.

                                                  25. Longest positive run

                                                  Read 12 ints and print the length of the longest run of consecutive values greater than 0.

                                                  Answer

                                                  Try it yourself first, then open the solution step by step.

                                                    26. Second distinct maximum

                                                    Read 10 ints. Print the second-largest distinct value, or NONE when there are fewer than two distinct values.

                                                    Answer

                                                    Try it yourself first, then open the solution step by step.

                                                      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.

                                                      Answer

                                                      Try it yourself first, then open the solution step by step.

                                                        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.

                                                        Answer

                                                        Try it yourself first, then open the solution step by step.

                                                          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.

                                                          Answer

                                                          Try it yourself first, then open the solution step by step.

                                                            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);
                                                            Answer

                                                            Try it yourself first, then open the solution step by step.