SQL

Wednesday, 25 March 2015

Installing HandBreak on Ubuntu 14.04

Adding this PPA to your system

You can update your system with unsupported packages from this untrusted PPA by adding ppa:stebbins/handbrake-releases to your system's Software Sources. (Technical details about this PPA

Type the following to add PPA:
sudo add-apt-repository "deb http://ppa.launchpad.net/stebbins/handbrake-releases/ubuntu trusty main"
sudo apt-get update

Now if you go to software center , Just search for HandBreak. Now install it or you  can just type :
 sudo apt-get install handbrake-gtk

Ubuntu Gnome 14.04 LTS slow internet Problem

Method 1:
Disable IPv6 If your internet is working Slow . I recommend before you change any file ,please keep a backup.

To do this open Terminal (Press Ctrl+Alt+T) and copy the following commands in the Terminal:

sudo gedit /etc/sysctl.conf

Now add these lines at the end of file:
# IPv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1 
Now Save sysctl.conf file and close. 

Enter following command to restart sysctrl settings:

sudo sysctl -p

Method 2:
open your terminal and type:

sudo gedit /etc/nsswitch.conf

Find the following line :
hosts:          files myhostname mdns4_minimal [NOTFOUND=return] dns

replace the the line with the following:

hosts:          files dns

save the file before you exit.

Tuesday, 10 March 2015

Selection Sort Recursion

/*!
 * file SelectionSortRecusion.cpp
 * author Hasan
 * date 20 June 2014 03:32:27 
 * Exercise:4.31 C++How to Program 4th Ed
 */
#include <iostream>
#include <iomanip>
#include <ctime>
 
using namespace std;
 
void selectionSort(int[], int);
void printArray1(int[]);
const int SIZE = 10;
int MAXRANGE = 100;
int main(){
 int array[SIZE] = { 0 };
 srand(time(0));
 for (int i = 0; i < SIZE; i++){
  array[i] = 1 + rand() % MAXRANGE;
 }
 cout << "Unsorted Array\n";
 printArray1(array);
 cout << "Sorted Array\n";
 selectionSort(array, SIZE);
 printArray1(array);
 cin.get();
 return 0;
}
void selectionSort(int a[], int arraySize){
 int temp;
 if (arraySize >= 1){
  for (int i = 0; i < arraySize; i++){
   if (a[i] < a[0]){
    temp = a[i];
    a[i] = a[0];
    a[0] = temp;
   }
  }
  cout << "\n"<<a[0]<<"\n";//for debug purpose

  printArray1(a);//for debug purpose
  //Now array changes ..first element is i.e a[0] is &a[1]
  //so the for loop will compare this &a[1] which is first
  //element to the rest of the elements and a[0] will be the
  //smaller than all.then recursion will take &a[1] as first 
  //element again and will continue..
  selectionSort(&a[1],arraySize-1);
 }
}
void printArray1(int a[SIZE]){
 for (int i = 0; i < SIZE; i++){
  cout << setw(4) << a[i]<<"  ";
  
 }
 cout << endl;
}

output:

Unsorted Array
  64    87    59    19    84    25    48    76    55    27
Sorted Array

19
  19    87    64    59    84    25    48    76    55    27

25
  25    87    64    84    59    48    76    55    27  -858993460

27
  27    87    84    64    59    76    55    48  -858993460  -359215729

48
  48    87    84    64    76    59    55  -858993460  -359215729  1703300

55
  55    87    84    76    64    59  -858993460  -359215729  1703300  3578681

59
  59    87    84    76    64  -858993460  -359215729  1703300  3578681     1

64
  64    87    84    76  -858993460  -359215729  1703300  3578681     1  3970760


76
  76    87    84  -858993460  -359215729  1703300  3578681     1  3970760  39668
08

84
  84    87  -858993460  -359215729  1703300  3578681     1  3970760  3966808  -3
59215809

87
  87  -858993460  -359215729  1703300  3578681     1  3970760  3966808  -3592158
09     0
  19    25    27    48    55    59    64    76    84    87

Monday, 9 March 2015

Binary Search in Recursion C++

/*!
* file BinarySearchRecursion.cpp
* author Hasan
* date 25 June 2014 17:18:07
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 15;
 
int binarySearch(const int[], intintint);
void printRow(const int[], intintint);
void printHeader(void);
int main()
{
 int a[SIZE], key, result;
 
 for (int i = 0; i < SIZE; ++i)
  a[i] = 2 * i;
 
 cout << "Enter a number between 0 and 28: ";
 cin >> key;
 
 printHeader();
 result = binarySearch(a, key, 0, SIZE - 1);
 
 if (result != -1)
  cout << '\n' << key << " found in array element " << result << endl;
 else
  cout << '\n' << key << " not found" << endl;
 
 cin.get();
 return 0;
}
 
int binarySearch(const int b[], int searchKeyint lowint high)
{
 int middle;
 
 if (low <= high) {
  middle = (low + high) / 2;
  printRow(blow, middle, high);
 
  if (searchKey == b[middle])
   return middle;
  else if (searchKey < b[middle])
   return binarySearch(bsearchKeylow, middle - 1);
  else
   return binarySearch(bsearchKey, middle + 1, high);
 
 }
 
 return -1; // searchKey not found
}// Print a header for the output
 
void printRow(const int b[], int lowint midint high)
{
 for (int i = 0; i < SIZE; ++i)
  if (i < low || i > high)
   cout<< "";
  else if (i == mid)
   cout << setw(3) << b[i] << '*'//mark middle value
  else
   cout << setw(3) << b[i] << " ";
 
  cout << '\n';
  
}
 
 
void printHeader(void)
{
 cout << "Subscripts:\n";
 
 for (int i = 0; i < SIZE; ++i)
  cout << setw(4) << i;
 
 cout << '\n';
 
 for (int k = 1; k <= 4 * SIZE; ++k)
  cout << '-';
 
 cout << '\n';
 cin.get();
}// print one row of output showing the current
// part of the array being processed.


output:

Enter a number between 0 and 28: 13
Subscripts:
   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14
------------------------------------------------------------
  0   2   4   6   8  10  12  14* 16  18  20  22  24  26  28
  0   2   4   6*  8  10  12
  8  10* 12
 12*

13 not found


Enter a number between 0 and 28: 16
Subscripts:
   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14
------------------------------------------------------------
  0   2   4   6   8  10  12  14* 16  18  20  22  24  26  28
 16  18  20  22* 24  26  28
 16  18* 20
 16*

16 found in array element 8

Sunday, 8 March 2015

Static and non static local variables

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

Example 1:


#include 
#include 
 using namespace std;
// Function declaration
void func(void);

static int count = 10; /* Global variable */

