Daily Archives: January 8, 2014

Failed to load the JNI shared library :Eclipse Error

I recently downloaded the 64 bit version[eclipse-SDK-3.7.1-win32-x86_64] of eclipse for windows 7 .But on when I tried to run the eclipse.exe file , the following error message was displayed.

” Failed to load the JNI shared library  c:\Program Files(x86)\java\jdk1.6.0_25\bin\..\jre\bin\client\jvm.dll\ “

As displayed here:

eclipse error

Failed to load the jni shared library :Eclipse Error

What’s the reason for this Error ?

1.You have downloaded the 64 bit version of eclipse but are using the 32 bit jdk.

2.For using 64bit version of any of the eclipse ide, you have to install the 64 bit version of jdk[jdk-7u2-windows-x64.exe].

How to solve the problem:

1.Download the 64 bit version of jdk .

2.Install it .

3.Set the value of JAVA_HOME enviornment variable to C:\Program Files\Java\jdk1.7.0 [default installation].

I’ll recommend try to start eclipse now before going to next step

Still not able to launch workspace? Do this

Modify the  eclipse.ini file

1.Eclipse startup is controlled by the options in eclipse.ini file present in eclipse installation directory it contains command-line options that are added to the command line used when Eclipse is started up.

2.Now use the -vm option to specify pate to the java executable which u want to with this eclipse modify the file as shown here:
initial eclipse.ini file present

eclipse.ini before before

eclipse.ini before before

after modifying eclipse.ini

eclipse.ini after modification

eclipse.ini after

3.Note that u have to specify the full path to the javaw.exe file with javaw.exe added to it  not just the path to it different from what we do while specifying JAVA_HOME .                          Visit eclipse wiki page for more information eclipse.ini.
This is a kind of workaround if u are facing problems in specifying the jvm path

Now Run eclipse.exe and enjoy the 64 bit version.

Binary Search

Binary Search finds the position of a specified value (key)in a sorted array.In each step it discards half of the elements to search , thus the worst case running time of binary search is O(log n).

Binary search follows these simple steps :

1. Compare the middle element of the array with key if middle element is same as key return the index of the middle else go to step 2

2.if middle element greater than key  binary search for the key in the left half of the array (start to middle-1) , if its less  than key binary search for the key in the right half of the array (middle+1 to end) .

3.repeat this till the size of array to search reduces to zero.

 

For more information on binary search visit http://en.wikipedia.org/wiki/Binary_search_algorithm

Below is the C++ code for binary search algorithm.
CODE:

/*

http://randomtechbits.in/

*Program to search a number in a sorted array using BINARY SEARCH
*inputs: N- number of integers to sort ,A-array of integers,NUM -the number to search for     
*output: position of number in the array
*code tested on devc++ IDE
*/
#include<iostream>
#include<conio.h>
using namespace std;

int binarysearch(int A[],int NUM,int low,int high)
{int mid;
if(low>high)return -1;
else mid=low+(high-low)/2;
if(A[mid]==NUM)  return mid;
else if(A[mid]>NUM)  return binarysearch(A,NUM,low,mid-1);
else  return binarysearch(A,NUM,mid+1,high); 
}
main()
{
int N,NUM,pos;
int *A;
int i,j;
cout<<"Enter the number of elements in te sorted array:\n"; cin>>N;
A=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)   cin>>A[i];
cout<<"Enter the number to search for\n";   cin>>NUM;
pos=binarysearch(A,NUM,0,N);
if(NUM!=-1)
  cout<<"\nNumber found at postion at "<<pos<<" in the array"<<"\n";
else 
  cout<<"\nNumber not found in the array"<<"\n";
//for more codes visit randomtechbits.in
getch();   
}

Counting Sort

Counting Sort assumes that each of the  input element is an integer in the range 0 to k, for some integer k. When k=O(n), the sort runs in O(n) time. The basic idea of counting sort is to determine, for each input element x, the number of elements less than x. This information can be used to place element x directly into its position in the output array. For example, if there are 10 elements less than x, then x belongs in output position 11. This scheme must be modified slightly to handle the situation in which several elements have the same value, since we don’t want to put them all in the same position.

