You are on page 1of 48

Chapter 3

Arrays

By: SARIKA KULKARNI


PPS Hadapsar
Need of using an Array

Example:
int n, sum=0;
for(int i=1; i<=10; i++)
{
n=sc.nextInt();
sum=sum+n;
}
2
Dimensional Array

A dimensional array is a structure created in the memory to


represent a number of values of the same data type with the
variables having the same variable name along with different
subscript.
A dimensional array is basically of two types:
1. Single Dimensional Array.
2. Double Dimensional Array.
3
Single Dimensional Array
Single dimensional array is also called as single subscripted
variable because each element of the array is represented by
using a common variable name along with a single subscript.
Creating a Single Dimensional Array:[By declaring]
Syntax: <data type><variable>[] = new <data type>[size];
Example: int n[] = new int[10];
n
0 1 2 3 4 5 6 7 8 9
4
Single Dimensional Array
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
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

2. Give the output of the following statements:


String x[] = {“SAMSUNG”, “NOKIA”, “SONY”,
“MICROMAX”, “BLACKBERRY”};
(i) System.out.println(x[1]); (ii) System.out.println(x[3].length());
7
Program 1
Write a program to input 10 subject marks and store in a single
dimensional array. Calculate and display the total and average of
10 subject marks.
import java.util.*;
public class TotalAvg
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
8
int m[] = new int[10];
int sum=0;
System.out.println(“Enter 10 subject marks”);
for(int i=0; i<10; i++) m
{ 70 75 80 65 50 95 85 75 60 55
m[i] = sc.nextInt(); 0 1 2 3 4 5 6 7 8 9
sum = sum+m[i];
}
double avg = sum/10.0;
System.out.println(“Total of 10 subject marks=”+sum);
System.out.println(“Average of 10 subject marks=”+avg);
9
}}
Program 2
Write a program to input 10 numbers and store in a single
dimensional array. Find the largest and smallest number in an
array.
import java.util.*;
public class LargeSmall
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
10
float n[] = new float[10];
System.out.println(“Enter 10 numbers”);
for(int i=0; i<10; i++) n
{ 7.0 7.5 8.0 6.5 5.0 9.5 8.5 7.5 6.0 5.5
n[i] = sc.nextFloat(); 0 1 2 3 4 5 6 7 8 9
}
float max=n[0], min=n[0];
for(int i=0; i<10; i++)
{ if(n[i]>max)
max=n[i]; System.out.println(“Largest no=”+max);
else if(n[i]<min) System.out.println(“Smallest no=”+min);
min=n[i];
}}
} 11
Program 3 (ICSE 2010)
Write a program to store 6 elements in an array P and 4 elements
in an array Q. Now, Create a third array R, containing all the
elements of array P and Q. Display the resultant array.
import java.util.*;
public class ResultantArray
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
12
int P[] = new int[6]; 10 15 20 25 30 35 40 45 50 55
int Q[] = new int[4];
int R[] = new int[10]; 0 1 2 3 4 5 6 7 8 9
System.out.println(“Enter 6 elements for array P ”);
for(int i=0; i<6; i++)
System.out.println(“Elements of resultant
{
array R are”);
P[i] = sc.nextInt();
for(int i=0; i<10; i++)
R[i] = P[i];
System.out.println(R[i]);
}
for(int i=0; i<4; i++) }}
{ Q[i] = sc.nextInt();
R[i+6] = Q[i];
} 13
Program 4 (ICSE 2018)
Write a program to accept name and total marks of N number of
students in two single subscript arrays name[] and totalmarks[].
Calculate and print:
(i) The average of the total marks obtained by N number of
students. [average = (sum of total marks of all the students)/N]
(ii) Deviation of each student’s total marks with the average.
[deviation= total marks of a student – average]
import java.util.*;
public class Deviation
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in); 14
System.out.println(“Enter number of students”);
int n=sc.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int sum=0;
for(int i=0; i<n; i++)
{
System.out.println(“Enter name and total marks of a student”);
name[i] = sc.next();
totalmarks[i] = sc.nextInt();
sum = sum+totalmarks[i];
} 15
double avg = (double)sum/n;
System.out.println(“Average of total marks of N number of
students=”+ avg);
System.out.println(“Deviation of each student’s total marks with
the average are:”);
for(int i=0; i<n; i++)
{
System.out.println(totalmarks[i] -avg);
}
}
} 16
Program 5 (ICSE 2015)
Write a program to input and store roll numbers, names and marks
in 3 subjects of n number of students in five single dimensional
arrays and display the remark based on average marks as given
below:
Average Marks Remarks
85 – 100 Excellent
75 – 84 Distinction
60 – 74 First Class
40 – 59 Pass
Less than 40 Poor
17
import java.util.*;
public class Remark
{ public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter number of students”);
int n= sc.nextInt();
int rno[] = new int[n];
String name[] = new String[n];
int m1[] = new int[n];
int m2[] = new int[n];
int m3[] = new int[n];
18
double avg =0;
for(int i=0; i<n; i++)
{
System.out.println(“Enter ”+(i+1)+ “ student roll number,
name and marks in 3 subjects”);
rno[i]= sc.nextInt();
name[i] = sc.next();
m1[i]= sc.nextInt();
m2[i]= sc.nextInt();
m3[i]= sc.nextInt();
avg=(double)(m1[i]+m2[i]+m3[i])/3;
19
if(avg>=85 && avg<=100)
System.out.println(“Excellent”);
else if(avg>=75 && avg<85)
System.out.println(“Distinction”);
else if(avg>=60 && avg<75)
System.out.println(“First Class”);
else if(avg>=40 && avg<60)
System.out.println(“Pass”);
else if(avg>=0 && avg<40)
System.out.println(“Poor”);
else
System.out.println(“Invalid average”);
20
} }}
Program 6 (ICSE 2009)
The Annual Examination result of 50 students in a class is
tabulated in a single dimensional array is as follows:
Roll No. Subject A Subject B Subject C
---- ---- ---- ----
Write a program to read the data, calculate and display the
following:
(a) Average marks obtained by each student.
(b) Print the roll number and average marks of the students whose
average is above 80
(c) Print the roll number and the average marks of the students
whose average is below 80 21
import java.util.*;
public class Exam
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int rno[] = new int[50];
int s1[] = new int[50];
int s2[] = new int[50];
int s3[] = new int[50];
double avg[] = new double[50];
22
for(int i=0;i<50;i++)
{
System.out.println(“Enter”+(i+1)+ “Student roll number and 3
subject marks”);
rno[i] = sc.nextInt();
s1[i] = sc.nextInt();
s2[i] = sc.nextInt();
s3[i] = sc.nextInt();
avg[i] = (s1[i]+s2[i]+s3[i])/3.0;
}
System.out.println(“Average marks obtained by each student”);
for(int i=0;i<50;i++)
System.out.println(avg[i]); 23
System.out.println(“Students whose average above 80 are”);
System.out.println(“Roll no. \t Average Marks”);
for(int i=0;i<50;i++)
{ if(avg[i]>80)
System.out.println(rno[i]+ “\t” +avg[i]);
}
System.out.println(“Students whose average below 80 are”);
System.out.println(“Roll no. \t Average Marks”);
for(int i=0;i<50;i++)
{ if(avg[i]<80)
System.out.println(rno[i]+ “\t” +avg[i]);
}
}} 24
Program 7
Write a program to input 11 cricketer names and their runs, store
in two single dimensional arrays. Display highest runs made by
the cricketer.
import java.util.*;
public class Cricketer
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String name[] = new String[11];
int runs[] = new int[11]; 25
System.out.println(“Enter 11 cricketers names and their runs”);
for(int i=0;i<11; i++)
A B C D E F G H I J K
{
name[i] =sc.next(); 0 1 2 3 4 5 6 7 8 9 10
runs[i] = sc.nextInt();
70 35 50 45 10 25 85 15 60 55 20
}
int max=0, pos=0; 0 1 2 3 4 5 6 7 8 9 10

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

sal[i] = sc.nextInt(); 5L 2L 6L 4L 7L 10L 15L 1L 9L 8L


0 1 2 3 4 5 6 7 8 9
}
String temp; int temp1; temp=n[j];
for(int i=0; i<15; i++) n[j]=n[j+1];
{ n[j+1]=temp;
for(int j=0; j<(14-i); j++) temp1=sal[j];
{ sal[j]=sal[j+1];
if(n[j].compareToIgnoreCase(n[j+1])<0) sal[j+1]=temp1
{ } }} 47
System.out.println(“Employee Names in alphabetical
order are”);
System.out.println(“Employee name \t Salary”);
for(int i=0; i<15; i++)
System.out.println(n[i]+ “\t” + sal[i]);
}
}

48

You might also like