0% found this document useful (0 votes)
7 views31 pages

Arrays - Revised

The document provides an overview of arrays in programming, including their definition, types, and various operations such as declaration, initialization, and searching techniques. It includes examples of Java code for reading and manipulating arrays, as well as explanations of linear and binary search methods. Additionally, it discusses the differences between linear and binary search algorithms.

Uploaded by

nairpriya395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views31 pages

Arrays - Revised

The document provides an overview of arrays in programming, including their definition, types, and various operations such as declaration, initialization, and searching techniques. It includes examples of Java code for reading and manipulating arrays, as well as explanations of linear and binary search methods. Additionally, it discusses the differences between linear and binary search algorithms.

Uploaded by

nairpriya395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Arrays

Array : finite set of homogenous


Name
a data elements stored in contiguous
int a =10; 1010 address memory location.
10 value Types of array
1. Single dimensional
2. Double dimensional
Declaring array
a[0]+a[3]/a[4]
int a []; int []a ; 30+57/34
30+1= 31
Initializing array 30 40 100 57 34
int a[]=
int a[]= new a[5];
0 1 2 3 4
Initilization of array: (Static Initization)

String s[]={“abc”,”pqrst”,”program”,”code”}; static initialization


0 1 2 3
int a[]={1,2,3,4,5}
char c[]={‘a’,’b’,’c’}; = size( 2*3=6 byte)
double d[]={4.0,6.8,6.9,3.7}; size=( 8*4=32 byte)
1. s.length=4
2. s[2].length()=7 Uses of Array:
3. Sopln(c[2])= c 1. Counting the items
4. d[1]+2.4 +d[3]+4.4= 6.8+2.4+3.7+4.4=17.4 2. Searching elements
a[2]=3 3. Sorting elements.
Total size of a= 4*5=20 byte 4. Program become shorter
length- finds length of array
length()- finds length of string
Q1. String x[] = {"Artificial intelligence", "IOT", "Machine Learning", "Big data"}
Give the output of the following statements:
(i) System.out.println(x[3]); Big data
(ii) System.out.println(x.length); 4

