Beginner Exercises – Patient Game

Posted by : MOnsDaR | Sonntag, 22. August 2010 | Published in

Another one from Bazzy on Its original name was “While( user == gullible )”.

The program asks the user to enter any number but a special one. If the user finally enters the special number, a message will put out, praising the user if he was very patient. (If the user has done the game more than 10 times)

Example output:

Do NOT enter 1: 2
Do NOT enter 2: 3
Do NOT enter 3: 4
Do NOT enter 4: 5
Do NOT enter 5: 6
Do NOT enter 6: 7
Do NOT enter 7: 8
Do NOT enter 8: 9
Do NOT enter 9: 0
Do NOT enter 10: 11
Do NOT enter 11: 11

Wow, you were very patient.
You did the game 11 times


Beginner Exercises – Guess the number

Posted by : MOnsDaR | | Published in

I’m currently on the Python train again because a friend of mine wants to learn some basics about programming. Because its not easy to just learn the theoretical part without any  exercises I searched some beginner exercises for him.

In this series of articles I want to present some of the exercises along with a solution. The solutions will be written in Python, but the exercises could be written in any other language too.

So here is the first one:

Guess the number…

Found in an article from bazzy at under its original name “Bracketing Search”

The program chooses a number between 0 and 100. The user then needs to guess that number. After finding the right one, a message will show the number of guesses the user needed. If the user guesses wrong, he will be told, if he was higher or lower than the number.

Example output:

Try to guess the secret number. It is in range 0 to 100.
Enter a number: 50
Too low...
Enter a number: 75
Too low...
Enter a number: 87
Too high...
Enter a number: 81
Too low...
Enter a number: 85
Too high...
Enter a number: 83
Got it, the number was 83
You needed 5 guesses to get it.
Press any key to continue . . .


Still alive

Posted by : MOnsDaR | Freitag, 9. Juli 2010 | Published in

Due to the good weather and having a lot of work to do, the blog was quite inactive for the last weeks. I don’t want to promise that its getting better, but i’ll try.

The attentive reader might have noticed, that i’ve decided to write SergejFerlich in English from now on. This way a lot more readers could profit from the content of this blog. Perhaps i’ll translate the old posts too.

Another news is my reactivated twitter-account monsdar. I probably won’t spend much effort on twittering, but it is useful for getting in touch with serveral people.

So have a nice summer,

Sergej Ferlich

CMake: Find Thirdparty like QT

Posted by : MOnsDaR | Donnerstag, 20. Mai 2010 | Published in

Almost every project depends on Thirdparty software like Boost or QT. For building those projects, the compiler and linker need to know where they could find includes and which libraries they should link. But how should the programmer should know where the user has installed his Thirdparty-components?

CMake provides the command find_package().

This command uses special find-scripts called findPackageName.cmake and fills standard-variables which then give information about where and if libs ands includes are installed. Find-scripts are located at /usr/share/cmake/Modules on Linux systems. CMake already brings with scripts for the most used packages. A lot of components install their own scripts.

There are additional parameters for find_package() which allow the programmer to just search for certain versions or subcomponents of the Thirdparty. There also is a flag REQUIRED which will cancel the build-process if the requested Thirdparty is not installed.

Because code says more then thousand words:

find_package(Qt4 4.4.3 COMPONENTS QtCore QtGui QtXml REQUIRED ) include(${QT_USE_FILE})
add_executable(myexe main.cpp)
target_link_libraries(myexe ${QT_LIBRARIES})


The source searches for the QT4-components QtCore, QtGui and QtXml in version 4.4.3 and cancels, if they are not installed. The variables QT_USE_FILE and QT_LIBRARIES will be filled and could be used for the further build-process. There are a lot of other variables besides that two. For a complete list take a look at the CMake documentation.

KDevelop 4.0: Patch Review

Posted by : MOnsDaR | Mittwoch, 19. Mai 2010 | Published in

This article already gives a short introduction on how to create patches with SVN and what they’re good for. Now I’ll talk about how to review and apply patches.

The command svn diff creates a textual summary of changes in a specific revision. Even with small changes, those textfiles aren’t very easy to read. KDevelop with its builtin Patch Review tries to solve that problem.


After selecting the base (mostly thats our project), the user chooses a .diff-file which contains the changes of the patch. A dialog shows files affected by it. By doubleclicking specific files, the code will be displayed in the editor.


