Tuesday, January 11, 2011

Number To Words Algorithm

Number To Words Algorithm

Problem: Given any number of maximum 15 digits number convert it to a string which represents this number in words.
e.g., Input: 231 Output: Two Hundred And Thirty One.

1. Input example: 34541009234120

Find length of input string of digits, if it's a multiple of 3 then do nothing else pad with leading zero's to make it's length an exact multiple of 3 and divide the input number into 5 groups of 3 digits each, so our example input becomes

034 541 009 234 120
   Group No.   5   4   3   2   1

2. Find group of most siginificant digit, e.g., here MSD is 0 and it's group is:

groupNum = length%3 ? (length/3+1) : (length/3);

3. One loop for number of groups of 3 digits each;

4. One loop for each of the 3 digits inside a given group, so the time complexity boils down to linear;

5. Inside inner loop, for any given 3 digits generate a string like for e.g., 541 becomes "Five Hundred And Forty One";

6. Now decide which string would come after this according to the group number e.g., here group of 541 is 4 so "Billion" (from D array given above) would be appended to the string generated in step 5;

7. When outer loop finishes, return the string generated for the whole number.
    e.g., "Thirty Four Trillion Five Hundred And Forty One Billion Nine Million Two Hundred And Thirty Four  Thousand One Hundred And Twenty"

Limitations: 
   1. This algorithm generates words according to US number to words system.
   2. It generates words from numbers till 999 trillion 999 billion 999 million
      999 thousand and 999

Source code (C++):  Available on request.
   Post a comment to mention your email ID. You would receive the source code as
   soon as I get the request.

Cheers! 
Rajendra

A lightweight Image Processing Library in C++

Ever wondered how to work with/on images with little or no development overhead on your own? Here is the answer, the CImg image processing library. CImg stands for Cool Image; it is an open source, written entirely in C++, image processing toolkit which is very easy to use, handy for image processing tasks.

I first encountered this library when I was working on the dissertation, I had to extract frames from a video which I did easily using mpeg2encode and mpeg2decode, after that I had to read these frames (images) into 2D arrays and process them. I started looking for a way to achieve this and ended up with CImg library.
CImg is designed with following properties in mind:

Usefulness: CImg defines classes and methods to manage images in your own C++ code. You can use it to load/save various file formats, access pixel values, display/transform/filter images, draw primitives (text, faces, curves, 3d objects, ...), compute statistics, manage user interactions on images, and so on...

Genericity: CImg defines a single image class which can represent datasets having up to 4-dimensions (from 1d scalar signals to 3d hyperspectral volumetric images), with template pixel types (bool,char,int,float,...). It also handles image collections and sequences.

Portability: CImg is self-contained and thus highly portable. It fully works on different operating systems (UNIX, Windows, MacOS X, BSD, …) and is compatible with various C++ compilers (VC++, g++, …).

Simplicity: CImg is lightweight. It is made of a single header file “CImg.h” that must be included in your C++ source. It defines only four different classes, encapsulated in a namespace. It can be compiled using a minimal set of standard C++ and system libraries.

Just include a single header file in your application or library and you done and fully equipped with amazing capability of this library.

How to use CImg in your C++ program?
You just need to add these two lines in your C++ source code, in order to be able to work with CImg images:

    #include “CImg.h”
    using namespace cimg_library;

I guess this is enough introduction, believe me; you have to use it to realize its power and usefulness. 


Reference: http://cimg.sourceforge.net

Cheers!
Rajendra