Q2. String x[] = {“SAMSUNG”, “NOKIA”, “SONY” , “MICROMAX”,


“BLACKBERRY”};
Give the output of the following statements:
i) System.out.println(x[1]);=NOKIA
ii) System.out.println(x[3].length()); 8
Q3. If int x [ ] = { 4, 3, 7, 8, 9, 10 }; what are the values of p and q?
(i) p = x.length =6
(ii) q = x[2] + x[5] * x[1]
7+10*3
7+30=37
WAP to read 5 numbers print the elements using array.
import java.util.*;
class Arr 45 60 100 300 400
{ psvm()
{ Scanner sc=new Scanner (System.in); i 0 1 2 3 4
int n[]=new int[5];
Sopln(“ Enter 5 elements”);
for( int i=0;i<5;i++)
{
n[i]=sc.nextInt();
}

Sopln( “The elements are”);


for( int i=0 ; i<5;i++)
Sopln(n[i]);
}}
WAP to read 5 numbers and find its sum and average of the elements using
array.
import java.util.*; 45 60 100 300 400
class Arr
{ psvm() i 0 1 2 3 4
{ Scanner sc=new Scanner (System.in);
int n[]=new int[5];
Sopln(“ Enter 5 elements”);
avg=sum/5.0;
for( int i=0;i<5;i++)
Sopln(“The sum is “+sum);
{
Sopln(“Avg is”+avg);
n[i]=sc.nextInt();
}}
}
int sum=0; double avg=0.0d;
for( int i=0;i<5;i++)
{ sum=sum+n[i]; }
WAP to read 10 numbers in an array and find the sum of all even
elements find the avg of even numbers
import java.util.*; Sopln(“Average is”+((double) (sum/ctr))
class Arra Sopln(“The sum is “+sum);
{ psvm() }///main
{ Scanner sc = new Scanner (System.in); }//class

int n[]=new int[10];


Sopln(“Enter 10 numbers”);
for( int i=0;i<10;i++)
{ n[i]=sc.nextInt();
}
int sum=0; int ctr=0;
for( int i=0;i<10;i++)
{ if ( n[i]%2==0)
{
sum=sum+n[i];
ctr++;
}}
WAP to read length of the array from the user and print sum of
even and odd numbers separately .
import java.util.*;
class Arra for( int i=0;i<len;i++)
{ psvm() { if( n[i]%2==0)
{ Scanner sc = new Scanner (System.in); sume+=n[i]; or sume=sume+n[i];
Sopln(“Enter the length of the array”); else
int len=sc.nextInt(); sumo+=n[i]; or sumo=sumo+n[i];
int n[]=new int[len]; }
Sopln(“Enter the element”+len); Sopln(“Sum of odd is “+sumo);
for( int i=0;i<len;i++) Sopln(“Sum of even is”+sume);
{ n[i]=sc.nextInt(); }
} }
int sumo=0; int sume=0;
WAP to read the length of the array from the user and read salary of employee
and count how many are earning more than 50000 . import java.util.*;
class Arr3
{ psvm()
for( int i=0;i<len;i++) { Scanner sc=new Scanner (System.in);
{ if(n[i]>=50000) Scanner sc = new Scanner (System.in);
ctr++; Sopln(“Enter the length of the array”);
} sopln( “ no of people earning more than int len=sc.nextInt();
50000 are”+ ctr);}} int n[]=new int[len];
Sopln(“Enter the element”+len);
for( int i=0;i<len;i++)
{ n[i]=sc.nextInt();
}
for( int i=0;i<len;i++)
{
n[i]=sc.nextInt();
} int ctr=0;
WAP to read 5 numbers and find the max and min
number.
WAP to read 5 numbers and find the int max=n[0];
max and min number. int min=n[0];
for( int i=0;i<5;i++)
class Arr { if( n[i] > max)
{ psvm() max=n[i];
{ Scanner sc=new Scanner (System.in); if( n[i]<min)
min=n[i];
int n[]=new int[5]; }
Sopln(“ Enter 5 elements”); Sopln(“ Min”+ min);
for( int i=0;i<5;i++) Sopln(“ Max”+max);}}
{ 20 40 10 100 5
0 1 2 3 4
n[i]=sc.nextInt(); I i<5 n[i]>max max n[i]<min min
0 0<5 20>20 20 20<20 20
} 1 1<5 40>20 40 40<20
2 2<5 10>40 10<20 10
3 3<5 100>40 100 100<10
4 4<5 5>100 5<10 5
5 5<5f
Define a class to declare an array of size 20 of double datatype, accept the elements into the array and
perform the following: • Calculate and print the sum of all the elements. • Calculate and print the highest value
of the array
import java.util.*;
class greatest
{ public static void main()
{ Scanner sc=new Scanner(System.in);
double n[]=new double[20];
double sum=0.0;
System.out.println("Enter 20 numbers");
for(int i=0; i<20; i++)
{ n[i]=sc.nextDouble();
sum=sum+n[i]; }
double max=n[0];
for(int i=0;i<20;i++)
{ if(n[i]>max) max=n[i]; }
System.out.println("Sum is”+sum);
System.out.println(“Max is”+ max);
}}
Searching Techniques
1. Linear Search
2. Binary Search
WAP to read 10 pan numbers and read a pan number to be searched. If found print
“Successful ” and print the position of the element and if not “ Unsuccessful” using linear
search .
import java.util.*;
class Linear int flag=0;
{ psvm() for(i=0;i<10;i++)
{ Scanner sc=new Scanner(System.in); { if(pan[i]==search)
int pan[]=new int[10]; { flag=1;
int i=0; break;
Sopln(“Enter 10 pan numbers”); }
for( i=0;i<10;i++) }
{ pan[i]=sc.nextInt(); if( flag==1)
} Sopln(“Successful and found at “+(i+1));
Sopln(“Enter the pan to be searched”); else
int search=sc.nextInt(); Sopln(“Unsuccessful”);
}}
WAP to read 5 country names read a country name to be searched. If
found print “Found “ and if not “ Not Found” using linear search .
import java.util.*;
class Linear int flag=0;
{ psvm() for(i=0;i<5;i++)
{ Scanner sc=new Scanner(System.in); { if(c[i].equalsIgnoreCase(search))
String c[]=new String [5]; { flag=1;
int i=0; break;
Sopln(“Enter 5 country names”); }
for( i=0;i<5 ;i++) }
{ c[i]=sc.next(); if( flag==1)
} Sopln(“Found”);
Sopln(“Enter the country to be searched”); else
String search=sc.next(); Sopln(“Not Found”);
}}
WAP to read 5 country names read a country name to be searched. If
found print “Found “ and if not “ Not Found” using linear search .
for ( i=0;i<5;i++)
import java.util.*; { if( n[i].equalsIgnoreCase(search))
class Lsearch { flag=1
{ psvm() break; }
{ Scanner sc=new Scanner(System.in); }
String n[]=new String[5]; if(flag==1)
int i=0; Sopln(“Found”);
for( i=0;i<5;i++) else
{ n[i]=sc.next(); } Sopln(“Not Found”);
Sopln(“Enter name to be searched”); }}
String search=sc.next();
int flag=0;
WAP to read 5 city name and the std code in 2 different arrays .
Read a city name to be searched and print the std code if found else if not “ Invalid city
name ”. using linear search .
import java.util.*; int flag=0;
class Linear for( i=0;i<5;i++)
{ psvm() { if( c[i].equalsIgnoreCase(search))
{ Scanner sc=new Scanner(System.in); { flag=1;
int std[]=new int[5]; break;
String c[]=new String[5]; }
int i=0; }
Sopln(“Enter 5 city name and std code”); if( flag==1)
for( i=0;i<5;i++) Sopln(c[i]+ “ “ +std[i]);
{ c[i]=sc.next(); else
std[i]=sc.nextInt(); Sopln(“ Invalid city name”);
} }
Sopln(“Enter the city to be searched”); }
String search=sc.next();
Binary Search
10 20 30 40 50 len 5 mid =(lb+ub)/2 lb=0 ub=len-1
lb ub

for(int i=0;i<5;i++)
n[i]=sc.nextInt(); if (flag==1)
lb=0 Sopln(“Found ar”+(mid+1);
Ub=4 else
mid=-1; Sopln(“Not Found”);
search=sc.nextInt();
while(lb<=ub)
{ mid=(lb+ub)/2;
if( search >n[mid])
lb=mid+1;
if( search <n[mid])
ub=mid-1;
if(n[mid]==search)
{ flag=1; break;}}
Write a program to declare an array with the year of graduation from school as an integer value. Read a year to be
searched. Output the message “Record exists” if the value input is located in the array. If not, output the message
“Record does not exist”. Using Binary Search
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

import java.util.*;
class Binary
{ psvm()
{ Scanner sc=new Scanner(System.in);
int n[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010 };
int lb=0 ; int ub=9; int mid=-1; int flag=0;
Sopln(“Enter number to be searched”);
int search=sc.nextInt();
while(lb<=ub)
{ mid=(lb+ub)/2; if (flag==1)
if( search >n[mid]) Sopln(“Record exists”);
lb=mid+1; else
Sopln(“Record does not exists”);
if( search <n[mid])
}}
ub=mid-1;
if(n[mid]==search)
{ flag=1; break;}}
Read 2 array of length 5 and merge them into one array.
Read 2 array of length 5 and merge them into one array.
import java.util.*; for( int i=0 ;i<5;i++)
class Merge {
{ psvm() n2[i]=n[i];
{ Scanner sc=new Scanner(System.in); }
int n[]=new int[5];
int n1[]=new int[5]; for( int i=0 ;i<5;i++)
int n2[]=new int[10]; { n2[i+5]=n1[i];
}
Sopln(“Enter 5 nos in first array”); OR
for( int i=0;i<5;i++) for( int i=0 , j=5; i<5; i++, j++)
{ n[i]=sc.nextInt(); { n2[j]=n1[i];
}
}
Sopln(“Enter 5 nos in sec array”); Sopln(“Merged array is”)
for( int i=0;i<5;i++) for( int i=0 ;i<10;i++)
Sopln(n2[i]);
{ n1[i]=sc.nextInt(); }}
}
Difference between Linear and Binary Search
Linear Binary
1.Array elements need not be in sorted order 1.Array elements need to be in sorted order
2. Slow as it checks each element of array 2.Fast as it skips half the elements
3.Search begins from 0th position of array 3. Array is divided into 2 halves and desired
data is searched in first half or second half
WAP to read 5 number and sort them in ascending order using bubble sort

import java.util.*;
class array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n[]=new int[5];
Sopln(“Enter 5 nos”);
for( int i=0;i<5;i++)
{ n[i]=sc.nextInt();
}
for( int i=0;i<4;i++)
{ for( int j=0;j<4-i;j++)
{ if( n[j]> n[j+1]) Sopln(“The sorted array is”);
{ int temp=n[j]; for( int i=0;i<5;i++)
Sopln(n[i]);
n[j]=n[j+1]; }
n[j+1]=temp;}}} }
WAP to read 5 names and
sort them in ascending order import java.util.*;
class array
using bubble sort {
public static void main()
{
Scanner sc=new Scanner(System.in);
String n[]=new String [5];
Sopln(“Enter 5 names”);
for( int i=0;i<5;i++)
{ n[i]=sc.next();
}
for( int i=0;i<4;i++)
{ for( int j=0;j<4-i;j++)

Sopln(“The sorted array is”);


{ if( n[j].compareTo( n[j+1]) >0)
{ String temp=n[j];
for( int i=0;i<5;i++) n[j]=n[j+1];
Sopln(n[i]); n[j+1]=temp;
}}}
}
}
WAP to read 10 state name and their capital. Sort state name in
descending order and swap the capital accordingly using bubble
sort import java.util.*;
class array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String S[]=new String [10];
Sting C[]=new String [10]
Sopln(“Enter 10 states and their capital”);
for( int i=0;i<10;i++)
{ S[i]=sc.next();
C[i]=sc.next();
}

continued
for( int i=0;i<9;i++)
{ for( int j=0;j<9-i;j++)
{ if( S[j].compareTo( S[j+1]) <0)
{ String temp=S[j];
S[j]=S[j+1];
S[j+1]=temp;
String tem= C[j];
C[j]=C[j+1];
C[j+1]=tem;
}}}

Sopln(“The sorted array is”);


for( int i=0;i<10;i++)
Sopln(S[i]+ “ “ + C[i]);
}
}
Bubble Sort
Nos in ascending order n[j]> n[j+1]
Nos in descending order n[j]<n[j+1]
String is ascending n[j].compareTo(n[j+1])>0
String is descending n[j].compareTo(n[j+1])<0

