Saturday, April 25, 2009

Spot the bug! - Corrected

Oye me hearties!

Your dear friend, who churns out intriguing, comical and just embarrassingly absurd posts has done it again...

Find the bug in the code, written by murali, below...Note that it is a logical bug, and that the code compiles just fine...You can try compiling it once if ya wants...

Oh I've replaced the actual 'get_rtt' function to spare you homo sapiens the gory details...

The 'main' function is used to calculate the Round Trip Time (RTT) for a packet in TCP networks, and see how many times my calculation differs from my previous calculation.

This is all u guys need to know... But thats not the point...the bug is really funny :D

******************************************************************
#include stdio.h
#define alpha 0.15

int main()
{

long i =0,count=0;

double sample, estimate;

long timeout;
estimate = sample = 0.0;

for ( i = 0; i < 1,000,000 ; i++ )
{
sample = get_rtt();

estimate = sample * (alpha) + estimate * (1-alpha);

if ( sample > estimate ) count++;

timeout = 2 * estimate;
}

printf("The number of unnecessary re-transmissions is %d\n", count);
}

******************************************************************

5 comments:

Unknown said...

Looks like blogger screwed up the formatting and in the process messed up the code. Read your post and confirm whether the code is what you intended

Murali said...

Che didn't anticipate stupid blogger interventions...

Thanks for the alert srini...post has been corrected.

Unknown said...
This comment has been removed by the author.
Unknown said...

Ok - I missed out the initialization (hence removed last comment)

Count will be 1,000,000 always right? 'estimate' should be initialized to 'sample' the very first time. Otherwise, 'estimate' will always be less than sample

Murali said...

Doesn't matter what i "initialize" the var 'estimate' to... For every iteration inside the loop (for the first time also when i=0), both sample and estimate get initialized...sample is init by 'get_rtt' and estimate is init by the expression there...alva?


Anyways, thats not the bug...