Saturday, March 22, 2014

Java Jokes

(Knock! Knock!)
Who's there?
(a very long pause..)
Java
------------------------------------------------------------
C: I'll tell you a joke.
Java: Ok.
C:(points to NULL) Look.
Java:(Frowns) I don't get it.
------------------------------------------------------------
Here's a program my friend came up with, when I asked him to find the average of 5 numbers using any loop.

public class Average {
    
    public static void main() {
        int a[] = new int[5];
        a[0] = 73;
        a[1] = 42;
        a[2] = 15;
        a[3] = 9;
        a[4] = 1;

        int b=0;
        int sum=0;
        int x=0;
        
        for( x = 0 ; x<5 ; x++ ) {
            sum = a[0] + a[1] + a[2] + a[3] + a[4];
        }
        b = sum / 5;
        System.out.println("The average is: " + b);
    }
}

Hope you enjoyed them!

Wednesday, March 5, 2014

Frame creation

Formation of frames

The following code creates frames of size "n". The frame starts with a symbol and ends with a symbol. These frames are useful for transmitting information. Simple implementation is shown below:

Suppose the input is "Abhishek Baddi" . Let's take the start symbol to be '$' and stop symbol to be '@'. The frames would be:

$Abhi@
$shek@
$ Bad@
$di  @
The code is shown below:
/*
 * ----------------------------------------------------------------------------
 * "THE BURGER-WARE LICENSE" (Revision 42):
 * <abybaddi009@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a burger in return. Abhishek Baddi
 * ----------------------------------------------------------------------------
 */
 
#include<stdio.h>
#include<string.h>

int main() {
    char a[20],STARTSYMBOL='$',STOPSYMBOL='@';
    int i,j,lenOfa;
    int FRAMESIZE=4;
    printf("Enter a string: ");
    gets(a);
    lenOfa=strlen(a);
    i=0;
    while(i<lenOfa) {
        printf("%c",STARTSYMBOL);
        for(j=0;j<FRAMESIZE;++j) {
            printf("%c",i<lenOfa?a[i]:' ');
            i++;
        }
        printf("%c\n",STOPSYMBOL);
    }
    return 0;
}

The output is shown below: