Friday, 29 March 2013

C/C++ Puzzles: PART - 31

Printing the Map of India

  Yes, Yet another obfuscated C Program. But this one is interesting. The program is printing the map of India. I got this program from stackoverflow. But the name of the author was not specified. But he deserves full credit for this brilliant piece of code. The program is shown below.

#include <Stdio.h>
main()
{
    int a,b,c;
    int count = 1;
    printf("\n\n");
    for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
    TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
    UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
    NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
    HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
    T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
    Hq!WFs XDt!" [b+++21]; )
    for(; a-- > 64 ; )
    putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
    printf("\n\n");
    return 0;
}

Click here to download the C Program.


Figure Below Shows the Output


The program is obfuscated to hide the working from casual onlookers. But the version given below might help you understand the working of the program.


#include <stdio.h>

int main (void) 
{
    int a=10, b=0, c=10;
    char* bits ="TFy!QJu ROo TNn(ROo)SLq 
    SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}
    TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn 
    SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
    a = bits[b];
    printf("\n\n");
    while (a != 0) 
    {
        a = bits[b];
        b++;
        while (a > 64)
        {
            a--;
            if (++c == 'Z') 
            {
                c /= 9;
                putchar(c);
            } 
            else 
            {
                putchar(33 ^ (b & 0x01));
            }
        }
    }
    printf("\n\n");
    return 0;
}


Click here to download the C Program.

The string contains the run length encoding of the map to be printed. The run length encoding tells us how many times to print each character. The statement putchar(c) prints the newline character and the statement putchar(33 ^ (b & 0x01)) prints the character !.

Thursday, 28 March 2013

C/C++ Puzzles: PART - 30

The "goes to" Operator in C/C++

What is a "goes to" operator in C/C++? Well most of you might not have heard about it. I have come across this operator (-->) in a C/C++ puzzle recently. The program is shown below.

#include<stdio.h>
int main()
{
   int x=10;
   while(x --> 0)
   {
       printf("%d ", x);
   }
   printf("\n\n\n");
   return 0;
}


The Figure Below Shows the Output

The loop iterates until x reaches the value 0. Well the operator is not a standard operator in C. It is just post decrement ( -- ) followed by the greater than operator ( > ).To understand the working of the so called "goes to" operator consider the loop given below.

while(x--  >  0)
{
    printf("\n%d",x);
}

Also read the post on The Rule of Maximal Munch in C/C++ to understand the compilation process of operators. 

Tuesday, 26 March 2013

C/C++ Puzzles: PART - 29

Accessing the Stack Memory from C Program

  Recently I have seen this puzzle in which you have access the stack memory from the C Program to obtain a solution. Consider the code given below.

#include <stdio.h>

void change( )
{
        
}

int main( )
{
        printf("Hello\n");
        change( );
        printf("Hai\n");
        printf("How are you?\n");
        return 0;
}

The expected output of the program is shown below

The problem asks us to change the Output to the following form by adding code in the function change( ) alone. You are not supposed to make any change in the main( ) or any other part of the Program.


One simple yet ingenious solution was to add the following code in change( ).


void change( )
{
        printf("Hai\n");
        exit(0);
}


  But the expected solution for this puzzle is obtained by changing the return address of change( ) stored in Stack Memory. There are no functions in C which allows you to do this by directly accessing the stack memory. But the following code can achieve this by finding the location of stack memory from the address of  the variable x declared inside change( ). The code is shown below and the output obtained for this program is the required one.

#include <stdio.h>

void change( )
{
        char x;
        /* skip local variable by changing the return address in the Stack Memory*/
       *(long*)(&x + sizeof(char) + sizeof(long*)) += 10;
} 

int main( )

{

        printf("Hello\n");
        change( );
        printf("Hai\n");
        printf("How are you?\n");
        return 0;

}


Click here to download the C Program.


WARNING!!!
Some Operating Systems might give you segmentation fault on this C Program.

Friday, 22 March 2013

C/C++ Puzzles: PART - 28

Sizeof Structure Variables

  Consider the code given below. What is the Output? 

struct outer_str 
{ 
    struct nest_str1 
    { 
         int mem1; 
    }; 
    struct nest_str2 
    { 
         int mem2; 
    }nest_var2; 
}outer_var; 

int main( ) 
{
      printf("\n\nThe size of structure variable outer_var is %d\n\n",sizeof(outer_var)); 
      return 0;
}
Click here to download the C Program.
The expected Output is 8 bytes. But the actual Output is 4 bytes. Why?

Figure Below Shows the Output

  The inner structure nest_str1 does not have any structure variable associated with it. So there is no memory allocated for this structure. Thus the total memory size of the outer structure variable is just 4 bytes. The program given below will show a size of 8 bytes.

struct outer_str 
{ 
    struct nest_str1 
    { 
         int mem1; 
    }nest_var1; 
    struct nest_str2 
    { 
         int mem2; 
    }nest_var2; 
}outer_var; 

int main( ) 
{
      printf("\n\nThe size of structure variable outer_var is %d\n\n",sizeof(outer_var)); 
      return 0;
}

Monday, 11 March 2013

Java Development Kit (JDK)

  Java Development Kit ( JDK ) contains a collection of programming tools for Java Developers. Here in this post I am discussing about a few useful tools in JDK.
  • appletviewer - appletviewer runs Java applets without the help of a java enabled web browser.
  • javac - javac is the Java Compiler which produces Byte code from the Java Source code.
  • java - java is the Java Interpreter which runs the Byte code produced by the Java Compiler javac.
  • javah - javah produces header files to use with native methods.
  • javap - javap is the Java Disassembler which enables us to convert Byte code files into a program description.
  • javadoc - javadoc creates HTML format documentation from Java Source code files.
  • jdb - jdb is the Java Debugger.

Differences between Java and C++

  In this post I am discussing about the main differences between Java and C++. The subject is not covered thoroughly.
  • Java is a pure object oriented language, while C++ is basically C with object oriented extension.
  • Java does not support operator overloading.
  • Java does not have template classes as in case of C++.
  • Java does not support multiple inheritance of classes. To achieve multiple inheritance Java uses interface.
  • Header files are not there in Java.
  • There are no global variables in Java.
  • Java does not use explicit pointers.
  • In Java destructors are replaced with finalize( ). 

Sunday, 10 March 2013

C/C++ Program to Find the Sum of Perfect Numbers

  A perfect number is a positive integer that is equal to the sum of its proper positive divisors that is, the sum of its positive divisors excluding the number itself. For example 28 is a perfect number. The proper positive divisors of 28 are 1, 2, 4, 7 and 14 which adds up to 28. In this post I have added C/C++ Programs to print  the first N perfect numbers. The sum of these perfect numbers is also found by the program. For large values of N the program will not generate any output.

 Figure Below Shows a Sample Output