PRACTICAL COMPUTING
Upcoming SlideShare
Loading in...5
×
 

PRACTICAL COMPUTING

on

  • 639 views

matlab And java coding

matlab And java coding

Statistics

Views

Total Views
639
Views on SlideShare
639
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PRACTICAL COMPUTING PRACTICAL COMPUTING Document Transcript

  • PRACTICAL COMPUTING CSC305MC3 ASSIGNMENT-01 R LOGARAJAH 2005/SP/30DEPARTMENT OF COMPUTER SCIENCE
  • Matlab CodingFilter1).Maximum filter:-Sfunction Img=MaximumFilter(Img)[row,col]=size(Img);copyImg=Img;filter=[1 1 1;1 1 1;1 1 1];filter1=zeros(3,3);for x=2:row-1 for y=2:col-1 for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); c=filter(j+2,i+2); filter1(j+2,i+2)=c*p; end end q=max(max(filter1)); Img(x,y)=q; endendI=imread(123.tif);b=MaximumFilter(I);subplot(2,2,1); imshow(I);subplot(2,2,2); imshow(b);Output:-
  • 2) Minimum filterfunction Img=mininumfilter(Img)[row,col]=size(Img);copyImg=Img;filter=[1 1 1;1 1 1;1 1 1];filter1=zeros(3,3);for x=2:row-1 for y=2:col-1 for i=-1:1 for j=-1:1 c=copyImg(x+i,y+j); p=filter(j+2,i+2); filter1(j+2,i+2)=c*p; end end q=min(min(filter1)); Img(x,y)=q; endend>>I=imread(45.tif);>> m=mininumfilter(I);>> subplot(2,2,1);imshow(I);//h>> subplot(2,2,2);imshow(m);Output:-
  • 3) Average Filterfunction Img=AverageFilter(Img)[row,col]=size(Img);copyImg=Img;for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); sum=sum+p; end end q=sum/2; Img(x,y)=q; endend>> I=imread(11.tif);>> m=AverageFilter(I);>>subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m);Output:-
  • 4) Smoothing Filterfunction Img=Smoothing(Img)[row,col]=size(Img);copyImg=Img;filter=[0.075 0.125 0.175;0.125 0.200 0.125;0.075 0.125 0.175];for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 p=copyImg(x+i,y+j); c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endend>> I=imread(45.tif);>> m=Smoothing(I);>>subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m);Output:-5) Median filterfunction Img=MedianFilter(Img)[row,col]=size(Img);
  • k=4;p=zeros(2*k+1);copyImg=Img;for x=2:row-1 for y=2:col-1 m=1; for i=-1:1 for j=-1:1 p(m)=copyImg(x+i,y+j); m=m+1; end end sort(p); Img(x,y)=p(k); endend>>I=imread(11.tif);>> m=MedianFilter(I);>> subplot(2,2,1);imshow(I);>> subplot(2,2,2);imshow(m);Output:-6) Difference Filterfunction Img=DifferenceFilter(Img)[row,column]=size(Img);CopyImg=Img;filter=[0 0 -1 0 0; 0 -1 2 -1 0; -1 2 16 2 -1;0 -1 2 -1 0;0 0 -1 0 0];for x=2:row-1 for y=2:column-1 sum=0; for i=-1:1 for j=-1:1
  • p=CopyImg(x+i,y+j); c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endend>> I=imread(22.tif);>>Im=DifferenceFilter(I);>>subplot(1,2,1),imshow(I);>>subplot(1,2,2),imshow(Im);Output:-7) Prewitt Filterfunction Img=PrewittFilter(Img) CopyImg=Img; Hx=[-1 0 1 ; -1 0 1; -1 0 1]; Hy=[-1 -1 -1; 0 0 0 ; 1 1 1]; Dx=conv2(CopyImg ,Hx); Dy=conv2(CopyImg ,Hy); Img=round(sqrt(Dx.*Dx+Dy.*Dy));end>>I=imread(44.tif);>>Im=PrewittFilter(I);
  • >>subplot(1,2,1),imshow(I);>>subplot(1,2,2),imshow(Im)Output:-8) Sobel filterfunction Img=SobelFilter(Img) CopyImg=Img; Hx=[-1 0 1 ; -2 0 2; -1 0 1]; Hy=[-1 -2 -1; 0 0 0 ; 1 2 1]; Dx=conv2(CopyImg ,Hx); Dy=conv2(CopyImg ,Hy); Img=round(sqrt(Dx.*Dx+Dy.*Dy));end>>I=imread(44.tif);>>Im=SobelFilter(I);>>subplot(1,2,1),imshow(I);>>subplot(1,2,2),imshow(Im)Output:-9) UnsharpMask
  • function Img=UnsharpMask(Img,a) CopyImg=Img; D1=Smoothing(CopyImg); I=(1+a).*CopyImg; J=a.*D1; Img=I-J;endI=imread(55.tif);Im=UnsharpMask(I,0.5);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(Im)10) Robert filterfunction Img=RobertFilter(Img) CopyImg=Img; H0=[0 1;-1 0]; H1=[-1 0;0 1]; D0=conv2(CopyImg ,H0); D1=conv2(CopyImg ,H1); Img=round(sqrt(D0.*D0+D1.*D1));endI=imread(7.tif);Im=RobertFilter(Im);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(Im)output:-11) Gaussian filter
  •  Gaussian kernel.function kernal=makeGaussian(sigma)center=3*sigma;kernal=zeros(2*center+1);sigma2=sigma*sigma;for i=1:size(kernal)-1 r=center-i; kernal(i)=exp(-0.5*(r*r)/sigma2);end===================================function Img=Gussianfilter(Img)[row,col]=size(Img);copyImg=Img;filter=makeGaussian(5);for x=2:row-1 for y=2:col-1 sum=0; for i=-1:1 for j=-1:1 c=copyImg(x+i,y+j); p=filter(j+2,i+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; endendI=imread(122.tif);Im=Gussianfilter(I);subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(Im)output:-
  • Programming in Java1) Quick Sort in the array elements import java.util.*; public class QuickSort { static int n; static int[] array; Scanner s; public QuickSort() {s=new Scanner(System.in); System.out.print("How many elements :"); n=s.nextInt(); array=new int[n]; } public void readArray() { for (int i=0;i<array.length;i++) { System.out.print("array["+i+"] :"); array[i]=s.nextInt(); } } public void printArray() { System.out.print("The Array Elements are :"); for (int i=0;i<array.length;i++) {
  • System.out.print(array[i]+" ");}System.out.println();}public void quickSort(int[] array,int startIndex,int endIndex){ if(startIndex<endIndex){ int j=partition(startIndex,endIndex); quickSort(array,startIndex,j-1); quickSort(array,j+1,endIndex);}} public int partition(int startIndex,int endIndex){ int i=startIndex; int j=endIndex+1; int pivot=array[startIndex]; do { do { i=i+1; }while(array[i]<pivot&&i<endIndex); do { j=j-1; }while(array[j]>pivot&&j>startIndex); if(i<j) { swap(array,i,j); } }while(i<j); swap(array,startIndex,j); return j; } public void swap(int[] array, int index1, int index2 ) {
  • int tmp = array[index1]; array[index1]=array[index2]; array[index2]=tmp; } public static void main(String args[]) { QuickSort q=new QuickSort(); q.readArray(); q.printArray(); q.quickSort(array,0,array.length-1); q.printArray(); } } Output:-F:Java>java QuickSortHow many elements :7array[0] :22array [1] :11array [2] :55array [3] :12array[4] :99array[5] :77array[6] :55The Array Elements are: 22 11 55 12 99 77 55The Array Elements are: 11 12 22 55 55 77 99Quick sort:-Partition procedureStep1:- p- startindex. r- lastidex.Step2:-If startindex < lastidex thenstep3:-q Partition(array,p,r) Quick Sort (array, p, q) Quick Sort (array, q + r, r)Step4:-pivot array[p] i p j r+1step5:-while do
  • Repeat j j-1 Until array[j]≤ pivot Repeat i i+1 until array[i] ≥ pivot If i < j then swap(array,i,j) else return jstep:-stop.Best CaseProduces two regions of size n/2T (n)=T(n/2)+T(N/2)+Ѳ(n)T (n) = Ѳ (n lg n)Worst caseT(n)= Ѳ (n2)Average caseT (n) = Ѳ (n lg (n))Conclusion:-Compare to quick sort running time, worst case >average case>best case2)Import java .util.*;public class PerfectNumber{ static int n; static int pNo = 0; public static void main(String []args)
  • { System.out.print("Enter any number ="); Scanner s = new Scanner(System.in); n= s.nextInt(); System.out.println("PerfectNumber are:"); for (int i = 1; i < n; i++) { if (n % i == 0) { pNo += i; System.out.println(i); } } if (pNo == n) { System.out.println("number is a perfect number"); } else { System.out.println ("number is not a perfect number"); }}}Compiler and outputC:UserslograjahDesktop>java PerfectNumberEnter any number =6PerfectNumber are:123number is a perfect number
  • C:UserslograjahDesktop>java PerfectNumberEnter any number =100PerfectNumber are:124510202550number is not a perfect number4)The greatest common divisor (GCD)Import java.uti1l.*;Public class GCD{ Static int a,b; Public GCD() { Scanner s=new Scanner(System.in); System.out.print("Enter the first number: "); a=s.nextInt(); System.out.print("Enter the second number: "); b=s.nextInt(); } Public int gcd(int a,int b) { if(b==0) return a; else if(a<b) return gcd(b,a); else
  • return gcd(b,a%b); } public static void main(String args[]) { GCD g=new GCD(); System.out.println("The GCD is: "+g.gcd(a,b)); }}Output:-C:UserslograjahDesktop>java GCDEnter the first number: 12Enter the second number: 6The GCD is: 6C:UserslograjahDesktop>java GCDEnter the first number: 56Enter the second number: 48The GCD is: 85) A palindrome is a number or a text phrase that reads the same back and forth.Import java.util.*;public class palindrome{ String word; String copyword=""; public Stack s=new Stack(); public palindrome() { Scanner input=new Scanner(System.in); System.out.print("Enter the string :-"); word=input.nextLine(); word=word.toUpperCase(); filter(); System.out.println("Given string after ignore special characters is: "+copyword); for(int i=0;i<copyword.length();i++) { s.push((Object)copyword.charAt(i)); } }
  • public void filter() { for(int i=0;i<word.length();i++) { char ch=word.charAt(i); if(ch>=48 && ch<=57 || ch>=65 && ch<=90) copyword+=ch; } } public void checkpalin() { for(int i=0;i<copyword.length();i++) { if(s.peek()==(Object)copyword.charAt(i)) s.pop(); else break; } } public void getResult() { if(s.empty()) System.out.println("Given word or phrase is palindrome"); else System.out.println("Given word or phrase is not palindrome"); } public static void main(String args[]) { palindrome p=new palindrome(); p.checkpalin(); p.getResult(); }}Output:-I:Assignment>java palindromeEnter the string :-radarGiven string after ignore special characters is: RADARGiven word or phrase is palindrome
  • I:Assignment>java palindromeEnter the string :-wont lovers revolt now?Given string after ignore special characters is: WONTLOVERSREVOLTNOWGiven word or phrase is palindrome6) Tower of Hanoifunction[] = towers(n,source,destination,tmp) if (n == 1)fprintf(t move disk 1 source %c destination %c n,source,destination);else towers(n-1,source,tmp,destination); fprintf(t move disk %d source %c destination %c n,n,source,destination);towers(n-1,tmp,destination,source); end>> n= input(Towers of Hanoi: How many disks?n);fprintf(nn)towers(n,A,B,C);fprintf(nn);Towers of Hanoi: How many disks?3 move disk 1 source A destination B move disk 2 source A destination C move disk 1 source B destination C move disk 3 source A destination B move disk 1 source C destination A move disk 2 source C destination B
  • move disk 1 source A destination B7) Give area may be found shortest pathimport java.util.*;class shortpath{ public static int x1,x2,y1,y2,n,m; public static int a[][]=new int[10][10],parent[][]=new int[10][10],dist[][]=new int[10][10]; public static void main(String s[]) { Scanner s1=new Scanner(System.in); int x,y,num; LinkedList l=new LinkedList(); LinkedList path=new LinkedList(); System.out.println("Enter the value for n & m 1st matrix"); n=s1.nextInt(); m=s1.nextInt(); System.out.println("Enter the elements for 1st matrix(0 or 1)"); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { a[i][j]=s1.nextInt(); parent[i][j]=-1; dist[i][j]=n*m; } System.out.println("Enter the position of source(x,y)"); x1=s1.nextInt(); y1=s1.nextInt(); System.out.println("Enter the position of Destination(x,y)"); x2=s1.nextInt(); y2=s1.nextInt(); l.add(getnum(x1,y1)); dist[x1][y1]=0; while(l.size()!=0)
  • { num=Integer.parseInt(l.removeLast().toString()); x=num/m; y=num%m; if(x>0&&a[x-1][y]!=1&&(dist[x-1][y]>(dist[x][y])+1)) { l.add(getnum(x-1,y)); dist[x-1][y]=dist[x][y]+1; parent[x-1][y]=num; } if(y>0&&a[x][y-1]!=1&&(dist[x][y-1]>(dist[x][y])+1)) { l.add(getnum(x,y-1)); dist[x][y-1]=dist[x][y]+1; parent[x][y-1]=num; } if(x<n-1&&a[x+1][y]!=1&&(dist[x+1][y]>(dist[x][y])+1)) { l.add(getnum(x+1,y)); dist[x+1][y]=dist[x][y]+1; parent[x+1][y]=num; } if(y<m-1&&a[x][y+1]!=1&&(dist[x][y+1]>(dist[x][y])+1)) { l.add(getnum(x,y+1)); dist[x][y+1]=dist[x][y]+1; parent[x][y+1]=num; }}path.add("("+x2+","+y2+")");x=x2;y=y2;do{num=parent[x][y];x=num/m;y=num%m;path.add("("+x+","+y+")");
  • }while(!(x==x1&&y==y1)); while(path.size()!=0) System.out.println(path.removeLast()); } public static Integer getnum(int x,int y) { return new Integer(x*m+y); }}Output:-I:Assignment>java shortpathEnter the value for n & m 1st matrix44Enter the elements for 1st matrix(0 or 1)1236547894567894Enter the position of source(x,y)00Enter the position of Destination(x,y)3
  • 3(0,0)(0,1)(0,2)(0,3)(1,3)(2,3)(3,3)10) A car dealer, who sells petrol and diesel car, need to print personalized letters for customers according to the type of car they bough.Diesel Car classpublic class DieselCar extends Car{ public DieselCar(String name) { super(name); } public String toString() { return String.format("%s","many thanks for your custom"); }}Car classpublic class Car{ String Customername; public Car(String name) { Customername=name;
  • setName(name); } public void setName(String name) { Customername=name; } public String getName() { return Customername; } public String toString1() { return String.format("%s %s, ","Dear",Customername); }}Customer Directory class(main)import java.util.*;public class CustomerDirectory{ static Scanner s=new Scanner(System.in); static String n; static int t; public static void main(String args[]) { System.out.println("Enter the Name: "); n=s.nextLine(); System.out.println("Enter the Car type(petrolcar=1 or dieselcar=0):"); t=s.nextInt(); if(t==1) {PetrolCar pc=new PetrolCar(n); System.out.println(pc.toString1()+pc.toString()); } else if(t==0) { DieselCar dc=new DieselCar(n); System.out.println(dc.toString1()+dc.toString()); }
  • else System.out.println("another Type"); }}Output:-I:Assignment>javac CustomerDirectory.java I:Assignment>java CustomerDirectory Enter the Name: loga Enter the Car type(petrolcar=1 or dieselcar=0): 1Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000