In the code for counting sort, we assume that the input is an array A[], and thus length[A] =n. We require two other arrays: the array B[] holds the sorted output, and the array C[] provides temporary working storage.

 For more information on counting sort visit http://www.algorithmist.com/index.php/Counting_sort

Below is the C++ code for counting sort algorithm.
CODE:

/*
 http://randomtechbits.in/
*Program to sort numbers using COUNTINGSORT
*inputs: N- number of integers to sort ,A-array of integers       
*output: sorted array of integers
*Assumption : all the number will lie in range 0 to MAX-1
*code tested on devc++ IDE
*/
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

void coutingsort(int A[],int len,int MAX)
{int *C=new int[MAX];
//intialise all elements to 0  
memset(C,0,sizeof(int)*MAX);
for(int i=0; i<len; i++)
 {
 C[A[i]]+=1;
 }
for(int i=1; i<MAX; i++)   {   C[i]+=C[i-1];   } int *B=new int[len]; for(int j=len-1; j>=0; j--)
  {
  B[C[A[j]]-1]=A[j];
  C[A[j]]-=1;
  }
for(int j=len-1; j>=0; j--)
  {
  A[j]=B[j];
  }
}
main()
{
int N,MAX,temp;
int *A,*O;
int i,j;
cout<<"Enter the number of elements in array:\n"; cin>>N;
A=new int[N];
O=new int[N];
cout<<"Enter the upper limit for input values:\n"; cin>>MAX;
cout<<"Enter the array\n";
for(i=0;i<N;i++)   cin>>A[i];
coutingsort(A,N,MAX);
cout<<"\nThe Sorted array after COUNTING SORT:"<<i<<"\n";  
for(i=0;i<N;i++)
  cout<<"\t"<<A[i];   
//for more codes visit Code Library
getch();
}

Merge Sort

Merge Sort  belongs to category of divide and conquer algorithms. Which means it divides the problem into  small subproblems, solves them and then combine the solutions of these subproblems to generate solution  of the original problem.Merge Sort compares elements with each other for sorting the list.

The steps followed to mergesort are are:

  1. Divide the unsorted list into n sublists, each containing single element .
  2. Repeatedly merge(involves comparing elements of the sublists) sublists to produce new sublists until there is only 1 sublist remaining. This will be the sorted list.

 For more information on merge sort visit http://en.wikipedia.org/wiki/Merge_sort

Below is the C++ code for merge sort algorithm.
CODE:

/*
 http://randomtechbits.in/
*Program to sort numbers using MERGESORT
*inputs: N- number of integers to sort ,A-array of integers       
*output: sorted array of integers 
*code tested on devc++ IDE
*/
#include
#include
using namespace std;

void merge(int A[],int p,int q,int r,int O[])
{
int i=p;
int j=q+1;
int k=p;
//Note that I have intionally used few extra lines in all of the follwing loops tomake it more clear to learners
while((i<=q)&&(j<=r))
  {
  if(A[i]<A[j])
    {
    O[k++]=A[i++];
    }
  else
    {
     O[k++]=A[j++];
    }
}
while(i<=q)
  {
  O[k++] =A[i++];
  }
while(j<=r)
  {
  O[k++]=A[j++];
  }
int l=p;
while(l<=r)
  {
  A[l]=O[l];
  l=l+1;
  }
}

void mergesort(int A[],int p,int r,int O[])
{
  if( p < r)
    {
    int q=(p+r)/2;
    mergesort(A,p,q,O);
    mergesort(A,q+1,r,O) ;
    merge(A,p,q,r,O);
    }
}

