Tuesday 11 June 2013

C/C++ Puzzles: PART - 34

  Consider the two C code fragments given below.

Code 1:

int i;
i=0;
while(i < 10)
{
     // Single Line of Code Here
     printf("\n%d",i);
     i++;
}

Code 2:

int i;
for(i=0; i < 10; i++)
{
     // Single Line of Code Here
     printf("\n%d",i);
}

The Output will be same for both the codes. The Output is shown below.



The Challenge is to obtain different outputs for the two codes by adding the same single line of code in place of the comment. You should not make any changes in the code other than replacing the comment with the same single line of code. There might be different ways to solve the puzzle.


Information Retrieval: Notes

  Recently I prepared some short notes on the subject Information Retrieval. They do not discuss the subject in detail. The notes are useful if you are interested in just a brief introduction. 



Monday 10 June 2013

Python Puzzles: PART - 1

  The following Python Puzzle is taken from the Programming Practice site CodingBat. I have written about CodingBat in the previous post.

The Puzzle is given below.

We have to make a row of bricks that is goal inches long. There are a number of small bricks (1 inch each) and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. You should not use any loops.

The function make_bricks( ) is called as follows,
make_bricks(small, large, goal) : Here small is the number of small bricks, large is the number of large bricks and goal is the length of the row to be constructed.

The Figure Below Shows a Sample Output

Try CodingBat !

  Recently I came across a Programming Web Site Called CodingBat. They provide practice problems on Java and Python. It is a very good site to practice programming if you are a beginner in Java or Python

 Click here to view CodingBat.

Monday 3 June 2013

Sum of Two Complex Numbers in C

  C99 has a header file called <complex.h> which contains functions and macros to manipulate complex numbers. C99 also adds _Complex keyword to deal with complex numbers. But in this small program I have used C89 syntax and double data type to deal with complex numbers.

#include<stdio.h>
int main()
{
    double re1,im1,re2,im2,re3,im3;
    printf("\n\nThe format of Complex Number is a+bi\n\n");
    printf("\nEnter the First Complex number:  ");
    scanf("%lf+%lfi",&re1,&im1);
    printf("\nEnter the Second Complex number:  ");
    scanf("%lf+%lfi",&re2,&im2);
    re3=re1+re2;
    im3=im1+im2;
    printf("\nThe Sum = %lf+%lfi\n",re3,im3);
    return 0;
}



 
Figure Below Shows a Sample Output