Monday, November 23, 2015

Recursion in programming

Recursion is a very useful programming technique. It can be simply described as calling the function by the same function. It can be understood better by examples rather than by definitions. 

A recursive algorithm must have the following,
  1. Base Case (to stop recursive call at some point)
  2. Recursive Call
Example:

Finding factorial of a positive integer.

The factorial of a positive integer is multiplication of a series of descending natural numbers.
eg:
4! = 4 x 3 x 2 x 1

In the above example note the following property.

4! = 4 x (3 x 2 x 1)
4! = 4 x 3!

Therefore for any positive integer n,
n! = n x (n-1)!

The above property enables us to use recursion.
If we write a function to find the factorial of a positive integer, let's call it 'factorial(int number)',
then 
factorial(n) = n x factorial(n-1);
This is the recursive call. 

The base case is the factorial of 1. 
factorial(1) = 1;

Following is a program written in C to find factorial using a recursive algorithm.

[code]


#include <stdio.h>

int main()
{

      int number;
      printf("Enter an ineger: ");
      scanf("%d",&number);
      printf("The factorial is %d",factorial(number));

      return 0;

}

int factorial(int number)
{

      if(number == 1)
            return 1;

      return number*factorial(number-1);

}[/code]

Sunday, November 22, 2015

C program to find all combinations of user entered characters.

This program uses user entered characters(up to 8 characters) to generate all possible combinations of  these characters using every character entered. If duplicate characters are entered they will be treated as different characters. This can be useful in generating wordlists used in password cracking.
compiling : gcc -o program program.c -lm
[code]#include <stdio.h>
#include <math.h>  // compile the code with -lm to the end since ceil is used
#include <stdlib.h>

int size;

/*functions used*/
int min_num();
int max_num();
int to_int(int array[size]);
int* to_array(int num);
int is_bounded(int num);
int factorial(int num);

int main()
{
    printf("Enter the number of characters: ");
    scanf("%d",&size);
   
    if(size>8)
    {
        printf("Please enter a number less than 10\n");
        return 0;
    }

    char array[size];
    int i;       
    char c;

    printf("Enter characters seperated by space or newline\n");
    for(i=0; i<size; i++)
    {
        scanf("%c",&c);
        while(c==' ' || c=='\n')
        {
            scanf("%c",&c);
        }
        array[i] = c;       
    }   

    printf("There are %d number of possible combinations(combinations will include duplicate characters if you entered any!)\n",factorial(size));
    printf("Press enter to proceed.....\n");
    getchar();
    getchar();
   
    int iter = min_num();
    int max = max_num();

    int* arr;
   
    while(iter<=max)
    {
        if(is_bounded(iter))
        {   
            arr = to_array(iter);
            for(i=0; i<size; i++)
            {
                printf("%c",array[arr[i]-1]);       
            }
            printf("\n");
        }
        iter++;
    }
   
    return 0;
}

/***********************************************************/
/*returns the integer contructed by the given integer array*/
int to_int(int array[size])
{
    int i, ans = 0;
    for(i=0; i<size; i++)
    {
        ans = ans + array[i];
        ans = ans*10;
    }
    return ans/10;
}

/****************************************************************/
/*convert an integer number to an integer array
return a reference to the array*/
int* to_array(int num)
{
    int* array = malloc(sizeof(int)*size);
    double tmp1;
    int tmp2;
    double tmp3;
    int i;
    for(i = size-1; i>=0; i--)   
    {
        tmp1 = num/10.0;
        tmp2 = num/10;
        tmp3 = ceil((10*tmp1 - 10*tmp2));   
        array[i] = tmp3;
        num = num/10;
    }
    return array;
}

/*********************************************************/
/*check whether an integer has distict digits
and each digit is less than the number of digits
return 1 if conditions are met*/
int is_bounded(int num)
{
    int* array = to_array(num);
   
    int i,j;
    for(i = 0; i<size; i++)
    {
        for(j = 0; j<size && i!=j; j++)
        {
            if(array[i] == array[j])
                return 0;
        }
        if(array[i]>size || array[i]==0)
            return 0;
    }
    return 1;

}

/***********************************************************/
/*returns the maximum number that a number with 'size' number of digits
  and with concecutive digits from 1
  eg: if size = 4
      then max_num() returns 4321*/
int max_num()
{
    int i;
    int arr[size];
    int tmp = size;
    for(i = 0; i<size; i++)
    {
        arr[i] = tmp;
        tmp = tmp-1;
    }
    return to_int(arr);
}

/**************************************************************/
/*does the opposite of max_num()*/
int min_num()
{
    int i;
    int arr[size];
    for(i = 0; i<size; i++)
    {
        arr[i] = i+1;
    }
    return to_int(arr);

}

/*****************************************************************/
/*recursively return the factorial of a given number*/
int factorial(int num)
{
    if(num == 1)
        return 1;
    return num*factorial(num-1);

}[/code]

Example:
./program
Enter the number of characters: 3
Enter characters seperated by space or newline
a b c
There are 6 number of possible combinations(combinations will include duplicate characters if you entered any!)
Press enter to proceed.....

abc
acb
bac
bca
cab
cba


Saturday, October 11, 2014

Earn cash back for your purchases from Ebates!!