Selection Sort
Nos in ascending order n[j]< n[min]
Nos in descending order n[j] >n[mini]
String is ascending n[j].compareTo(n[min])<0
String is descending n[j].compareTo(n[min])>0
WAP to read 5 names and sort them in ascending order using Selection sort
import java.util.*;
class array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String n[]=new String [5];
Sopln(“Enter 5 names”);
for( int i=0;i<5;i++)
{ n[i]=sc.next();
}
for( int i=0;i<4;i++) Sopln(“The sorted
{ int min=i;
{ for( int j=i+1;j<5j++) array is”);
for( int i=0;i<5;i++)
{ if( n[j].compareTo( n[min]) <0) Sopln(n[i]);
{ min=j; }
}
String temp=n[min]; }
n[min]=n[i];
n[i]=temp
}
read 2D array 3*3 in java and print the elements in matrix form
import java.util.Scanner;

class Matrix {
public static void main() {
int[][] matrix = new int[3][3]; Scanner sc = new Scanner(System.in)
System.out.println("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt(); } }
System.out.println("The 3x3 matrix is:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
} System.out.println();
} }}
read 2D array 3*3 in java and add the elements of each row and column separately
import java.util.Scanner;

class MatrixSumExample {
public static void main()
int[][] matrix = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt(); } }

