SlideShare a Scribd company logo
PRACTICAL COMPUTING
          CSC305MC3
        ASSIGNMENT-01




         R LOGARAJAH
          2005/SP/30
DEPARTMENT OF COMPUTER SCIENCE
Matlab Coding

Filter

1).Maximum filter:-

S

function 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;
   end
end
I=imread('123.tif');
b=MaximumFilter(I);
subplot(2,2,1); imshow(I);
subplot(2,2,2); imshow(b);
Output:-
2) Minimum filter
function 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;
   end
end

>>I=imread('45.tif');
>> m=mininumfilter(I);
>> subplot(2,2,1);imshow(I);//h
>> subplot(2,2,2);imshow(m);
Output:-
3) Average Filter

function 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;
  end
end


>> I=imread('11.tif');
>> m=AverageFilter(I);
>>subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-
4) Smoothing Filter
function 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;
   end
end
>> I=imread('45.tif');
>> m=Smoothing(I);
>>subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-




5) Median filter
function 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);
  end
end

>>I=imread('11.tif');
>> m=MedianFilter(I);
>> subplot(2,2,1);imshow(I);
>> subplot(2,2,2);imshow(m);
Output:-




6) Difference Filter
function 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;
  end
end
>> I=imread('22.tif');
>>Im=DifferenceFilter(I);
>>subplot(1,2,1),imshow(I);
>>subplot(1,2,2),imshow(Im);
Output:-




7) Prewitt Filter

function 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 filter
function 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;
end
I=imread('55.tif');
Im=UnsharpMask(I,0.5);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(Im)




10) Robert filter
function 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));
end
I=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;
   end
end
I=imread('122.tif');

Im=Gussianfilter(I);

subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(Im)

output:-
Programming in Java
1) 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 QuickSort
How many elements :7
array[0] :22
array [1] :11
array [2] :55
array [3] :12
array[4] :99
array[5] :77
array[6] :55
The Array Elements are: 22 11 55 12 99 77 55
The Array Elements are: 11 12 22 55 55 77 99
Quick sort:-
Partition procedure
Step1:- p- startindex.
                r- lastidex.
Step2:-If startindex < lastidex then
step3:-q      Partition(array,p,r)
                         Quick Sort (array, p, q)
                         Quick Sort (array, q + r, r)
Step4:-pivot          array[p]
            i        p
            j       r+1
step5:-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 j
step:-stop.



Best Case

Produces two regions of size n/2

T (n)=T(n/2)+T(N/2)+Ѳ(n)

T (n) = Ѳ (n lg n)

Worst case

T(n)= Ѳ (n2)

Average case

T (n) = Ѳ (n lg (n))
Conclusion:-
Compare to quick sort running time, worst case >average case>best case




2)
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 output


C:UserslograjahDesktop>java PerfectNumber
Enter any number =6
PerfectNumber are:
1
2
3
number is a perfect number
C:UserslograjahDesktop>java PerfectNumber
Enter any number =100
PerfectNumber are:
1
2

4

5

10

20

25

50

number is not a perfect number
4)
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 GCD
Enter the first number: 12
Enter the second number: 6
The GCD is: 6
C:UserslograjahDesktop>java GCD
Enter the first number: 56
Enter the second number: 48
The GCD is: 8


5) 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 palindrome
Enter the string :-radar
Given string after ignore special characters is: RADAR
Given word or phrase is palindrome
I:Assignment>java palindrome
Enter the string :-won't lovers revolt now?
Given string after ignore special characters is: WONTLOVERSREVOLTNOW
Given word or phrase is palindrome



6) Tower of Hanoi
function[] = 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 B



7) Give area may be found shortest path
import 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 shortpath
Enter the value for n & m 1st matrix
4
4
Enter the elements for 1st matrix(0 or 1)
1
2
3
6
5
4
7
8
9
4
5
6
7
8
9
4
Enter the position of source(x,y)
0
0
Enter 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 class



public class DieselCar extends Car
{
   public DieselCar(String name)
   {
        super(name);
   }
   public String toString()
   {
        return String.format("%s","many thanks for your custom");
   }
}




Car class
public 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):
               1
Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000

More Related Content

PDF
C programs
PDF
DSU C&C++ Practical File Diploma
PDF
Java Practical File Diploma
PDF
Bartosz Milewski, “Re-discovering Monads in C++”
PPTX
Java simple programs
DOCX
DAA Lab File C Programs
PDF
Rainer Grimm, “Functional Programming in C++11”
DOC
Final JAVA Practical of BCA SEM-5.
C programs
DSU C&C++ Practical File Diploma
Java Practical File Diploma
Bartosz Milewski, “Re-discovering Monads in C++”
Java simple programs
DAA Lab File C Programs
Rainer Grimm, “Functional Programming in C++11”
Final JAVA Practical of BCA SEM-5.

What's hot (20)

