2D Add: Write A Program To Add Two One Dimensional Array
2D Add: Write A Program To Add Two One Dimensional Array
2D Add
Write a program to add two one dimensional array
class ArrayAdd
public void sum(ArrayAdd x,ArrayAdd y ) // function to add two arrays by passing two objects
public ArrayAdd sum(ArrayAdd y ) – add two arrays using passing object and calling object
Answer
import java.io.*;
size = x;
}
2
String S;
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
S=br.readLine();
a[i][j]=Integer.parseInt(S);
int i,j;
for(j=0;j<size;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
int i,j;
for(i=0;i<size;i++)
for(j=0;j<size;j++)
z.a[i][j]= a[i][j] + y.a[i][j]; // calling object and passing object is added to the created object
//CLASS II contains the main programe to make use of the class ArrayAdd
import java.io.*;
System.out.println( );
System.out.println();
OUTPUT
Enter number
5
Enter number
6
Enter number
7
Enter number
8
Enter number
9
1 2 3
4 5 6
7 8 9
9 8 7
6 5 4
3 2 1
BinarySearch
Write a program to search for an element an array using Binary Searching technique
import java.io.*;
size=x;
a=new int[size];
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(br.readLine());
int i;
for(i=0;i<size;i++)
7
System.out.print(a[i]+"\t");
System.out.println();
int i,j,temp;
for(i=0;i<size-1;i++)
for(j=0;j<size-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
boolean flag=false;
int L=0,U=size-1,M=(L+U)/2;
while(M>0)
if(a[M]==n)
8
flag=true;
break;
else if(a[M]<n)
L=M+1;
else
U=M-1;
M=(L+U)/2;
if(flag==false)
System.out.println("Unsuccesful Search");
else
import java.io.*;
class BinaryOut
int s=Integer.parseInt(br.readLine());
9
obj.fillArray();
obj.sort();
obj.dispArray();
int n=Integer.parseInt(br.readLine());
obj.search(n);
OUTPUT
23
12
10
42
35
5 10 12 23 35 42
35
LinearSearch
Write a program to search for an element an array using Linear Searching technique
import java.io.*;
size=x;
a=new int[size];
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(br.readLine());
int i;
for(i=0;i<size;i++)
11
System.out.print(a[i]+"\t");
System.out.println();
boolean flag=false;
int i;
for(i=0;i<size;i++)
if(a[i]==n)
flag=true;
break;
if(flag==false)
System.out.println("Unsuccesful Search");
else
import java.io.*;
class LinearOut
{
12
int s=Integer.parseInt(br.readLine());
obj.fillArray();
System.out.println("Given Array:");
obj.dispArray();
int n=Integer.parseInt(br.readLine());
obj.search(n);
OUTPUT
12
42
32
15
62
Given Array:
12 42 32 15 62
62