LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Exercise 4 of 4

Functions and Default Arguments Function Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

C++ programs built from functions: recursion, overloading, references and returning several values. Write each one yourself first, then open the solution – every solution was compiled and run, and the output shown is the real output.

Question 1 Easy

Write a recursive gcd function (Euclid's algorithm) and use it to compute the LCM. Read two numbers and print both.

Question page & discussion
Question 2 Medium

Compute xn in O(log n) multiplications using recursion: square the half power, and multiply once more for odd n. Also compute it modulo 1,000,000,007.

Question page & discussion
Question 3 Easy

Write overloaded area functions for a circle (radius), a rectangle (length, width) and a triangle (three sides, Heron's formula), and print each area to two decimals.

Question page & discussion
Question 4 Easy

Write a function that rotates three integers left (a gets b, b gets c, c gets a) using reference parameters, and apply it three times.

Question page & discussion
Question 5 Medium

Write a recursive function that checks whether a string is a palindrome, ignoring case and non-letters, and test it on several phrases.

Question page & discussion
Question 6 Medium

Read n numbers into a vector and write a function that returns their minimum, maximum, mean and median in one struct; unpack it with structured bindings.

Question page & discussion
Question 7 Medium

Write a recursive Fibonacci function that remembers computed values in a static vector, count the calls, and print fib(50) and fib(90).

Question page & discussion
Question 8 Medium

Read the number of disks and solve the Tower of Hanoi recursively, printing only the total number of moves and the first five moves.

Question page & discussion
Question 9 Hard

Store the operations + − * / ^ in a map from operator character to std::function, then evaluate expressions from the input, reporting unknown operators and division by zero.

Question page & discussion
Question 10 Medium

Count the ways to climb n stairs taking 1 or 2 steps at a time, with a recursive function that uses a memo array passed by reference; print the result for n = 1 to 10 and n = 45.

Question page & discussion
All exercises doneBack to Functions and Default ArgumentsRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links