main()
{
int N,temp;
int *A,*O;
int i,j;
cout<<"Enter the number of elements in array:\n"; cin>>N;
A=new int[N];
O=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)   cin>>A[i];
mergesort(A,0,N-1,O);
cout<<"\nThe Sorted array after MERGE SORT:"<<i<<"\n";  
for(i=0;i<N;i++)
  cout<<"\t"<<A[i];
//for more codes visit Code Library

getch(); 
}

Quick Sort

Quick Sort belongs to the category ofdivide and conquer algorithms. Quicksort  divides a large list into two smaller lists around a selected pivot . Quicksort can then recursively sort the sub-lists.It sorts the elements inplace.

The steps followed are are:

  1. Select an element, called a pivot, from the list.
  2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it . After this partitioning, the pivot is in its final position. This is called the partition operation.
  3. Recursively apply the above steps to the two sub-lists generated.

 For more information on bubble sort visit http://en.wikipedia.org/wiki/Quicksort.

Below is the C++ code for quick sort algorithm.
CODE:

/*
 http://randomtechbits.in/
*Program to sort numbers using QUICKSORT
*inputs: N- number of integers to sort ,A-array of integers       
*output: sorted array of integers
*code tested on devc++ IDE
*/

#include<iostream>

using namespace std;
int partition(int A[],int l,int r)
{
// note that I've used some vars unnecessarily just make the code more clear 
int key=A[l],q;
int  p=l+1;
q=r;
int t;
while(q>=p)
  {
   while(A[p]<key)
   ++p;
   while(A[q]>key)
   q--; 
  if(q>p)
    {t=A[q];
     A[q]=A[p];
     A[p]=t;  
    }
  } 
A[l]=A[q];
A[q]=key;
return q;                  
}

void quicksort(int A[],int p,int r)
{int q;
if(p>=r)return;
else
  q=partition(A,p,r);
  quicksort(A,p,q-1);
  quicksort(A,q+1,r);     
}

main()
{
int N,temp;
int *A;
int i,j;
cout<<"Enter the number of elements in array:\n";
cin>>N;
A=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)
  cin>>A[i];
quicksort(A,0,N-1);
cout<<"\nThe Sorted array after QUICK SORT:"<<i<<"\n";  
for(i=0;i<N;i++)
  cout<<"\t"<<A[i];   
//for more codes visit randomtechbits.in
getch();  
}

Insertion Sort

Insertion sort  is a simple sorting algorithm that iterates through the list consuming one input element each repetition, and growing a sorted output list. On a repetition, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.Sorting is done inplace .So its an inplace sorting algorithm and original order is maintained.For more information on insertion sort visit http://en.wikipedia.org/wiki/Insertion_sort.

Below is the C++ code for insertion sort algorithm.
CODE:

/*
http://randomtechbits.in/
*Program to sort numbers using INSERTION SORT
*inputs: N- number of integers to sort ,A-array of integers
*output: sorted array of integers
*code tested on devc++ IDE
*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int N,temp;
int *A;
int i,j;
cout<<"Enter the number of elements in array:\n";
cin>>N;
A=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)
  cin>>A[i];
for(i=1;i<N;i++)
  {temp = A[i];
  j=i-1;
  while((j>=0) && (A[j]>temp))
   {
   A[j+1]=A[j];j=j-1;
   }
  A[j+1]=temp;
  }
cout<<"\nThe Sorted array after INSERTION SORT:"<<"\n";
for(i=0;i<N;i++)
  cout<<"\t"<<A[i];
//for more codes visit Code Library
getch();
return 0;
}

BubbleSort

Bubble sort  is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in wrong order. We make several passes over the list till no swaps are needed, which indicates that the list is sorted.For more information on bubble sort visit http://en.wikipedia.org/wiki/Bubble_sort.

Below is the C++ code for bubble sort algorithm.
CODE:

/*
 http://randomtechbits.in/
*Program to sort numbers using BUBBLESORT
*inputs: N- number of integers to sort ,A-array of integers
*output: sorted array of integers
*code tested on devc++ IDE
*/
#include<iostream>
#include<conio.h>
using namespace std;