PDF
The Ring programming language version 1.4 book - Part 9 of 30
DOCX
Java practical
PPTX
Chapter 7 functions (c)
PDF
The Ring programming language version 1.5.3 book - Part 10 of 184
PPTX
Java весна 2013 лекция 2
PDF
&Y tgs P kii for
PDF
The Ring programming language version 1.5.4 book - Part 10 of 185
PDF
JVM Mechanics
PPTX
Monadic Comprehensions and Functional Composition with Query Expressions
PDF
The Ring programming language version 1.3 book - Part 24 of 88
PDF
C Prog - Array
PDF
Developer Experience i TypeScript. Najbardziej ikoniczne duo
DOC
PDF
Computer java programs
PDF
JVM Mechanics: Understanding the JIT's Tricks
PDF
Let the type system be your friend
PDF
Microsoft Word Hw#1
PDF
design and analysis of algorithm Lab files
PDF
From NumPy to PyTorch
PDF
The Ring programming language version 1.7 book - Part 91 of 196
The Ring programming language version 1.4 book - Part 9 of 30
Java practical
Chapter 7 functions (c)
The Ring programming language version 1.5.3 book - Part 10 of 184
Java весна 2013 лекция 2
&Y tgs P kii for
The Ring programming language version 1.5.4 book - Part 10 of 185
JVM Mechanics
Monadic Comprehensions and Functional Composition with Query Expressions
The Ring programming language version 1.3 book - Part 24 of 88
C Prog - Array
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Computer java programs
JVM Mechanics: Understanding the JIT's Tricks
Let the type system be your friend
Microsoft Word Hw#1
design and analysis of algorithm Lab files
From NumPy to PyTorch
The Ring programming language version 1.7 book - Part 91 of 196
Ad

Similar to PRACTICAL COMPUTING (20)

PDF
Please help with this JAVA Assignment and show output if you can ple.pdf
DOCX
cs3381-object oriented programming-ab-manual
PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
PDF
programs on arrays.pdf
PDF
Java Simple Programs
PPTX
Algorithms - "Chapter 2 getting started"
DOCX
DOCX
object oriented programming lab manual .docx
PDF
C++ Searching & Sorting5. Sort the following list using the select.pdf
PPTX
LectureSlidData_sturcture_algorithm_v2.pptx
DOC
Daapracticals 111105084852-phpapp02
DOCX
QA Auotmation Java programs,theory
DOC
Sorting programs
PDF
Hello, I need some assistance in writing a java program THAT MUST US.pdf
DOCX
Array
PPTX
rmrfnel;,;'sdc,hddguydgudfufdydydhfjguyju8y8
KEY
Why Learn Python?
PDF
ISCP internal.pdf
PDF
81818088 isc-class-xii-computer-science-project-java-programs
Please help with this JAVA Assignment and show output if you can ple.pdf
cs3381-object oriented programming-ab-manual
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
programs on arrays.pdf
Java Simple Programs
Algorithms - "Chapter 2 getting started"
object oriented programming lab manual .docx
C++ Searching & Sorting5. Sort the following list using the select.pdf
LectureSlidData_sturcture_algorithm_v2.pptx
Daapracticals 111105084852-phpapp02
QA Auotmation Java programs,theory
Sorting programs
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Array
rmrfnel;,;'sdc,hddguydgudfufdydydhfjguyju8y8
Why Learn Python?
ISCP internal.pdf
81818088 isc-class-xii-computer-science-project-java-programs
Ad

More from Ramachendran Logarajah (7)

PDF
A/L ICT syllabus
PDF
computer network
PDF
PPTX
BLOOD BANK SOFTWARE PRESENTATION
PPTX
point operations in image processing
A/L ICT syllabus
computer network
BLOOD BANK SOFTWARE PRESENTATION
point operations in image processing

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
01-Introduction-to-Information-Management.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Structure & Organelles in detailed.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Lesson notes of climatology university.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
01-Introduction-to-Information-Management.pdf
Complications of Minimal Access Surgery at WLH
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Structure & Organelles in detailed.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Computing-Curriculum for Schools in Ghana
Lesson notes of climatology university.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Supply Chain Operations Speaking Notes -ICLT Program

