Sunday, March 18, 2012

Introduction to C++

C,C++ are the basis for all the high level languages today!!!
C++ is an intermediate level language

Different levels of languages 

Low level languages are those languages which are closer to the hardware and can manipulate memory much more easily than high level languages.
example - assembly language

High Level Language are machine independent programming languages, they cannot manipulate the memory easily but they give a good and interactive foreground to the program.
example - java,python, perl

Intermediate language are those that have features from both the languages.
example - C,C++.

Let us start with C++

The first and the foremost important thing that we require is a compiler. A compiler is something that converts your code into machine language and makes it executable.
There are many compilers in the market but the one's that are pretty well known are Code::Blocks for windows and gedit for Linux or XCode for mac users.

Now, to start with the C++ coding, each line that we write in the editor is a command to the system to perform certain function.
This collection of commands makes a source code.
a command consists of keywords, variables and instructions.

Keywords are predefined words that the language uses and are not allowed to be used as variable names. for example - if, int, char, for, while - these are all keywords and these words have some significance in the library of the compiler and a user cannot use these names as  variable names.

Variables are locations in the memory of the computer created by the user where the user can store data and from where he can retrieve data.
a variable is always preceded by a data type. 

different data types -
int - to store a numeric or integer value
char - to store a letter
String - to store a sentence (not present in C)
float - to store a decimal value
boolean - store true or false(not present in C)
Double - to store large float values
long - to store large int values.

To perform certain inbuilt functions like printing value on the screen or to take values from user or do complex mathematical function like square and square roots there are inbuilt programs created in the library of C and before we write the simplest code it is mandatory to call a few of these library files.
The example below will show you how to call them

There are two parts to a program i.e Instructions and comments
Instructions are used to ask the program to perform a certain function.
anything and everything written on the editor is considered as an instruction until it is preceded by "// " or "/*".

 //line comment
/*block comment or multiline comment */

Comment is something that a programmer writes for future reference, so that when he reads the code sometime later he knows what a few statements mean and what do they do.

here is a simple example to start with

using namespace std;

