Dumbest Story, you would’ve ever read

Everything he could hear was nothing,but a nightingale sing. And notably, he was in that typical Friday Last Hour Class of Compiler Design.
For all those who nod of how a nightingale sang in a friday class: Its coz he was fast asleep, into the world of dreams.

And Aawww! that nightingale voice turned into a typical, not so sweet voice, quiet familiar one. It took a minute to get his sense and realize it was actually the lecturers voice and he was not actually was in a wonderland!

So finally when he managed and stood up, All he saw through that blurred view was- A board, A Man standing beside with a Chalk, And no nightingale anywhere around, and a snippet –

for(i=1;i<50;i++) {
c=i*7;
}

By the time he realized that all he had to do was optimize the snippet by removing the * from the expression, without modifying the result.

So lets come to senses, What we do now!

– The code does nothing, except finding the multiples of 7 upto 49*7
– There are three different ways you could handle this
– One by how we learnt tables, our sweet little shortcut method when we wrote them at 1st std, add 7 every step
-So that will lead to something like this

for(i=0;i<50;i++) {

   c=c+7;

}


-That will do pretty fine! but if mr.Nightingale looks for a solution without actually modifying the strategy itself,to eliminate * , a double loop will do!(thanks to lucia, for making me change this!)


for(i=0;i<50;i++) {
sum = 0 ;
for(j=0;j<7;j++) {// j is just a namesake variable to produce 7 iterations
sum = sum + i;
}
}

– This eliminates *, but what if mr. Nightingale looked to reduce the code significantly! Lets take this

for(i=1;i<343;i+7) {
i;
}

-Its that simple!

Now that poor guy couldn’t actually go and explain all these methods! So he sat and snored off to his neverneverland! To his nightingale symphony!

Advertisements

7 thoughts on “Dumbest Story, you would’ve ever read

    1. Lucia, That’s simply a useless variable to provide 6 iterations in that loop, I Think you made me rewrite the code! Let me Explain you why –

      The solution without change in logic for i*7 is actually i+i+i…7 times,
      Let’s put it this way –

      for(i=0;i<50;i++) {
      c = i+i+i+i+i+i+i
      }

      So anyway the compiler that parses this expression has to convert it into three address code and that obviously is more a complex task. So why dont we make it a bit more easy for the compiler by looping it and getting the expression shorter?


      for(i=0;i<50;i++) {
      sum = 0 ;
      for(j=0;j<7;j++) {// j is just a namesake variable to produce 7 iterations
      sum = sum + i;
      }
      }

      Thanks for correcting me lucia!

      1. Its a useless var to provide 6 iterations ,
        yeah , ur rit , bt y ur using that useless var
        the main concept is to optimize the code , rit

      2. Hi ,
        Its a useless var to create 6 iterations ,
        then , y ur using the var in that func ,
        the main concept is to optimize the pgm rit
        , wat is the use of that 7 namesake iterations !!

      3. Yeah Lucia, we have to optimize the program, right.
        Imagine without the iterations(The var J is namesake ,not the iterations!!).
        We have to write it as ” c = i+i+i+i+i+i+i ” .
        To eliminate , these many ‘+’ operations, we can put it inside a loop right?
        Thats just what I did, you get me?
        Maybe you wonder why we use so many loops and iterations to optimize the simple code. Its because, in the second snip, I want to eliminate ‘ * ‘ without changing the logic by itself.

Speak:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s