Monday 26 February 2018

ANALYSIS OF MERGE SORT C PROGRAM

#include<stdio.h>
#include<stdlib.h>
#define MAX 10

void combine(int a[MAX],int low,int mid,int high)
{
int i,j,k;
int temp[MAX];
k=low;             //k AS INDEX FOR ARRAY temp
i=low;   //i AS INDEX FOR LEFT SUBARRAY
j=mid+1;   //j AS INDEX FOR RIGHR SUBARRAY
while(i<=mid && j<=high)
{
if(a[i]<=a[j])
{
temp[k]=a[i];
i++;
k++;
}
else
{
temp[k]=a[j];
j++;
k++;
}
}
while(i<=mid)      //COPYING REMAINING ELEMENT OF LEFT SUBARRAY
{
temp[k]=a[i];
i++;
k++;
}
while(j<=high)     //COPYING REMAINING ELEMENT OF LEFT SUBARRAY
{
temp[k]=a[j];
j++;
k++;
}
for(k=low;k<=high;k++)
a[k]=temp[k];
}
void Display(int a[MAX],int n)
{
int i;
printf("THE SORTED ARRAY IS :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

void MergeSort(int a[MAX],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;          //SPLIT THE LIST AT MID
MergeSort(a,low,mid);    //LEFT SUBARRAY
MergeSort(a,mid+1,high);   //RIGHT SUBARRAY
combine(a,low,mid,high);   //MERGING OF TWO SUBARRAY
}
}

int main()
{
int i,low=0,n;
printf("MERGE SORT :: \n");
printf("ENTER THE LENGTH OF LIST : ");
scanf("%d",&n);
int a[MAX];
printf("ENTER THE ELEMENT OF A LIST :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
MergeSort(a,low,n-1);
Display(a,n);
}

OUTPUT :
------------------------------------
MERGE SORT ::
ENTER THE LENGTH OF LIST : 5
ENTER THE ELEMENT OF A LIST :
85
9
36
45
25
THE SORTED ARRAY IS :
9 25 36 45 85

-------------------------------------

ANALYSIS OF BINARY SEARCH C PROGRAM

#include<stdio.h>
#include<stdlib.h>
#define MAX 10

int binary_search(int a[],int n,int x)
{
int mid=n/2;
int high=n-1,low=0;
while(high>=low)
{
if(a[mid]==x)
return mid;
if(x>a[mid])
low=mid+1;       //SEARCH THE RIGHT SUBARRAY
else
high=mid-1;      //SEARCH THE LEFT SUBARRAY
mid=(low+high)/2;
}
return -1;
}

int main()
{
int i,a[MAX],no,x;
printf("ENTER THE NO OF ELEMENTS :: ");
scanf("%d",&no);
printf("ENTER THE ELEMENTS OF AN ARRAY :: \n");
for(i=0;i<no;i++)
scanf("%d",&a[i]);
printf("THE NO YOU WANT TO SEARCH::");
scanf("%d",&x);
if(binary_search(a,no,x)!=-1)
printf("\nELEMENT FOUND AT %d INDEX IN AN ARRAY",binary_search(a,no,x));
else
printf("\nTHE ELEMENT IS NOT FOUND");
}

OUTPUT :
-------------------------------------------
ENTER THE NO OF ELEMENTS :: 4
ENTER THE ELEMENTS OF AN ARRAY ::
55
85
68
18
THE NO YOU WANT TO SEARCH::18

ELEMENT FOUND AT 3 INDEX IN AN ARRAY

--------------------------------------------

Wednesday 7 February 2018

PROGRAM TO IMPLEMENT DECISION MAKING,LOOPING,CONTROL STATEMENTS

import java.util.*;// since we are creating object of scanner class
class Loops
{
public static void main(String args[])
{
int i,j,fact=1,rev=0,r,ch,n;
do{
System.out.println("\n\t ****Menu****");
System.out.println("1. find ood even number(if el)");
System.out.println("2. Factorial of a number(for loop)");
System.out.println("3. print partter of a(right angle triangle inserted for loop)");
System.out.println("4. reverse of number (while loop)");
System.out.println("5. Exit\n");
System.out.print("Enter Choice");
Scanner sc=new Scanner(System.in);
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("enter number ");
n=sc.nextInt();
if(n%2==0)
{
System.out.println("entered no."+n+" is even");
}
else
{
System.out.println("entered no."+n+" is odd");
}
break;


case 2:
System.out.println("enter number ");
n=sc.nextInt();
for (i=2;i<=n;i++)
{
fact=fact*i;
}
System.out.println("FActorial of "+n+";"+fact);
break;

case 3:
for(i=1;i<=4;i++)
{//start outer for loop
for(j=1;j<=i;j++)
{//start outer for loop
System.out.print("*");
}//end of outer for loop
System.out.println(" ");
}//end outer for loop
break;

case 4:
System.out.println("enter number");
n=sc.nextInt();
while(n!=0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
System.out.println("reverse of given number:" +rev);
break;

case 5:
System.exit(0);
break;

default:
System.out.println("please enter the valid choice");
}//end of while
}while(true);
}//end of main
}//end of class

Output:

      ****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit

Enter Choice1
enter number
3
entered no.3 is odd

****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit

Enter Choice2
enter number
5
FActorial of 5;120

****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit

Enter Choice3
*
**
***
****

****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit

Enter Choice4
enter number
1234
reverse of given number:4321

****Menu****
1. find ood even number(if el)
2. Factorial of a number(for loop)
3. print partter of a(right angle triangle inserted for loop)
4. reverse of number (while loop)
5. Exit

Enter Choice5


------------------
(program exited with code: 0)
Press return to continue

Java program to perform Circular convolution of periodic sequence (DSP)

import java.util.*;

class ConcentricMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int x1Length,x2Length,i=0,j=0;

// First Sequence
System.out.println("Enter the length of first sequence");
x1Length = sc.nextInt();

int x1[][]=new int[x1Length][x1Length];
int x2[]=new int[x1Length];
int y[]=new int[x1Length];

System.out.println("Enter first sequence");

for(i=0;i<x1Length;i++)
x1[0][i] = sc.nextInt();


// Second Sequence
System.out.println("Enter the length of second sequence");
x2Length = sc.nextInt();


System.out.println("Enter second sequence");

for(i=0;i<x2Length;i++)
x2[i] = sc.nextInt();

// Padding
if(x1Length>x2Length)
{
for(i=x1Length;i<x2Length;i++)
x2[i]=0;
}

// Circular Matrix
for(i=1;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
if(j-1<0)
x1[i][j]=x1[i-1][x1Length-1];
else
x1[i][j]=x1[i-1][j-1];

}
}


// Print Circular Matrix
System.out.println("\nCircular Matrix");
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
System.out.print(x1[j][i] + "\t");
}
System.out.println("");
}