PRACTICAL COMPUTING

  • 1. PRACTICAL COMPUTING CSC305MC3 ASSIGNMENT-01 R LOGARAJAH 2005/SP/30 DEPARTMENT OF COMPUTER SCIENCE
  • 2. Matlab Coding Filter 1).Maximum filter:- S function 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; end end I=imread('123.tif'); b=MaximumFilter(I); subplot(2,2,1); imshow(I); subplot(2,2,2); imshow(b); Output:-
  • 3. 2) Minimum filter function 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; end end >>I=imread('45.tif'); >> m=mininumfilter(I); >> subplot(2,2,1);imshow(I);//h >> subplot(2,2,2);imshow(m); Output:-
  • 4. 3) Average Filter function 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; end end >> I=imread('11.tif'); >> m=AverageFilter(I); >>subplot(2,2,1);imshow(I); >> subplot(2,2,2);imshow(m); Output:-
  • 5. 4) Smoothing Filter function 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; end end >> I=imread('45.tif'); >> m=Smoothing(I); >>subplot(2,2,1);imshow(I); >> subplot(2,2,2);imshow(m); Output:- 5) Median filter function Img=MedianFilter(Img) [row,col]=size(Img);
  • 6. 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); end end >>I=imread('11.tif'); >> m=MedianFilter(I); >> subplot(2,2,1);imshow(I); >> subplot(2,2,2);imshow(m); Output:- 6) Difference Filter function 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
  • 7. p=CopyImg(x+i,y+j); c=filter(i+2,j+2); sum=sum+c*p; end end q=sum; Img(x,y)=q; end end >> I=imread('22.tif'); >>Im=DifferenceFilter(I); >>subplot(1,2,1),imshow(I); >>subplot(1,2,2),imshow(Im); Output:- 7) Prewitt Filter function 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);
  • 8. >>subplot(1,2,1),imshow(I); >>subplot(1,2,2),imshow(Im) Output:- 8) Sobel filter function 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
  • 9. function Img=UnsharpMask(Img,a) CopyImg=Img; D1=Smoothing(CopyImg); I=(1+a).*CopyImg; J=a.*D1; Img=I-J; end I=imread('55.tif'); Im=UnsharpMask(I,0.5); subplot(1,2,1),imshow(I); subplot(1,2,2),imshow(Im) 10) Robert filter function 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)); end I=imread('7.tif'); Im=RobertFilter(Im); subplot(1,2,1),imshow(I); subplot(1,2,2),imshow(Im) output:- 11) Gaussian filter
  • 10.  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; end end I=imread('122.tif'); Im=Gussianfilter(I); subplot(1,2,1),imshow(I); subplot(1,2,2),imshow(Im) output:-
  • 11. Programming in Java 1) 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++) {
  • 12. 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 ) {
  • 13. 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 QuickSort How many elements :7 array[0] :22 array [1] :11 array [2] :55 array [3] :12 array[4] :99 array[5] :77 array[6] :55 The Array Elements are: 22 11 55 12 99 77 55 The Array Elements are: 11 12 22 55 55 77 99 Quick sort:- Partition procedure Step1:- p- startindex. r- lastidex. Step2:-If startindex < lastidex then step3:-q Partition(array,p,r) Quick Sort (array, p, q) Quick Sort (array, q + r, r) Step4:-pivot array[p] i p j r+1 step5:-while do
  • 14. 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 j step:-stop. Best Case Produces two regions of size n/2 T (n)=T(n/2)+T(N/2)+Ѳ(n) T (n) = Ѳ (n lg n) Worst case T(n)= Ѳ (n2) Average case T (n) = Ѳ (n lg (n)) Conclusion:- Compare to quick sort running time, worst case >average case>best case 2) Import java .util.*; public class PerfectNumber { static int n; static int pNo = 0; public static void main(String []args)
  • 15. { 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 output C:UserslograjahDesktop>java PerfectNumber Enter any number =6 PerfectNumber are: 1 2 3 number is a perfect number
  • 16. C:UserslograjahDesktop>java PerfectNumber Enter any number =100 PerfectNumber are: 1 2 4 5 10 20 25 50 number is not a perfect number 4) 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
  • 17. 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 GCD Enter the first number: 12 Enter the second number: 6 The GCD is: 6 C:UserslograjahDesktop>java GCD Enter the first number: 56 Enter the second number: 48 The GCD is: 8 5) 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)); } }
  • 18. 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 palindrome Enter the string :-radar Given string after ignore special characters is: RADAR Given word or phrase is palindrome
  • 19. I:Assignment>java palindrome Enter the string :-won't lovers revolt now? Given string after ignore special characters is: WONTLOVERSREVOLTNOW Given word or phrase is palindrome 6) Tower of Hanoi function[] = 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
  • 20. move disk 1 source A destination B 7) Give area may be found shortest path import 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)
  • 21. { 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+")");
  • 22. }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 shortpath Enter the value for n & m 1st matrix 4 4 Enter the elements for 1st matrix(0 or 1) 1 2 3 6 5 4 7 8 9 4 5 6 7 8 9 4 Enter the position of source(x,y) 0 0 Enter the position of Destination(x,y) 3
  • 23. 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 class public class DieselCar extends Car { public DieselCar(String name) { super(name); } public String toString() { return String.format("%s","many thanks for your custom"); } } Car class public class Car { String Customername; public Car(String name) { Customername=name;
  • 24. 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()); }
  • 25. 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): 1 Dear loga, Please find enclosed a complimentary voucher for the amount of Rs.5000