Test: functions (C++)

Test 7 of 8 · Tests

A test on functions: writing a function with parameters and a return value, logical functions (bool), functions that work with the digits of a number, and passing parameters by reference. This page is new – the tasks are in two groups, with solutions.

Before you start: review the lessons Functions in C++, Functions in C, and then do the tasks from the page Functions – 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

  • the function header: return type, name and parameters
  • return – returns a value and ends the function immediately
  • logical functions (bool isPrime(int n)) and using them in if
  • working with the digits of a number (% 10 and / 10) inside a function
  • the difference between passing by value and by reference (int &x)

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

Group 1

TASK 01Group 1

Prime numbers up to N

Write a function bool isPrime(int n) that checks whether a number is prime, and use it to print all prime numbers from 2 to N.

Example

Input

20

Output

2 3 5 7 11 13 17 19
TASK 02Group 1

The largest digit sum

Write a function int digitSum(int x). Read n natural numbers and print the one with the largest digit sum (if there are several – the first one).

Example

Input

4
123 99 405 18

Output

99
TASK 03Group 1

Simplifying a fraction

Write a function int gcd(int a, int b) (Euclid's algorithm). Read the numerator and the denominator and print the simplified fraction in the form a/b.

Example 1

Input

12 18

Output

2/3

Example 2

Input

7 5

Output

7/5

Group 2

TASK 04Group 2

Number of combinations

Write a function long long factorial(int n) and use it to compute the number of ways to choose a team of k from n students: C(n, k) = n! / (k! · (n − k)!). It holds that 0 ≤ k ≤ n ≤ 20.

Example 1

Input

5 2

Output

10

Example 2

Input

20 10

Output

184756
TASK 05Group 2

Palindromes in an interval

Write a function bool isPalindrome(int x) that checks whether a number reads the same from left to right and from right to left. For a given interval [a, b] print all such numbers and how many there are.

Example

Input

10 50

Output

11 22 33 44
Total: 4
TASK 06Group 2

Sorting three numbers (passing by reference)

Write a function void swapValues(int &x, int &y) and a function void sort3(int &a, int &b, int &c) that arranges three numbers from the smallest to the largest. Read three numbers, sort them by calling the function and print them.

Example

Input

7 2 5

Output

2 5 7
Common mistakes and tips
  • A function must be written (or declared) before the place where it is called.
  • Parameters without & are copies: a change inside the function does not change the variable in main.
  • In a function that should return a value, every branch must end with return.
  • Move the same job that is repeated several times (digit sum, prime check) into a function – the program is shorter and easier to check.

Check your knowledge

Open the quiz

Mini quiz: functions

1. What does a function of type void return?




2. After the call f(x), where void f(int a) { a = 5; }, what is x if it was 2 before?




3. How do you write a parameter that the function can change in the caller?