main()
{
int N,temp;
int *A;
int i,j;
cout<<"Enter the number of elements in array:\n";
cin>>N;
A=new int[N];
cout<<"Enter the array\n";
for(i=0;i<N;i++)
cin>>A[i];
for(i=0;i<N-1;i++)
{
for(j=0;j<N-1-i;j++)
 {
 if(A[j]>A[j+1])
  {int t;
  t=A[j];
  A[j]=A[j+1];
  A[j+1]=t;
  }
 }
}
cout<<"\nThe Sorted array after BUBBLE SORT:"<<"\n";
for(i=0;i<N;i++)
 cout<<"\t"<<A[i];
//for more codes visit Code Library
getch();
}

LinkedList : creation and manipulation

LinkedList is a data structure consisting of a group of nodes .In its simplest form, each node contains a data element and a link or reference  to the next node in the sequence . Because of this structure  insertion or removal of elements from any position in the list becomes very easy.

linkedlist-randomtechbits

linkedlist code for creation and list manipulation

Here is the c++ code which can create a link list and allows the user to perform various operations on linked list like  inserting element, delete,reversing the linked list , counting the number of elements etc to manipulate it:

CODE:

/*
 http://randomtechbits.in/
*Program to various operations on linked list of integers
*code tested on devc++ IDE
*/

#include
#include
#include
using namespace std;

struct node
{int data;
node *link;
}*P;

void add(int NUM)
{
node *temp,*q;
if(P==NULL)
  {P=new node ;
   P->data=NUM;
   P->link=NULL;
  }
else
  {q=P;
  while(q->link!=NULL)q=q->link;
  temp=new node;
  temp->data=NUM;
  temp->link=NULL;
  q->link=temp;
  }
}

void display()
{node *q;
q=P;
while(q!=NULL)
  {
  cout<data<<"\n";   q=q->link;
  }
}
void del(int NUM)
{node *q,*prev=NULL;
q=P;
while(q->data!=NUM&&q!=NULL)
  {prev=q;
   q=q->link;
  }
if(q==NULL)cout<<"\nElement does not exist in the list";
 else   {prev->link=q->link;
  delete(q);
  }
}

void reverse()
{node *q,*temp,*next;
if(P==NULL)return;
q=P->link;
P->link=NULL;
while(q!=NULL)
  {temp=q->link;
  q->link=P;
  P=q;
  q=temp; 
  }
}
int count()
{int count=0 ;node *q=P;
while(q!=NULL){q=q->link;++count;}
return count;
}

int nthfromend(int INDEX)
{node *i,*j,*prev;
int current=0,n=0,len,ifromend;
i=P;
len=count();
ifromend=len+1-INDEX;
if(INDEX<0||INDEX>len)
 {cout<<"invalid INDEX\n";  } else for(i=P;i!=NULL;i=i->link)
  {if(current==ifromend){return i->data;}
   current++;  
  }
}

main()
{P=NULL;
node *temp;
int OPTION;
while(true)
{cout<<"Select the operation u want to perform on linked list:\n1.Add Element\n2.Delete Element\n3.Display()\n4.Reverse List\n5.Find nth element from end\n6.Count number of Elements in list\n7.Exit\n\n"; cin>>OPTION;
switch(OPTION)
 {
 case 1:int NUM;
        cout<<"Enter the element to add:\n";         cin>>NUM;
        add(NUM);
        break;
 case 2:
        cout<<"Enter the element to delete:\n";         cin>>NUM;
        add(NUM);
        break;
 case 3:display();
        break;
 case 4:reverse();
        break;
 case 5:int INDEX;
        cout<<"Enter INDEX:\n";         cin>>INDEX;
        cout<<INDEX<<"th element fomr end"<<nthfromend(INDEX)<<"\n";
        break;
 case 6:cout<<"There are "<<count()<<" elments in the list\n";
        break;
 case 7:exit(0);
 default:cout<<"u entered invalid option";
 }
}
getch();
}