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< stdio.h> #include< conio.h> #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<= n;i++) { for(j=1;j<= n;j++) { printf("\n l[%d][%d]:",i,j); scanf("%d",&l[i][j]); } } floyd(l,n); getch(); } void floyd(int l[MAX][MAX],int n) { int D[MAX][MAX],p[MAX][MAX],i,j,k; for(i=1;i<= n;i++) for(j=1;j<= n;j++) { D[i][j]=l[i][j]; p[i][j]=0; } for(k=1;k<= n;k++) { for(i=1;i<= n;i++) { for(j=1;j<= n;j++) { if(D[i][k]+D[k][j] < D[i][j]) { p[i][j]=k; } D[i][j]=min(D[i][j],D[i][k]+D[k][j]); } } printf("\n--k=%d-------D%d---------\t\t ---------P%d------",k,k,k); for(i=1;i<=n;i++) { printf("\n "

Find Longest Common Subsequence using Dynamic Programming in C

#include< stdio.h > #include< stdio.h> #include< conio.h> #include< string.h> #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]

0/1 knapsack problem using Dynamic programming in C

#include< stdio.h > #include< conio.h > #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<=no;i++) { printf("\n Enter Weight and Value for Object %d:",i); scanf("%d %d",&weight[i],&value[i]); } printf("\n Enter knapsack Capacity:"); scanf("%d",&W); knapsackDP(no,W); backtracking(); getch(); } void knapsackDP(int no,int W) { int i,j; for(i=0;i<= W ;i++) v[0][i]=0; for(i=0;i<= no;i++) v[i][0]=0; for(i=1;i<= no;i++) { for(j=1;j<= W;j++) { if((j-weight[i])< 0) v[i][j]=v[i-1][j]; else v[i][j]=max(v[i-1][j],v[i-1][j-weight[i]]+value[i]); } } printf("\n \t "

Merge Sort using Divide and Conquer in C

#include< stdio.h> #include< conio.h> #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 < n;i++) { printf("\n Enter element[%d]:",i); scanf("%d",&T[i]); } printf("\n------------------------------------------"); printf("\n Elements before sorting:"); display(T,n); printf("\n------------------------------------------"); Merge_sort(T,0,n-1); printf("\n------------------------------------------"); printf("\n Elements after sorting:"); display(T,n); printf("\n------------------------------------------"); getch(); } void Merge_sort(int T[MAX],int low,int high) { int mid; if(high > low) { mid=(low+high)/2; Merge_sort(T,low,mid); Merge_sort(T,mid+1,high);

Quick Sort Using Divide an Conquer

#include< stdio.h> #include< conio.h> #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< n;i++) { printf("\n Enter element[%d]:",i); scanf("%d",&T[i]); } printf("\n------------------------------------------"); printf("\n Elements before sorting:"); display(T,n); printf("\n------------------------------------------"); Quick_sort(T,0,n-1); printf("\n------------------------------------------"); printf("\n Elements after sorting:"); display(T,n); printf("\n------------------------------------------"); getch(); } void Quick_sort(int T[MAX],int low,int high) { int pivotpoint; if(high > low) { pivotpoint=pivot(T,low,high); Quick_sort(T,lo

Multiplication of Large Integer using Divide and Conquer

#include< stdio.h > #include< conio.h > #include< math.h > 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<=2) { return (u*v); } else { m=floor(n/2); w=u/pow(10,m); x=u%(int)pow(10,m); y=v/pow(10,m); z=v%(int)

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 <= H->heapsize && spotfound==0) { if(2*parent < H->heapsize && H->S[2*parent] < H->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)