Skip to main content

Posts

Showing posts from December, 2012

(MAY/JUNE- 2012)Programming Solution of Object Oriented Programming with Java (150704)

Q-3(b) (7 marks) Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle. abstract class Shape{ abstract void area(); } class Circle extends Shape{ double r; Circle(double r) { this.r=r; } void area() { double a=(3.14*r*r); System.out.println("Area of circle having radius "+r+" is:"+a); } } class Rectangle extends Shape{ double w,h; Rectangle(double w,double h) { this.w=w; this.h=h; } void area() { double a=(h*w); System.out.println("Area of rectangle having width "+w+" and height "+h+" is:"+a); } } class Triangle extends Shape{ double b,h; Triangle(double b,double h) { this.b=b; this.h=h; } void area() { double...

Floyd’s Algorithm using dynamic Programming in C

#include< stdio.h > #include #include #define MAX 30 void floyd(int[MAX][MAX],int); int mix(int,int); void main() { int l[MAX][MAX],n,i,j; clrscr(); printf("\n Enter number of vertices you need:"); scanf("%d",&n); printf("\n[Enter 9999 as infinity value]"); for(i=1;i OUTPUT Enter number of vertices you need:4 [Enter 9999 as infinity value] l[1][1]:0 l[1][2]:5 l[1][3]:9999 l[1][4]:9999 l[2][1]:50 l[2][2]:0 l[2][3]:15 l[2][4]:5 l[3][1]:30 l[3][2]:9999 l[3][3]:0 l[3][4]:15 l[4][1]:30 l[4][2]:9999 l[4][3]:5 l[4][4]:0 l[4][3]:5 l[4][4]:0 --k=1-------D1--------- ---------P1------ 0 5 9999 9999 0 0 0 0 50 0 1 5 0 0 0 0 30 35 0 15 0 1 0 0 30 35 5 0 0 1 0 0 --k=2-------D2--------- ---------P2------ 0 5 20 10 0 0...

Find Longest Common Subsequence using Dynamic Programming in C

#include< stdio.h > #include #include #include #define MAX 50 int c[MAX][MAX]; char b[MAX][MAX]; char str1[MAX],str2[MAX]; char x[MAX],y[MAX],ans[MAX]; int m,n,ind; void LCS(char x[MAX],char y[MAX]); void printLCS(char[MAX][MAX],char[MAX],int,int); void main() { int i,j,ind1=1; clrscr(); printf("\n Enter string1:"); scanf("%s",&str1); printf("\n Enter string2:"); scanf("%s",&str2); LCS(str1,str2); printf("\n All the possible Longest Common Subsequence(LCS):"); printf("\n--------------------------------------------------------------"); for(i=n;i>0;i--) { if(c[m][i]==c[m][n]) { ind=0; printLCS(b,x,m,i); printf("\n solution %d: %s",ind1++,strrev(ans)); } else { break; } } printf("\n--------------------------------------------------------------"); getch(); } void LCS(char x1[MAX],char y1[MAX]) { int i,j; m=strlen(x1); n=strlen(y1); ...

0/1 knapsack problem using Dynamic programming in C

#include #include #define MAX 20 void knapsackDP(int,int); int max(int,int); void backtracking(); int weight[MAX],value[MAX],W,no,*x; int v[MAX][MAX]; void main() { int i,j; clrscr(); printf("\n Enter number of Object you want:"); scanf("%d",&no); printf("\n Enter weight and values in ascending order of vales");; for(i=1;i b)?a:b; } void backtracking() { int j1,i; j1=W; printf("\nIncluded Object \t weight \t value"); printf("\n-----------------------------------------------------------------------------"); for(i=no;i >=0;i--) { if(v[i][j1]!=v[i-1][j1] && (v[i][j1]==v[i-1][j1-weight[i]]+value[i])) { printf("\n%2d \t\t\t %2d \t\t %2d",i,weight[i],value[i]); j1=j1-weight[i]; } } } OUTPUT Enter number of Object you want:5 Enter weight and values in ascending order of values Enter Weight and Value for Object 1:1 1 Enter Weight and Value for Object 2:2 6 Enter Weigh...

Merge Sort using Divide and Conquer in C

#include #include #define MAX 50 void display(int[MAX],int); void Merge_sort(int[MAX],int,int); void Combine(int[MAX],int,int,int); void main() { int i,T[MAX],n; clrscr(); printf("\n Enter number of element you want:"); scanf("%d",&n); for(i=0;i low) { mid=(low+high)/2; Merge_sort(T,low,mid); Merge_sort(T,mid+1,high); Combine(T,low,mid,high); } } void Combine(int T[MAX],int low,int mid,int high) { int i,j,k,temp[MAX]; k=low; i=low; j=mid+1; while(i OUTPUT Enter number of element you want:7 Enter element[0]:77 Enter element[1]:55 Enter element[2]:44 Enter element[3]:66 Enter element[4]:44 Enter element[5]:22 Enter element[6]:99 ------------------------------------------------------------------------------------ Elements before sorting: 77 55 44 66 44 22 99 ------------------------------------------------------------------------------------ --------------------------------------------------------------------...

Quick Sort Using Divide an Conquer

#include #include #define MAX 50 //int pivotpoint; void display(int[MAX],int); void Quick_sort(int[MAX],int low,int high); int pivot(int[MAX],int,int); void main() { int i,T[MAX],n; clrscr(); printf("\n Enter number of element you want:"); scanf("%d",&n); for(i=0;i low) { pivotpoint=pivot(T,low,high); Quick_sort(T,low,pivotpoint-1); Quick_sort(T,pivotpoint+1,high); } } int pivot(int T[MAX],int low,int high) { int i,j; int pivotitem,temp; pivotitem=T[low]; i=low; j=high; while(i pivotitem) j--; if(i OUTPUT Enter number of element you want:10 Enter element[0]:56 Enter element[1]:33 Enter element[2]:2 Enter element[3]:90 Enter element[4]:67 Enter element[5]:45 Enter element[6]:43 Enter element[7]:77 Enter element[8]:56 Enter element[9]:2 ------------------------------------------------------------------------------------ Elements before sorting: 56 33 2 90 67 45 43 77 56 2 ---------------------...

Multiplication of Large Integer using Divide and Conquer

#include #include #include long prod(long,long); long noOfDigit(long,long); void main() { long m,n; clrscr(); printf("\n-----------------------------------------------"); printf("\nMULTIPLICATION USING DIVIDE & CONQUER"); printf("\n-----------------------------------------------"); printf("\n Enter number1:"); scanf("%ld",&m); printf("\n Enter number2:"); scanf("%ld",&n); printf("\n-----------------------------------------------"); printf("\n multiplication of %ld and %ld is:%ld",m,n,prod(m,n)); printf("\n-----------------------------------------------"); getch(); } long prod(long u,long v) { long x,y,w,z; long n,m,p,q,r; n=noOfDigit(u,v); if(u==0 || v==0) { return 0; } else if(n =n) max=m; else max=n; while(max>0) { max=max/10; b++; } return b; } OUTPUT ----------------------------------------------- MULTIPLICATION USING DIVI...

Heap sort using Divide & Conquer

#include #include #define MAX 30 struct heap{ int S[MAX]; int heapsize; }H; void siftdown(struct heap* H,int i) { int parent,largerchild; int siftkey; int spotfound; siftkey=H->S[i]; parent=i; spotfound=0; while(2*parent heapsize && spotfound==0) { if(2*parent heapsize && H->S[2*parent] S[2*parent+1]) largerchild=2*parent+1; else largerchild=2*parent; if(siftkey S[largerchild]) { H->S[parent]=H->S[largerchild]; parent=largerchild; } else spotfound=1; } H->S[parent]=siftkey; } int root(struct heap* H) { int keyout; keyout=H->S[1]; H->S[1]=H->S[H->heapsize]; H->heapsize=H->heapsize-1; siftdown(H,1); return keyout; } void removekeys(int n,struct heap* H) { int i; for(i=n;i>=1;i--) { H->S[i]=root(H); } } void makeheap(int n,struct heap* H) { int i; H->heapsize=n; for(i=n/2;i>=1;i--) siftdown(H,i); } void heapsort(int n,struct heap* H) { makeheap(n,H); removekeys(n,H...