Professional Documents
Culture Documents
Arrays
Example:
int n, sum=0;
for(int i=1; i<=10; i++)
{
n=sc.nextInt();
sum=sum+n;
}
2
Dimensional Array
0 1 2 3 4 5 6 7 8 9
n[0] = 10
n[1] = 15
n[2] = 20
.
.
n[9] = 55 5
By using Assignment Statement
int n[] = {10, 15, 20, 25, 30, 35, 40, 45, 50, 55};
n 10 15 20 25 30 35 40 45 50 55
0 1 2 3 4 5 6 7 8 9
n[0] = 10
n[1] = 15 char x[] ={‘a’, ‘e’, ‘i‘, ‘o’, ‘u’};
String s[] = {“Pune”, “Delhi”, “Mumbai”, “Agra”, “Jaipur”};
n[2] = 20 double a[] = { 2.5, 3.5, 4.5, 5.5, 6.5};
.
n[9] = 55 6
Example
1. If int x[] = {4, 3, 7, 8, 9, 10}; what are the values of p and q?
(i) p=x.length (ii) q=x[2]+x[5]*x[1]
x 4 3 7 8 9 10
0 1 2 3 4 5
for(int i=0;i<11;i++)
{
if(runs[i]>max)
System.out.println(“Highest runs made by
{ max=runs[i];
”+ name[pos] + “ Runs=”+max);
pos=i;
26
}}
Searching
It is a process to determine whether a given item (a number or a
word) is present in the array or not.
There are two techniques:
1. Linear Search
2. Binary Search
Linear Search
It is one of the simplest techniques in which the searching of an
item begins from the start of an array (i.e. from 0th index of the
array). The process continues checking the existence of the given
data in the list of array elements one by one, until end of the array
is reached. 27
Program 8
Write a program to input 10 numbers and store in a single
dimensional array. Check whether a user entered number is
present in an array or not. Display a message accordingly.
import java.util.*;
public class Search
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n[] = new int[10];
28
System.out.println(“Enter 10 numbers”);
for(int i=0; i<10; i++) n
{ 12 34 2 7 9 15 25 30 20 16
n[i] = sc.nextInt(); 0 1 2 3 4 5 6 7 8 9
}
System.out.println(“Enter a number for searching”);
int s= sc.nextInt();
int flag=0; if(flag==1)
for(int i=0; i<10; i++) System.out.println(s+ “ is present in the
{ list”);
if(n[i]==s) else
{ flag=1; break; } System.out.println(s+ “ is not present in the
} list”); }} 29
Program 9
Write a program to accept the year of graduation from school as
an integer value from the user. Using the linear search technique
on the array of integers given below, output the message
“Record exists” if the value input is located in the array. If not
output the message “Record does not exist”.
1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010
import java.util.*;
public class Search
{ public static void main(String args[])
{
Scanner sc = new Scanner(System.in); 30
1982 1987 1993 1996 1999 2003 2006 2007 2009 2010
0 1 2 3 4 5 6 7 8 9
int year[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007,
2009, 2010};
System.out.println(“Enter the year of graduation from school”);
int s= sc.nextInt();
int l=year.length, flag=0; if(flag==1)
for(int i=0; i<l; i++) System.out.println(“Record exists”);
{ else
if(year[i]==s) System.out.println(“Record does not
{ flag=1; break; } exists”);
} }} 31
Program 10
Write a program to input 10 students names and their telephone
numbers and store in two different single dimensional arrays.
Enter student name for searching. If found display student name
along with his telephone number otherwise display search name
does not exist in the list.
import java.util.*;
public class Search
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
String name[] = new String[10];
long tel[] = new long[10]; 32
System.out.println(“Enter 10 student names and their telephone
number”); A B C D E F G H I J
for(int i=0;i<10; i++) 0 1 2 3 4 5 6 7 8 9
{ name[i] =sc.next();
11 22 33 44 55 66 77 88 99 100
tel[i] = sc.nextLong();
} 0 1 2 3 4 5 6 7 8 9
System.out.println(“Enter student name for searching”);
String s= sc.next(); if(flag==1)
int flag=0, pos=0; System.out.println(s+ “ is present in
for(int i=0;i<10;i++) the list and telephone number =”+
{ tel[pos]);
if(name[i].equalsIgnoreCase(s)) else
{ System.out.println(s+ “ is not present
flag=1; pos=i; break; in the list”); }} 33
}}
Program 11 (ICSE 2016)
Write a program to initialize the seven Wonders of the World along
with their locations in two different arrays. Search for a name of the
country input by the user. If found, display the name of the country
along with its Wonder, otherwise display “Sorry Not Found!”
Seven wonders – CHICHEN ITZA, CHRIST THE REDEEMER,
TAJMAHAL, GREAT WALL OF CHINA, MACHU PICCHU,
PETRA, COLOSSEUM
Locations – MEXICO, BRAZIL, INDIA, CHINA, PERU,
JORDAN, ITALY
Example – Country Name: INDIA Output: INDIA – TAJMAHAL
Country Name: USA Output: Sorry Not Found!
34
import java.util.*;
public class Search
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String W[] = {“CHICHEN ITZA”, “CHRIST THE
REDEEMER”, “TAJMAHAL”, “GREAT WALL OF CHINA”,
“MACHU PICCHU”, “PETRA”, “COLOSSEUM”};
String L[] = {“MEXICO”, “BRAZIL”, “INDIA”, “CHINA”,
“PERU”, “JORDAN”, “ITALY”};
35
System.out.println(“Enter name of the country for searching”);
String s= sc.next();
int flag=0, pos=0;
for(int i=0; i<7; i++)
{
if(L[i].equalsIgnoreCase(s))
if(flag==1)
{
System.out.println(“Country= ”+s+ “
flag=1;
Wonder =”+ W[pos]);
pos=i; else
break; System.out.println(“Sorry not found”);
} }
} }
36
Program 12
Write a program to input 10 city names and their STD codes
store in two different single dimensional array. If user enter city
name display corresponding STD Code and vice versa according
to user’s choice.
import java.util.*;
public class Search
{ public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String city[] = new String[10];
int std[] = new int[10]; 37
System.out.println(“Enter 10 city String s= sc.next();
names and their STD codes”); for(int i=0;i<10;i++)
for(int i=0;i<10; i++) {
{ if(city[i].equalsIgnoreCase(s))
city[i] =sc.next(); {
std[i] = sc.nextInt(); flag=1; pos=i; break;
} }
System.out.println(“Enter 1 to know }
the STD code or 2 to know the city if(flag==1)
name”); System.out.println(s+ “ is present in
int x=sc.nextInt(); the list and STD code =”+ std[pos]);
int flag=0, pos=0; else
switch(x) System.out.println(s+ “ is not present
{case 1: System.out.println(“Enter city in the list”);
name for searching”); break; 38
case 2: System.out.println(“Enter
STD code for searching”); System.out.println(s1+ “ is not present
int s1= sc.nextInt(); in the list”);
for(int i=0;i<10;i++) break;
{ default: System.out.println(“Wrong
if(std[i]==s1) input of choice”);
{ }
flag=1; pos=i; break; }
} }
}
if(flag==1)
System.out.println(s1+ “ is present in
the list and city name =”+ city[pos]);
else
39
Sorting
It is a system of arranging array elements in a specific order i.e.
ascending or descending.
Sorting is done in two ways:
1. Bubble Sort
2. Selection Sort
Bubble Sort
In this technique, the array is sequentially scanned several times
and during each iteration the pair of consecutive elements are
compared and interchanged (if necessary) to bring them into the
specific order (ascending or descending order)
40
12 5 7 15 10
0 1 2 3 4
5 12 7 15 10
0 1 2 3 4
5 7 12 15 10
0 1 2 3 4
5 7 12 10 15
0 1 2 3 4
5 7 12 15 10
0 1 2 3 4 41
Program 13
Write a program to input 10 numbers and store in a single
dimensional array. Sort the numbers in an ascending order using
Bubble sort technique.
import java.util.*;
public class Sort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n[] = new int[10];
42
System.out.println(“Enter 10 numbers”);
for(int i=0; i<10; i++)
n[i] = sc.nextInt(); 80 30 20 60 70 100 40 90 50 10
int temp; 0 1 2 3 4 5 6 7 8 9
for(int i=0; i<10; i++)
{ for(int j=0; j<(9-i); j++)
{
if(n[j]>n[j+1]) System.out.println(“Numbers in
{ ascending order are”);
temp=n[j]; for(int i=0;i<10; i++)
n[j]=n[j+1]; System.out.println(n[i]);
n[j+1]=temp; }
} }} } 43
Program 14
Write a program to input 20 names and store in a single
dimensional array. Sort the names in an alphabetical order using
Bubble sort technique.
import java.util.*;
public class Sort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String n[] = new String[20];
44
System.out.println(“Enter 20 names”);
for(int i=0; i<20; i++)
n[i] = sc.next(); Ff Jj Ee Aa Dd Bb Cc Gg Ii hh
String temp; 0 1 2 3 4 5 6 7 8 9
for(int i=0; i<20; i++)
{ for(int j=0; j<(19-i); j++)
{
if(n[j].compareToIgnoreCase(n[j+1])>0)
{ System.out.println(“Names in
temp=n[j]; alphabetical order are”);
n[j]=n[j+1]; for(int i=0;i<20; i++)
n[j+1]=temp; System.out.println(n[i]);
} }} }} 45
Program 15
Write a program to input 15 Employee names and their salary,
store in two different single dimensional arrays. Sort the
employee names in descending order i.e. Z to A using Bubble
sort technique. Display the sorted employee names along with
their salary.
import java.util.*;
public class Sort
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
String n[] = new String[15];
int sal[] = new int[15]; 46
System.out.println(“Enter 15 employee names and their salary”);
for(int i=0; i<15; i++)
Ff Jj Ee Aa Dd Bb Cc Gg Ii hh
{
n[i] = sc.nextLine(); 0 1 2 3 4 5 6 7 8 9
48