Test: arrays

Test 4 of 8 · Tests

A test on one-dimensional arrays: reading and printing an array, sum and average, the largest and the smallest element, counting elements with some property, forming a new array from an existing one.

Before you start: review the lessons Arrays in C++, Arrays in Java, and then do the tasks from the pages Arrays – examples, Sorting – examples.
How to practise: choose one group and solve it in 45 minutes, without looking at the solutions or your notes. Only after that compare your solution with the solution of the practice test.

What you need to know for the test

  • declaring an array and the indexes from 0 to n - 1
  • reading an array in a loop and printing it in one line
  • sum, average, the largest and the smallest element, the second largest
  • counting and printing the elements that satisfy a condition (even, divisible by 3, greater than the average…)
  • forming a new array: reversed order, every element increased, a copy with a condition

What the test looks like

  • Duration: one school lesson (45 minutes).
  • Tasks: 3 (sometimes 4) tasks; every group has tasks of the same difficulty, but with different data.
  • What is graded: whether the program works correctly for the example and for edge cases, whether the input and output are as in the text and whether the code is clear.
Suggested scoring (3 tasks of 10 points each)
Points0–1112–1617–2122–2627–30
Grade12345

The scale is a suggestion for self-assessment; the teacher can adapt it. (In Serbian schools grades go from 1 to 5, where 5 is the best.)

Practice test with solutions

The tasks from "Group 4" (PDF) are written out here as text, with an example, instructions and a solution. Set a timer for 45 minutes and try to solve them on your own; open the solutions only at the end.

TASK 01Group 4

The smallest and the largest height

An array of heights (in cm) of the members of a sports club is given. Find the smallest and the largest height.

Input: the number of members n, and then n heights.
Output: the smallest and the largest height, each in its own line.

Example

Input

5
182 175 190 168 179

Output

Smallest: 168
Largest: 190
TASK 02Group 4

Countdown

Write a program that counts down from a given number to zero. Store the numbers in an array, and then print the array and the sum of its elements.

Example

Input

5

Output

5 4 3 2 1 0
Sum: 15
TASK 03Group 4

Special offer: six products

There is a special offer in a shop: a customer who buys six products gets the cheapest of them for 1 dinar. Find the reduced price of the six products.

Input: 6 integers from the interval from 50 to 5000 – the prices of the products in dinars.
Output: the total reduced price.

Example

Input

300 150 1200 80 640 99

Output

2390
Common mistakes and tips
  • An array of n elements has the indexes from 0 to n − 1; a[n] is already outside the array.
  • For the average use real division: (double)sum / n.
  • For the "second largest", remember two values and be careful about what happens when a new value exceeds the first one.
  • An array in reverse order: b[i] = a[n - 1 - i].
  • If it is enough to look at a number only once, you may not even need an array – but a test often asks you to store the data in an array.

Check your knowledge

Open the quiz

Mini quiz: arrays

1. What is the index of the last element of the array int a[10]?




2. How do you get the element symmetric to the element a[i] in an array of length n?




3. What is a good starting value for the smallest element of an array?