int main(){
    int a,b;      //This is a comment. Here a,b are variables, int is a data type
    int c (6);   // constructor initialization. here 6 is the initial value of c
    int d = 5;  //here initial value of d is 5
    int result;
    string new1,new2;
    string new3;
    new1 = " this is my first program";// initializing values to new1 and new2
    new2 = " this is my second string";
    cout<<"Please enter the first number"<<endl;
    cout<<"please enter the second number\n";
    cout<<"enter the new string"<<endl;
    result = a + b;
    cout<<"final result = "<<result<<"\n the value of c is = "<<c<<endl;
    return 0;

in the above code
#include<iostream> is a header file used to include predefined program to print out on the screen and take value from the user.
to print something on the screen we write
cout<<"Print on the screen";
 to take values from the user
cin>>variable name ;

#include<String> is used to as we are making use of String as a data type.

endl is used to go to the next line.

2 strings can be concatenated using "+" sign.

using namespace std - if we want to use cout and cin directly we need to specify this else we'll have to type as follows


i will explain you the significance of :: later. 

Friday, March 16, 2012

Tower of Hanoi

Code for Playing Towes of Hanoi-
A very Interesting game, which we generally played when we were kids or when we are with kids.
about the game - Tower of Hanoi
The code has been written in Python.

"""Code By Arpit Jain """

import random
import copy
import math

def int2baseTwo(x):
    """converts interger value to base of two and represents in binary"""
    binary = [] # make an empty list named bunary.
    while (x>0):
        value = x % 2  # Find the remainder on dividing by 2 as we need binary values.
        binary.append(value)  # append the binary value in the list
        x =  x//2  # Integer divide the number by 2.
    return binary
def randLocationR(n):
    """Creates a list of random numbers between 0 and 2 inclusive and we call it rollcall"""
    rollcall = [] # Create an empty list called roll call to return a list of random numbers between 0 and 2 inclusive.
    for i in range(0,n):
        tower = random.randint(0,2) # creates n random numbers between 0 and 2 inclusive.
        rollcall.append(tower) # adds the random number generated to the list.
    return rollcall

def rollcall2List(R):
    """converts the roll calls that we get in the above functions into a combined list of 3 lists of each tower and its elements"""
    tower1 = []     # creation of blank lists
    tower2 = []
    tower0 = []
    length = len(R)
    for i in range(0,length):   # loop to run form start to length of the list so that we can check for each and every elememt
            tower0.append(i)  #if the  element is 0 then we add the value of i to tower zero similarly for the others.
            length -= 1
            length -= 1
            length -= 1
    return [tower0,tower1,tower2]

def isLegalMove(L,a,b):
    """ this function returns a boolean value whether the move is legal or not i.e larger disc will not be allowed to go above smaller disc"""
    if(a == b):         #checks whether a and b are same if they are same then retuen false as we cannot move the disc from a post onto itself
        return False
    elif (len(L[a]) == 0):  # checks whether the length of the post a is zero or not i.e whether it has any any disc on it or not if it doesnt have any list then we can move nothing hence it should return false
        return False
    elif(len(L[b])==0): # checks for the length of the post B and returns true even if the post contains no element as it is the right condition.
        return True
    elif (L[b][0] > L[a][0]): # we movr from the post A to post B if B is zero which we checked above or if the top disc at B is larger than the disc we are moving from A else we return False
        return True
        return False

def makeMove(L,a,b):
    """Moves the disc from one tower to another if the move is legal"""
    legal = isLegalMove(L,a,b)  # calls the function isLegalMove to check if the move made is legal or not
    if (legal):
        L[b].append(L[a][0])  # if the move is legal then moves
        L[a].remove(L[a][0])  # removes the element from list a that has been transferred to tower b.
        L[b].sort() # sorts the list in tower b
        return L
        return False

def printList(L):
    """This function prints the hanaoi game in the format that we want"""
    List1 = [[],[],[]]
    length1 = len(L[0])# we find the lenght of the 3 lists inside L
    length2 = len(L[1])
    length3 = len(L[2])
    final = [length1,length2,length3]   # we add all the 3 values to a new list
    maximum = max(final)# find the maximum out of the 3

    for i in range(3):
         for j in range(maximum-len(L[i])):     # this function finds the difference between the length of the list inside L and the maximum value and appends that many number of spaces so that all the lists become of equal length.
         List1[i] = List1[i] + L[i]

    for i in range(maximum):
        print(str(List1[0][i]).rjust(6),str(List1[1][i]).rjust(6),str(List1[2][i]).rjust(6))# This function is used to print the list. rjust is used to give space between the two values that we have to print

    for i in range(25):
        print('-',end ='')# This loop is used to print the line. We make use of end to indicate that print on the same line until the loop is finished and end bu space.

def list2Rollcall(L):
    """converts list into rollcall i.e checks each number and returns the tower on which it is placed"""
    rollcall = L[0]+L[1]+L[2]   # concatinating all the 3 lists with each other.
    rollcall.sort()     # Sorting the list and arranging it in ascending order.
    rollcall.reverse()  # reversing the order of the sorted list
    for i in range(0,len(rollcall)):  
        if(rollcall[i] in L[0]):  # we search for each element in the new list within the 3 lists and checks to which list does it belong.
           rollcall[i] = 0
        elif(rollcall[i] in L[1]):
           rollcall[i] = 1
        elif(rollcall[i] in L[2]):
           rollcall[i] = 2
    return rollcall

def rollcall2SA(R):
    """converts rollcall into sierpinski's address"""
    p =[]   # empty list which is used to assign numbers depending whether the len of the rollcall is even or odd.
    s = [] # represents sierpinski's address
    i = 0
    if(len(R)%2 == 1):     # checking if the list length is even or odd.
        p = [0,1,2]         # if odd
        p = [2,1,0]         # if even
    while(i<len(R)):                   # we run a loop uptil the length if the input i.e R
        if (R[i] == p[0]):      # we compare the value in R at ith position with the value in p at 0th position to check if they are same
            s.append(0)         # if they are same then we append zero in s otherwise we check the next conditions
            temp = p[1]         # now we need to swap the remaning 2 elements from the list P hence we make use of temp as a temporary variable.
            p[1] = p[2]         # we exchange the position of the elements in the list and store the new P
            p[2] = temp
            i = i+1
        elif (R[i] == p[1]):    # here we do the same as above and compare the 1st element of p with ith value of R
            temp = p[0]
            p[0] = p[2]
            p[2] = temp
            i = i+1
        elif (R[i] == p[2]):    # perform the same function as above.
            temp = p[1]
            p[1] = p[0]
            p[0] = temp
    return s                    # return s

def SA2rollcall(SA):
    p = []   # empty list which is used to assign numbers depending whether the len of the rollcall is even or odd.
    R = [] # represents empty roll call list
    i = 0
    if(len(SA)%2 == 1):     # checking if the list length is even or odd.
        p = [0,1,2]
        p = [2,1,0]
        if (SA[i] == p[0]): # we perform the same function as the function above only in this case we find the location in p which corresponds to the element in p and then return that location index
            temp = p[1]
            p[1] = p[2]
            p[2] = temp
            i = i+1
        elif (SA[i] == p[1]):
            temp = p[0]
            p[0] = p[2]
            p[2] = temp
            i = i+1
        elif (SA[i] == p[2]):
            temp = p[1]
            p[1] = p[0]
            p[0] = temp

    return R

def SA2TA(SA):
    """converts Sierpinski's address to ternarty address"""
    TA = [0,0,0]    # intialize a new list to 3 elements with zero each as we need to give the output of 3 elements.
    n = len(SA)
    for i in range(0,n):
        if(SA[i] == 0): # if the ith element of the list SA is 0 then we use the formula - (0,1,1)x2^(n-1)
            TA[0] += 0*(2**(n-i-1)) # we have to add TA to itself as we need addition of the all the elemnts that get entered there.
            TA[1] += 1*(2**(n-i-1))
            TA[2] += 1*(2**(n-i-1))
        elif(SA[i] == 1):  # if the ith element of the list SA is 0 then we use the formula - (1,0,1)x2^(n-1)
            TA[0] += 1*(2**(n-i-1))
            TA[1] += 0*(2**(n-i-1))
            TA[2] += 1*(2**(n-i-1))

        elif(SA[i] == 2):# if the ith element of the list SA is 0 then we use the formula - (1,1,0)x2^(n-1)
            TA[0] += 1*(2**(n-i-1))
            TA[1] += 1*(2**(n-i-1))
            TA[2] += 0*(2**(n-i-1))
    return TA

def TA2SA(TA):
    """converts Ternary Address back into Sierpinski's Address"""
    total = TA[0]+TA[1]+TA[2]       # Total consist of addition of all the elements or values in the TA
    power = int2baseTwo(total)  # We send the total to the int2baseTwo function to find out power of 2 and then we subtract
    SA = []     # Empty SA list
    temp = TA   # Storing the TA in a new varibale called Temp
    mod = [0,0,0]   # we initialize the list for mod with 3 elements all zero's as we need to store the addition of the elements in this list hence it is initialzed to zero.
    i = 0
    for i in range(0,len(power)-1):
            temp[0] = temp[0]//2    # We do Integer division of the value in temp[0] by 2 and then in the next step we mod it by 2 and store the result in mod.
            temp[1] = temp[1]//2    # we do Integer division by 2 as we need to know how many 2's it has.
            temp[2] = temp[2]//2

        mod[0] = temp[0]%2
        mod[1] = temp[1]%2
        mod[2] = temp[2]%2

        if( mod == [0,1,1] ):   # now we have the value of mod. we check whether the current value if mod and append 0,1 or 2 accordingdly in the emplty list SA.
        elif(mod == [1,0,1]):
        elif(mod == [1,1,0]):
    return SA

def reduceTA(A,B):
    """ It takes in 2 ternary addresses and removes the large discs with the same value at the same position from both the SA's. But the large discs should be in a proper order.
    If 7 is the highest and is at the same location but 6 is at different locations and then again 5 is at the same location then we remove only 7 and not 5 because 6 is not in the same location."""

    SA= TA2SA(A)    #Converts the Ternary Address into sierpinski's
    SB = TA2SA(B)
    length1 = len(SA) #we find the length of the SA formed as we need the loop to run to this length.
    k = 0

    for i in range(0,length1):
        if(SA[i]==SB[i]):   #we compare the two values of SA and SB and if they are same then we change that value to "Equal" stating both are same
            SA[i]= 'Equal'
            SB[i]= 'Equal'
        elif(SA[i]!=SB[i]): # if the two are not at the same location then we break the loop as if the higher disc is not at the same location then we dont worry about the other discs.
    while(k<length1): # Now we append the values which are not Equal into another lists SA1 and SB1. we only append the values which are not 'Equal'
        if (SA[k]!= 'Equal'):
        if (SB[k]!= 'Equal'):
        k = k+1

    TA1= SA2TA(SA1) # convert SA back into TA
    TB1= SA2TA(SB1)
    for i in range(0,3): # changing the original Array as we donot return anything explicitly.
       A[i] = TA1[i]
       B[i] = TB1[i]

def distTA(A_in,B_in):
    """This function is used to calculate the distance between two ternary address."""
    total = 0
    indexA = []
    indexB = []
    final = []
    Acopy = copy.deepcopy(A_in) # we make a deep copy of the first input. we make deep copy as we dont want the original list to change.
    Bcopy = copy.deepcopy(B_in)
    if(A_in == B_in):   # if the 2 inputs are the same then we retrn 0 as the distance between them.
        return 0
        reduceTA(Acopy,Bcopy)   # if the 2 inputs are not same then we find the reduced TA of the 2 inputs.

        minA = Acopy.index(min(Acopy))  # this function first finds the minimum value in Acopy and then finds the index of that value in Acopy and stores it in minA
        minB = Bcopy.index(min(Bcopy))# we find the index of the lost value so that we donot subtract the lowest valued index with 2^(n-1)

        for i in range(0,len(Acopy)): # we add all the elements in Acopy and then find its int2BaseTwo value as we need to find the number of times o appears in the loop.
            total +=Acopy[i]  
        total = (total+2)//2 # once we add all the values we divide it by 2 and then send that value to find the power.
        power = int2baseTwo(total)
        n = power.count(0)  # n stores the number of times 0 appears in power as that will be the value that we will use

        for i in range(0,len(Acopy)):
            if (i!=minA): # while the value of i is not minimun i.e while the index is not that of the lowest value subtract 2^(n-1) from all the values
                Acopy[i] = Acopy[i] - (2**(n-1))
                indexA.append(i)# we append the indexes that we have changed into a new list.

            if (i!=minB): # performs the same function as the above
                Bcopy[i] = Bcopy[i] - (2**(n-1))
        difference = 3-(minA+minB)  # now to find the 3rd point in the triangle we subtract the addition of the 2 min indexes with the 3. we use this value for option 2
        option1 = Acopy[minB] + 1 + Bcopy[minA] # for option 1 we add the values at index minB in A and minA in B and add '1' to to, this 1 is for the bridge.
        final.append(option1)   # we append this result into final list.

        option2 = Acopy[difference] + 2 + Bcopy[difference] + (2**(n-1))-1 # This is the longer route. we add the 2 values at index 'difference' and add the length of 1 side and 2 bridges.

        required = min(final)# this gives the minimum of the 2.
        return required

def play():
    """This Function is used to call all the remaining functions and play the game"""
    value = 0
    print(" Welcome to the game of Tower of Hanoi")
    n = int(input("Enter the number of disc you would like to play with = ")) # input of the number of discs by the user.
    rollcall = randLocationR(n)# calling all the function from above. finding rollcall and SA and TA
    List = rollcall2List(rollcall)
    SA = rollcall2SA(rollcall)
    TA = SA2TA(SA)
    print("Your Goal is to move everything to tower 3. (Q or q quits)")
    TA2 = (2**n)-1   # we know when we reach the final state the TA of the 2 positions are the same and its value is 2^(n-1)
    if(n%2!=0):   # but the position of the TA2 is not same for all the values its different for odd and even values of n.
        distance1 = distTA(TA,[TA2,TA2,0])# by this we calculate the miminum distance between the current position and the final state.
        distance1 = distTA(TA,[0,TA2,TA2])
    print("Fastest Path is " ,distance1," moves")
    while(value != 1 or distance != 0): #loop to run the game until the user quits or finishes the game.
        stack1 = input("Take from stack")
        if (stack1 == '1'):  # user inputs values as 1,2 or 3 and we refer to them as 0,1,2 in our code hence we convert the input accordingly. i.e 1 to 0, 2 to 1, 3 to 2.
            stack1 = 0
        elif (stack1 == '2'):
            stack1 = 1
        elif (stack1 == '3'):
            stack1 = 2
        if(stack1 == 'q' or stack1 =='Q'):  # if user enters 'q' and 'Q' then the game quits.
            value = 1
            return "Quitter"
        stack2 = input("place on stack ")
        if (stack2 == '1'):
            stack2 = 0
        elif (stack2 == '2'):
            stack2 = 1
        elif (stack2 == '3'):
            stack2 = 2
        if(stack2 == 'q' or stack2 =='Q'):
            value = 1
            return "Quitter"
            check = isLegalMove(List,int(stack1),int(stack2))  #we check if the the move that user is doing is legal or not.
            if(check == True):  # if the isLegalMove true then we make move
                rollcall2 = list2Rollcall(List) #now we convert the new list formed again into new TA and find its distance from the final state
                SA1 = rollcall2SA(rollcall2)
                TA1 = SA2TA(SA1)
                    distance2 = distTA(TA1,[TA2,TA2,0])
                    distance2 = distTA(TA1,[0,TA2,TA2])
                print("Fastest Path is " ,distance2," moves")
                if(distance2!=0): # if the distance is not zero then we continue to play
                    value = 0
                elif(distance2==0): # if distance is zero we stop playing
                    value =1
                    print("Puzzle Solved great work!!!")
                print("wrong Input") # if the move that user is trying to make is illegal we say the move is incorrect and ask him to press enter to continue playing or Q to quit.
                enter = input("Press enter to continue")
                if (enter == 'q' or enter == 'Q'):
                    value = 1
                    value = 0


200$ Tablet

The 200$ Investment!!!
RIM has decreased the price of its BlackBerry PlayBook OS version 2.0 to 200$, which is a massive decrease in the cost. 
What is the reason??
Well having Introduced in the market to compete with the likes of I-Pad and Android Tabs, it couldn't live up to the Expectations and failed terribly at the market. It wasn't as competitive as the other tablets in its price range and didn't have that great features.
Now RIM has decreased the price of its tablet to 200$ for 16 gigs and is back in the market. People say that RIM has decreased the price coz it is planning to go out of market in the tablet business,but i really feel that with this slash in price they are back in the business.

Competition :
I was looking to buy a new tablet, something not too expensive but powerful and something that was portable.
I had already ruled out Ipad 2 and Galaxy tab as they were too out of my pocket.
I had short listed 3 tablets in the category of in and around 300$ they were
1.Kindle Fire
3.BlackBerry PlayBook

Technical Comparison
Kindle Fire :-
Courtesy Amazon - Kindle Fire

Display - 7-inch multi touch display with IPS technology and anti reflective treatment.
               1024 X 600, 169 pixels per inch (PPI)

Dimensions - 7.5"x4.7"x0.45"

Weight- 14.6 ounces

On - Device Storage - 8GB internal Storage. No memory slot.

Wi-Fi - Internal Wi-Fi is present

USB - USB 2.0

Audio - 3.5mm stereo audio jack, top mounted stereo speaker.

Formats Supported - (AA, AAX)), DOC, DOCX, JPEG, GIF, PNG, BMP,
                                  non-DRM AAC, MP3, MIDI, OGG, WAV, MP4, VP8.

