Test: Loops

Test 3 of 8 · Tests

A test on loops: for, while and do–while, counting, adding up, finding the largest and the smallest, series, and input "until zero" or until the end of the input.

Before you start: revise the lessons Loops in C++, Loops in Java, and then do the tasks on the pages Loops – examples, Additional examples on loops.
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

  • the for loop with a given number of repetitions and a step (e.g. every other number, backwards)
  • the while loop when the number of repetitions is not known (input until zero, until the end of the input, until a condition is met)
  • a counter, a sum and a product – and their initial values (0, 0 and 1)
  • finding the largest and the smallest element and the longest series
  • divisibility (%) inside a loop and compound conditions with && and ||
  • physics simulations step by step (braking, free fall)

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.)

Tasks by groups (PDF)

Every card opens the PDF of one group (the PDF files are in Serbian). Below each title the tasks are listed briefly, to make it easier to choose a group.

First generation – groups 1–10

Groups 1–5 have examples of input and output (in the style of Petlja), groups 6–10 are shorter tasks without examples.

Second generation – groups 11–15

The same topics as groups 1–5, with small changes (in the PDF they are labelled as groups 1–5).

Practice test with solutions

The tasks from "Group 11" (PDF) are written out here as text, with an example, a hint 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 11

Entering numbers until zero

Integers are entered until a zero is entered. Write a program that prints how many numbers were entered, not counting the zero.

Input: every line contains an integer different from zero, except the last one, which contains a zero.
Output: the number of numbers entered.

Example 1

Input

5
12
-5
675
-123
0

Output

5

Example 2

Input

0

Output

0
TASK 02Group 11

The longest winning streak

A basketball team played N games; in each one it won (1) or lost (−1). Find the length of the longest series of consecutive wins.

Input: a natural number N (5 ≤ N ≤ 50000), and then N numbers 1 or −1.
Output: the length of the longest winning streak.

Example

Input

8
1
1
-1
1
1
1
-1
-1

Output

3
TASK 03Group 11

The worst test

During the year a student wrote 5 tests. When giving the final grade, the teacher will ignore the lowest grade (if there are several, only one of them). Read 5 grades and print the average without the worst test, to two decimal places.

Example

Input

5
4
4
5
3

Output

4.50
Common mistakes and tips
  • Set the counter and the sum to 0, and the product to 1 – and do it before the loop.
  • With input "until zero" do not count the zero: first read the number, and only then decide whether it is processed.
  • For the initial value of the largest and the smallest take the first number read (or, if you know the range, a value outside the range).
  • The loop for (int i = 100; i >= -90; i--) goes backwards – watch the comparison sign and the i--.
  • Check the program on the smallest input (e.g. a zero right away) – then the loop should not run even once.

Check your knowledge

Open the quiz

Mini quiz: loops

1. How many times does the body of the loop for (int i = 1; i <= 10; i += 3) run?




2. Which loop is sure to run at least once?




3. What initial value does the variable in which you compute a product start with?




4. What does int s = 0; for (int i = 1; i <= 4; i++) s += i; cout << s; print?