You can easily earn a part of the money you spend for a purchase on ebay ebates. It's free!!! It's like getting a discount. You can receive the pay back to your paypal account.
This site is not only for ebay, it is also available for amazon, walmart, livingsocial and etc.
Sign up and enjoy the cash backs!!. Simply click the following link to sign up.
http://www.ebates.com/rf.do?referrerid=prJehZWAYqOmg1SuONkOaw%3D%3D&eeid=26471http://www.ebates.com/rf.do?referrerid=prJehZWAYqOmg1SuONkOaw%3D%3D&eeid=26471



Sunday, October 5, 2014

How to buy items from ebay without a credit card(for Sri Lankans)?

In this post I'm  going to explain you the easiest way to purchase in ebay and get items posted to your home for Sri lankans. For this you will only need a savings bank account and a debit card.

The first step is to open a bank account. Commercial bank, Sampath bank and Hatton National banks allow international transactions. But the best bank to choose is Commercial bank. Because they issue an international Visa shopping debit card which is already activated for international transactions.

Go to your nearest Commercial bank and open a savings account.
Ask for a debit card application and fill it applying for a Visa shopping debit card. Not a master card. They charge Rs.400 for the card. Any bank does.
You can obtain the card within 15 minutes if you don't want your name printed on it.
If you want your name on the card, your card will sent via post within two weeks.

After you obtain your card check it's balance with an ATM. Then your card will be activated.

Then go to ww.ebay.com  website and register as a new user.







Enter your address and other details correctly. 
Then you will receive a confirmation email to your email address. Confirm it and sign in to your ebay account. 


Then visit www.paypal.com and make a new personal account. 
Enter your details correctly. 



Confirm the confirmation email that you will receive and sign in to your paypal account. 
Go to profile tab and select add/edit card. Enter your card details here. (Card number and other details). Then they will ask for a 3 digit number mentioned after the card number in your card. 
You will find it on the card. 

After that, you need to get your card verified. For this you need to transfer 1$  to paypal. Don't worry this will be credited back once the account is verified. 

The way to verify the account is,  when you transfer the money to paypal, the transaction will be recorded in your bank account. A four digit number will appear in your bank statement for this transaction. You simply can call your bank and ask for this number. They will ask your detail just to confirm that the owner is speaking. 

Enter this number in your paypal account and then you are verified. 
Now you are ready to purchase. 

Now go to ebay and find your items. 

Sometimes ebay sends coupons for new users.
Make sure you get a free shipping and buy it now item. Because sometimes shipping charges are more than the price of your item. Bidding items are not sure to have. Therefore when you search, go for advanced search and specify your search. 

In advanced search you can specify the price range, keyword and category.





You can also specify buying format and shipping option. 





You can find cheap items if you search the right way. 

Wednesday, April 10, 2013

Herbs to fight cancer



Garlic

Garlic is a very widely used herb in lowering both cholesterol and triglycerides. It also has anti-clotting and anti-bacterial properties and in addition, not many people know that it also has anti-tumor properties. Garlic contains phytochemicals which are helpful in getting rid of toxins and free radicals that are found inside the body.

Milk thistle

Milk thistle is a medicinal plant which is usually used to treat varicose veins, menstrual problems and depression. True to its name, it is also used to increase breast milk production in women. Milk thistle has also been used as to detoxify liver.

Turmeric

Turmeric is a yellow- orange spice that belongs to the same family as the ginger. Aside from being used as a spice for cooking, it is also used for its medicinal properties. Many researches have been made in order to prove that turmeric really is the wonder herb against cancer.

Sunday, March 31, 2013

Steps To Avoid Kidney Diseases.


  • Be Active 
  • Controlling of Blood Sugar Levels 
  • Blood Pressure Control 
  • Proper food and Control of Weight 
  • No Smoking 
  • Avoid Unnecessary Medicines 
  • Kidney Check-Up

Monday, March 18, 2013

Lie detection, is it true?


A lie detector or polygraph machine is used to aid in finding out if someone is truthfully answering a specific set of questions. It is a combination of various devices used to measure a person's physical responses to what are known as irrelevant, control and relevant questions. How this data is interpreted by the examiner will determine if the subject is telling the truth.

In a typical lie detection test the subject is asked a number of question before the actual test is performed. These are used to create what is known as a "baseline". How the subjects reaction to these question will help create the baseline. The subjects pulse, blood pressure, rate of breathing and how much they sweat could be all measured as physical responses to help create the baseline. When the actual test questions are asked the response to these questions is compared to the questions asked to create the baseline.

There are three types of questions asked. Some questions are irrelevant such as asking the subject what their name is. Others are called probable-lie control questions and often get an answer which is not truthful. This type of question could be asking the subject if they ever committed a crime. The type of question most important to the examiner is what is known as a relevant question. A relevant question would be like asking the subject if they committed the crime. If the subjects response to the probable-lie control question is greater than to the relevant questions, then the answer to the relevant question is considered truthful.

It should be noted many scientists do not consider a lie detector test to be very accurate. It is not considered scientific by many and therefore not accurate enough to be used in a court of law.

Optimized quick sort

In the previous article  we discussed about quick sort algorithm. In this article we are going to discuss about further optimizing quick sor...