// Perform Circular Matrix
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
y[i]+=(x1[j][i]*x2[j]);
}
}

// Print Output
System.out.println("\nCircular Convolution - y(n)");
for(i=0;i<x1Length;i++)
{
System.out.print(y[i] + "\t");
}

}
}

OUTPUT:

Enter the length of first sequence
4
Enter first sequence
1
2
3
4
Enter the length of second sequence
3
Enter second sequence
7
0
3

Circular Matrix
1 4 3 2
2 1 4 3
3 2 1 4
4 3 2 1

Circular Convolution - y(n)
16 26 24 34

Program to understand Continuebreak Label

class Continuebreak
  {
  public static void main(String args[])
  {
  LOOP1:for(int i=1;i<100;i++)
  {
  System.out.println(" ");
  if(i>=10) break;
  LOOP2:for(int j=1;j<100;j++)
  {
  System.out.print(" * ");
  if(j==i)
  continue LOOP1;
  }
  }
  System.out.println("terminated by break LOOP1");
  }
  }

Output:
 
   *
   *  *
   *  *  *
   *  *  *  *
   *  *  *  *  *
   *  *  *  *  *  *
   *  *  *  *  *  *  *
   *  *  *  *  *  *  *  *
   *  *  *  *  *  *  *  *  *
  terminated by break LOOP1

Program to understand creation of classes,object and method

class Room
{
float length;
float breadth;
void getdata(float a,float b)
{
length=a;
breadth=b;

}

}

class RoomArea
{
public static void main(String args[])
{
float area;
Room R1=new Room();//creating object of class Room
R1.getdata(14,10);//calling method of Room class
area=R1. length*R1.breadth;//calling variables of Room class
System.out.println("Area of Room is:"+area);
}
}

Java Program to accept data through keyboard

import java.util.*;
import java.io.*;

class input 
{
 public static void main (String args[]) throws Exception
 {
   int i;
   Scanner sc=new Scanner(System.in);
   System.out.println("enter a number");
   i=sc.nextInt();
   System.out.println("entered number is:"+i);
 
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("enter a statement");
   String s=br.readLine();
   System.out.println("enter line is:"+s);
}
}

Output:
enter a number
10
entered number is:10
enter a statement
we are engineers
enter line is:we are boys