Exercise 8
Added Exercise 8 to repository
This commit is contained in:
69
Informatik_I/Exercise_8/Task_1/README.org
Normal file
69
Informatik_I/Exercise_8/Task_1/README.org
Normal 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 🇨🇭
|
Reference in New Issue
Block a user