The screenshot above shows a red X, marking that the light-blue area has been deleted in the patch. By clicking that X the change will be accepted.

This way a patch could be reviewed a lot easier. It’s simply viewing all files and choosing if a specific change should be accepted or not. After reviewing the whole patch, the button “Finish Review” could be clicked, which applys the patch.

Patches mit Subversion

Posted by : MOnsDaR | | Published in

Während der Arbeit mit Projekten anderer (seien es Arbeitskollegen oder OpenSource-Projekte) findet man ab und an Bugs, die sich mit Zugriff auf den Sourcecode leicht selbst beheben lassen.
Doch wie kommuniziert man diese Änderungen nun? Oft hat man keinen Schreibzugriff aufs SVN und selbst wenn, möchte man nicht einfach im Code des Kollegen rumwurschteln.

Subversion bietet die Möglichkeit, alle Änderungen in eine diff-File zu exportieren. Dies ist eine einfache ASCII-Datei die zusätzlich zu den einzelnen Änderungen jeder Datei auch die Revision enthält, mit denen diese gemacht wurden. Sogut wie jeder bessere Texteditor erkennt .diff und kann Syntax-Highlighting auf diese Dateien anwenden. Ein Programmierer, der diese Datei erhält, kann alle Änderungen nachvollziehen und anwenden.

Im einzelnen sehen die Schritte so aus:
  1. Zunächst wird das Projekt aus dem Repository ausgecheckt und alle Änderungen an den Dateien vorgenommen.
  2. Mit dem Befehl svn diff erhält man eine Auflistung aller Änderungen. Der Befehl svn diff > myfirstpatch.diff schreibt diese Auflistung unter Linux in eine Datei.
  3. Zusammen mit weiteren Anmerkungen (vielleicht per Mail oder in einem Bugreport) sendet man die Patch-Datei nun an den anderen Programmierer.
  4. Dieser sollte sich die .diff genau ansehen. Natürlich können Patches auch schädlichen Code beinhalten. Unter Kollegen wird dies nicht vorkommen, aber bei anonymen Patches in Opensource-Projekten ist die Wahrscheinlichkeit schon höher.
  5. Ist der Patch in Ordnung, kann dieser unter Linux mit dem Befehl patch -p0 -i myfirstpatch.diff angewandt und committed werden. Der Befehl sollte im selben Ordner ausgeführt werden, in dem der Patch auch erstellt wurde (im SVN-Root). Der Parameter -p0 stellt sicher, dass alle Dateien gefunden werden. Mit -i wird die Patchdatei angegeben (kurz für input).
Unter Windows wird es einen ähnlichen Weg geben, dies konnte ich leider nicht testen.

C++: Plugins the QT way

Posted by : MOnsDaR | Freitag, 7. Mai 2010 | Published in

Compared to modern languages like C# or Java, C++ lacks in support of dynamically loading code at runtime. It is not easy to implement a portable plugin-framework, but it is possible. Luckily there are frameworks available which make such a functionality possible.

Nokias QT-Framework is one of the most active and best maintained frameworks for C++. Besides the famous GUI-functionality are some neat features like reimplementations of the std-features Lists, Strings, Maps etc. It also has a plugin-framework integrated. For testing purposes, I wrote the MediaMasher plugin-architecture in C++ with QT-Plugins:


(click on image for resize)

The finished project is uploaded to a git-repository and could be downloaded HERE.

It includes a library which adopts the above UML-architecture. Additionally a binary using the lib with a hardcoded sample-source is included. The path points to a systemsound in Ubuntu 10.04. It could be easily changed in /test/TestMain/main.cpp

For building the project CMake is needed. It was another goal for me to demonstrate how QT and QPlugin could be included in such a build-environment.

Unfortunately there is not much documentation about QPluginLoader and the related mechanisms. Besides an official Plug and Paint example directly in the QT-documententation is just the class reference. There isn’t much info in blogs or by projects using this framework.

Finally I’ll show the POCO-Framework as an alternative to the QT-framework. The ClassLoader-component could be interesting for commercial projects which could not effort a QT developer licence (commercial licences start at 3000€ for QT)

Just ask if there are any questions about the example-code or if I should give some deeper explanation.