// Calculate and print the sum of each row


for (int i = 0; i < 3; i++) {
int rowSum = 0;
for (int j = 0; j < 3; j++) {
rowSum += matrix[i][j];
}
System.out.println("Sum of elements in Row " + (i + 1) + ": " + rowSum);
} for (int j = 0; j < 3; j++) {
int colSum = 0;
for (int i = 0; i < 3; i++) {
colSum += matrix[i][j];
}
System.out.println("Sum of elements in Column " + (j + 1) + ": " + colSum);
}
}
r ead 2D array 3*3 in java and add the elements of left and right diagonal
class MatrixSumExample {
public static void main()
int[][] matrix = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt(); } }

int suml=0; int sumr=0;


for( int i=0;i<3;i++)
{ suml += matrix[i][i]; }
System.out.println(" left diagonal”+suml);
int j=2;
for( int i=0;i<3;i++)
{ sumr+= matrix[i][j]; j–; }
System.out.println(" right diagonal”+sumr); }}}
read 2D matrix 3*3 in java and find the sum of all odd and even numbers separately
import java.util.Scanner;

class OddEvenSumExample {
public static void main() {
int[][] matrix = new int[3][3];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt(); } }
int oddSum = 0; int evenSum = 0;

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (matrix[i][j] % 2 == 0) {
evenSum += matrix[i][j];
else
oddSum += matrix[i][j];
} }
System.out.println("Sum of even numbers: " + evenSum);
System.out.println("Sum of odd numbers: " + oddSum); } }

You might also like