main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// Function definition
void func( void )
{
    static int i = 5; // local static variable
    i++;
    cout << "i is " << i ;
   cout << " and count is " << count << endl;

}

Output:

i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
But if you erase the static storage class the the function always find i = 5. let's see it:
Example 2:
#include <iostream>
#include <iomanip>
// Function declaration
void func(void);
 
static int count = 10; /* Global variable */
 
main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// Function definition
void func( void )
{
    int i = 5; // local static variable
    i++;
    cout << "i is " << i ;
    cout << " and count is " << count << endl;
}
output:
i is 6 and count is 9
i is 6 and count is 8
i is 6 and count is 7
i is 6 and count is 6
i is 6 and count is 5
i is 6 and count is 4
i is 6 and count is 3
i is 6 and count is 2
i is 6 and count is 1
i is 6 and count is 0

Tuesday, 3 March 2015

Insertion Sort C++

/* Insertion Sort
 * Created by using MS2013
 * Coded by M. Hasan
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
 const int arraySize = 10; // size of array a
 int data[arraySize] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 };
 int insert; // temporary variable to hold element to insert
 
 cout << "Unsorted array:\n";
 
 // output original array
 for (int i = 0; i < arraySize; i++)
  cout << setw(4) << data[i];
 // insertion sort
 // loop over the elements of the array
 for (int next = 1; next < arraySize; next++)
 {
  insert = data[next]; // store the value in the current element
  int moveItem = next; // initialize location to place element
  // search for the location in which to put the current element
  while ((moveItem > 0) && (data[moveItem - 1] > insert))
  {
   // shift element one slot to the right
   data[moveItem] = data[moveItem - 1];
   moveItem--;
  } // end while
  data[moveItem] = insert; // place inserted element into the array// end for
 cout << "\nSorted array:\n";
 
 // output sorted array
 for (int i = 0; i < arraySize; i++)
  cout << setw(4) << data[i];
 
 cout << endl;
 cin.get();
 return 0;
} // end main

output:

Unsorted array:
  34  56   4  10  77  51  93  30   5  52
Sorted array:
   4   5  10  30  34  51  52  56  77  93

Linear Search using Recursion in C++

/*!
 * file LinearSearchRecursion.cpp
 * author Hasan
 * date 25 June 2014 00:12:21 
 */
#include <iostream>
#include <iomanip>
 
void printArray3(int[]);
int linearSearch1(int [],int,int,int);
using namespace std;
const int SIZE = 10;
int searchKey,element;
int main(){
 int array[SIZE] = { 0 };
 
 for (int i = 0; i < SIZE; i++){
  array[i] = i*2;
 }
 cout << "Array\n";
 printArray3(array);
 
 cout << "Enter the integer to search = ";
 cin >> searchKey;
 element = linearSearch1(array,searchKey,0,SIZE -1);
 if (element == -1)
 {
  cout << "Key not found";
  cin.get();
 }
 
 else {
  cout << "Key found in element " << element;
  cin.get();
 }
  
 cin.get();
return 0;
}
int linearSearch1(int a[], int sKeyint lowint high){
 if (a[low] == sKey)
  return low;
 else if (low == high)
  return -1;
 else
  return linearSearch1(asKeylow + 1, high);
 
}
void printArray3(int a[SIZE]){
 for (int i = 0; i < SIZE; i++){
  cout << setw(4) << a[i];
 
 }
 cout << endl;
}

output:

Array
   0   2   4   6   8  10  12  14  16  18
Enter the integer to search = 12
Key found in element 6

Array
   0   2   4   6   8  10  12  14  16  18
Enter the integer to search = 13
Key not found

Monday, 2 March 2015

Linear Search C++

#include <iostream>
#include <iomanip>
using namespace std;
int linearSearch(const int[],int ,int);
int main(){
 const int arraySize = 10;
 int a[arraySize];
 int searchKey; //value to locate in array a
 
 for (int i = 0; i < arraySize; i++){
  a[i] = 2 * i;
 }
 for (int i = 0; i < arraySize; i++)
  cout << setw(4) << a[i];
 cout << "\n\nEnter integer search key = ";
 cin >> searchKey;
 
 int element = linearSearch(a, searchKey, arraySize);
 if (element != -1){
  cout << "Found value in element in "<< element <<endl;
  cin.get();
  }
 else { 
  cout << "Value not found"<<endl; 
  cin.get();
 }
 
 cin.get();
 return 0;
}
int linearSearch(const int array[], int keyint sizeOfArray){
 for (int i = 0; i < sizeOfArray; i++){
  if (array[i] == key){
   return i;
  }
   
 }
 return -1;
}

output:

   0   2   4   6   8  10  12  14  16  18

Enter integer search key = 12
Found value in element in 6

   0   2   4   6   8  10  12  14  16  18

Enter integer search key = 17
Value not found