NOOK Specs :

Slim, Light & Portable -  Height: 8.1 inches

                                      Width: 5.0 inches
                                      Depth: 0.48 inches
                                      Weight: 15.8 ounces

Display -    7-inch VividView™ Color Touchscreen
                  16 million+ colors on IPS display
                  High resolution display -- 1024 X 600, 169 pixels per inch (PPI)

Web and Email -      Enhanced Web browsing with video via Adobe® Flash® Player  

                                Parental controls to easily disable the Web browser

                                Check & send email all from one inbox.


Built-in Wi-Fi® -    Wireless connectivity via Wi-Fi® (802.11b/g/n)

Supported File Types - 

 Load EPUB (including Non or Adobe DRM) or PDF file types from your computer or microSD card Other documents supported: CBZ, XLS, DOC, PPT, TXT, DOCM, XLSM,  PPTM, PPSX, PPSM, DOCX, XLX, PPTX, JPG, GIF, PNG, BMP, MP3, MP4, AAC


Battery Life - Read for up to 8 hours without recharging with wireless off
                     Installed rechargeable battery 
         Charge from a wall outlet using the in-box Power Adapter and USB Cable                             (charging time: approximately 3 hours from wall outlet)

Memory -     8GB(up to 5,000 books) built-in memory (6GB for content; 5GB reserved for B&N content) 

                     Expandable storage up to 32GB with microSD memory card

