Thursday, April 14, 2011

Simple recursion

Hi,

Recursion is one topic in programming which people don't really understand enough to implement in practice. It remains a very elegant programming construct, though underused.

I am writing this post because i used to think that i know recursion well enough to implement, but when i tried to explain the concept programatically i was unable to convert the theory i knew into the practical i thought i knew.

So i sat down and wrote a code to add all numbers till n, and did it using recursion.

 #include <iostream> 
using namespace std;
int addTillN(int n);
int main(int argc,char *argv[])
{
int n;
cout<<"Please enter the number till which you want the summation\t";
cin>>n;
cout<<"the summation is :\t"<<addTillN(n);
return 0;
}
int addTillN(int n)
{
static int y=0;
if(n>0)
y=n+addTillN(n-1);
return y;
}
The code above is a simple example of elegance of recursion, the other option was using plain old for loop, but isn't it just boring :)

Regards,
Ishan

Wednesday, April 13, 2011

IDEA 3G --- How it fared ?

Hi,

I received an sms 2day from idea stating that 3g has been activated on my sim and i can use 100 MB of data valid for 15 days, being the bandwidth lover that i am , i whipped out my 3g modem and set upon the task of testing what idea has to offer.

I connected the modem to a netbook running ubuntu 10.10 netbook remix, on "speedtest.net" i got download speeds of around 2.5 Mbps and upload speeds of .5 Mbps, ping was also better than bsnl's 3g that i use.

To get a better understanding of signal strength and QOS etc I disconnected the modem from the netbook and connected it to my pc.

Mobile partner software indicated the WCDMA signal strength to 99% and HSPA signal strength to 97%, best i have seen so far.



To dig deeper I disconnected the mobile partner and started mdma, negotiated QOS revealed that the tower that i have connected to supports 8 Mbps uplink and 16 Mbps downlink, not up there with the claims of 21 Mbps but certainly better than bsnl which allows downlink upto 2 Mbps only.







But then came the biggest blunder idea could have done after such a good start, they deducted my balance after promising free 100 MB data, i don't think that's a good IDEA :)

So far i have tried all the operators who have launched their services in Maharashtra, bsnl is still the most feasible to use from a price to performance ratio.

Regards,
Ishan

Sunday, April 10, 2011

Begining multithreaded programming in boost

Hi,

With boost dev environment successfully installed, I now am embarking on the path to multi threaded nirvana.

For starters am going to paste the most basic multi threaded code here and the instructions how to build it.

 #include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>


using namespace std;

void workerFunc()
{

cout<<"workerFunc Started"<<endl;
boost::posix_time::seconds sec(3);
boost::this_thread::sleep(sec);
cout<<"workerFunc Finished"<<endl;
}

int main ( int argc, char * argv[])
{

cout<<"main started"<<endl;
boost::thread workerThread(workerFunc);
cout<<"main : waiting for workerThread"<<endl;
workerThread.join();
cout<<"main finished"<<endl;
return 0;

}
The code posted above is the most basic multi threaded code in boost, I will first explain the code and then post the instructions on how to compile it.

The code comprises of two functions.

1. workerFunc
2. main

We will be discussing only those lines which are relevant to threading.

In the workerFunc function two lines are of interest.

1. boost::posix_time::seconds sec(3)
2. boost::this_thread::sleep(sec)

In the first line a time construct named sec is defines with 3 as its value, it is defined in boost/date_time.hpp

The second line makes use of sleep function from boost::this_thread, it pause the execution of the current thread for the time provided as an argument to the sleep function. The handle/reference to the current thread is provided by boost::this_thread.

In the main function , again there are two lines which are of interest.

1. boost::thread wokerThread(workerFunc)
2. workerThread.join()

Line 1 creates a thread named workerThread and passes the name of the function to be executed in the newly created thread.

Line 2 uses the join function to instruct the main thread to wait for the new thread to finish execution.

Now , to compile the program the command is

g++ -o appName main.cpp -L /path/to/boost/lib -lboost_thread

g++ is the c++ compiler bundled with gcc, -0 flag tells the compiler to generate the executable with the supplied name, in our case appName, -L instructs the compiler to add the provided path to its search directories for libraries and finally -l flag is immediately followed by the library name.

The method we have used to supply the function to be executed as thread is not the only one, we can have thread functions which take arguments, have a class with thread function inside it etc etc.

Will explain other methods of creating and using threads using boost libraries as i delve deeper into the library.

Regards,
Ishan

Installing boost on ubuntu - 2

Hi,

In the previous post i described the method to install boost library on ubuntu.
It seems that it installed the header only version of the library.
To get the complete thing installed the command is :

apt-get install libboost-all-dev

And you will be good to go.

Regards,
Ishan

Saturday, April 9, 2011

installing boost on ubuntu 10.10 netbook remix

Hi,

After setting up boost library on my winxp machine, i wanted to install boost on my new netbook running ubuntu netbook remix.

I tried building the library from source as i did on windows but kept running on to some build errors.

Then i went to the traditional way to install from ubuntu packages, but there were some unmet dependency problem which i was unable to solve, then after a bit of searching around i got the exact package name to download.

apt-get install libboost-dev libboost-doc

This did the trick, but it installed the 1.4.2 version of the boost library ,current version being 1.4.6.

Will trying building some cpp code and get back with the results.

Regards,
Ishan

Wednesday, April 6, 2011

A way forward

Hi,

I have read some where , to be a good programmer you should know more than one programming languages, it gives you a better problem solving perspective, and i completely agree.

C++ is the primary development path i have chosen, because its close enough to the system to fiddle with the OS and its suitable for handling large projects.

Python is the second language i prefer to get the work done quickly, its a very fluid language and i love it almost as much as C++, the amount of libraries the come packaged with python is unbelievable, they are not wrong when they say that python has "Batteries Included" :)

For long i have been looking for a new language to study, but i haven't reached a decision yet.
Lets see where i end up.

Regards,
Ishan

Tuesday, April 5, 2011

Using boost static libraries with CodeBlocks

Hi,

Since the past two days , i was trying to run the most trivial multi threading code snippet using boost thread library.

And was unsuccessful, but failure is not always negative , it provided me with the matter for a new blog post to share :)

The issue i was facing was , when i built the project using dynamic boost libraries it worked fine, but as i didn't wanted to have to provide the libraries separately, i tried building the project with statically linked libraries, but no matter what i tried, build always failed with unreferenced symbol error.

Tried re downloading the boost code and recompiling it after a detailed study of bjam build option, but everything failed, then i found , hidden under the pile of junk on the internet a single line of code , and everything was working.

I'll stop being poetic and post the code below.

#define BOOST_THREAD_USE_LIB

Just add this line at the top of the source file using the library and u'll be fine.

As it turned out, the problem was with codeblocks build system, the headers being used were expecting the library to get linked dynamically , atleast this is what i was able to under stand.

So, now i can move along on an adventurous voyage of exploring the boost underworld.
Will keep you all posted.

Regards,
Ishan.