Exercise 8

Added Exercise 8 to repository
This commit is contained in:
2025-04-21 19:08:07 +02:00
parent c71d2e026f
commit 950098d35e
8 changed files with 432 additions and 11 deletions

View File

@@ -0,0 +1,69 @@
#+title: Task 1: Reverse Digits
#+author: JirR02
* Task
:PROPERTIES:
:CUSTOM_ID: task
:END:
The goal of this task is to implement a program to read a number and
print its reverse.
You should complete the implementation of the reverse function in
=reverse.cpp=. This function gets an =int= as input parameter, and it
should print the digits of the number in reverse.
The =reverse= function must be implemented recursively (without any
loop). To enforce this, we disallow the use of =for= or =while=.
* Input
:PROPERTIES:
:CUSTOM_ID: input
:END:
The input is a single non-negative =int= number.
An input example:
#+begin_src shell
321231
#+end_src
* Output
:PROPERTIES:
:CUSTOM_ID: output
:END:
For the above input, the output should be:
#+begin_src shell
132123
#+end_src
Note: You are not allowed to use the string library to solve this task!
* Solution
:PROPERTIES:
:CUSTOM_ID: solution
:END:
#+begin_src cpp
#include "reverse.h"
#include <iostream>
// PRE: n >= 0
// POST: print the digits of the input in reverse order to std::cout
void reverse(int n) {
int res = n % 10;
std::cout << res;
int new_int = n / 10;
if (new_int == 0) {
return;
}
reverse(new_int);
}
#+end_src
--------------
Made by JirR02 in Switzerland 🇨🇭