Pre-Loaded NOOK Apps -  Pandora internet radio, Chess, Crossword, Sudoku, Media Gallery, NOOK  Friends, Email

BlackBerry PlayBook-
Display - TFT Capacitive Touch screen, 16M colors, Multi Touch.
Dimentsions - 194x130x10mm
Weight - 425g
Sound - Speakers and 3.5mm Stereo Jack.
Memory - No card Slot
                 Internal Storage 16GB,32GB,64GB and 1GB RAM
Wi-Fi - Internal Wi-fi.

Bluetooth -Yes.
USB -Yes

Camera - Primary (Back Camera ) 5MP, 2592x1944 pixels,autofocus
               video  yes
               Secondary( Front Camera) 3MP

Operating System - BlackBerry Tablet OS v2.0
CPU   -   Dual - core 1.5GHz
Email - Push Email
Micro HDMI port
Document Viewer 
Media Player - MP3/WMA/AAC+
Video Player
Predictive text input.

For people who are more interested in reading novels and want a nice collection of books then i would recommend going for either Kindle Fire or Nook. Their online collection of books is really vast whereas Playbook has no such collection.

Nook overtakes Kindle in the Section of display and RAM as Nook color has a better display and better RAM. Nook has a better surface on the backside to hold the tablet whereas in Kindle its a little more slippery.

But Amazon has a better Collection of books on its Market . Also, Nook has a large amount of Internal Memory saved for Applications and books from Barnes & Noble's Market and very less for user but it does have slot for an External memory card which is not present in playbook or kindle fire.

Many units of Kindle Fire has faulty Wi-Fi and people have issues connecting with the internet and many users have not given it a good rating on the Amazon's official site itself because of its lagging experience.

Reviews about Wifi - You are Not Alone!!!

The display is not as good as the NOOK color and NOOK wins hands down in terms of RAM, performance, display and Handling.

In all Nook is a little better than Kindle fire and currently Nook color is placed at 169$ where as fire is at 199$.

BlackBerry PlayBook - 
In its Price range i would say this is the best tablet beating NOOK and Kindle in terms of Memory, RAM, CPU, Multi Tasking.

Though the other 2 have android as the basic OS and have access to some applications from the android market, BB market is not as good.

Common games like Angry Bird is charged between 4 to 5$.
The range of utility Softwares available in the BB market for free is very less but it does give software's for PDF reading, Word, Excel for free.

official Sites for - 

In all I would recommend to buy a BB Playbook in case your main purpose to buy a tablet is not reading  novels but official use or to have a tablet experience. 
For Novels the choice would be yours as NOOK color and Kindle Fire both are almost at par.

Thank you.

Your comments and Suggestions are always welcome.