Array

Section B

2005

Question 7
Write a program to bubble sort the following set of values in ascending order:- 5,3,8,4,9,2,1,12,98,16
Output:
1
2
3
4
5
8
9
12
16
98 [15]

public class employee
{
public void compute()
{
int a[]={5,3,8,4,9,2,1,12,98,16};
int i,j,temp;
for(i=0;i<10-1;i++)
{
for(j=0;j<10-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Values in ascending order");
for(i=0;i<10;i++)
System.out.println(a[i]);
}
}

Variable Description

VariableTypeDescription
aintArray to store values
iintOuter loop variable
jintInner loop variable
tempintTo exchange for sorting

Question 9
Write a program to initialize an array of 5 names and initialize another array with their respective telephone numbers. Search for a name input by the User, in the list. If found, display "Search Successful" and print the name along with the telephone number, otherwise display "Search unsuccessful. Name not enlisted". [15]

import java.util.*;
public class Search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String name[]={"Ashok","Benji","Cinda","David","Elsa"};
long number[]={2532556,251478,3257842,2513278,243786};
System.out.print("Enter subscriber name: ");
String sname=sc.next();
boolean flag=false;
for(int i=0;i<5;i++)
{
if(name[i].equals(sname))
{
flag=true;
break;
}
}
if(flag==true)
System.out.println("Search Successful. "+name[i]+" : "+number[i]);
else
System.out.println("Search unsuccessful. Name not enlisted.");
}
}
Variable Description
VariableTypeDescription
nameStringArray to store names
numberintArray to store numbers
snameStringTo store search name
iintLoop variable to generate array subscripts
flagbooleanTo indicate whether name is found or not

2006

Question 8
The marks obtained by 50 students in a subject are tabulated as follows:-
NameMarks
********
********
Write a program to input the names and marks of the students in the subject. Calculate and display:-
(i) The subject average marks (subject average marks=subject total / 50)
(ii) Highest mark in the subject and name of the student. (The maximum mark in a subject is 100) [15]

import java.util.*;
public class Marks
{
public static void main()
{
Scanner sc=new Scanner(System.in);
//Array creations
String name[]=new String[50];
double mark[]=new double[50];
//Input of name and marks of 50 students
for(int i=0;i<50;i++)
{
System.out.println("Enter name and mark ");
name[i]=sc.next();
mark[i]=sc.nextDouble();
}
//Finding total, average
double tot=0.0,avg;
for(int i=0;i<50;i++)
{
tot+=mark[i];
}
avg=tot/50.0;
//Finding maximum of marks
double max=mark[0];
String maxname=name[0];
for(int i=1;i<50;i++)
{
if(mark[i]>max)
{
max=mark[i];
maxname=name[i];
}
}
//Display of results
System.out.println("Average of marks : "+avg);
System.out.println("Highest mark : "+max);
System.out.println("Highest mark scorer : "+maxname);
}
}

Question 9
Write a program to accept 15 integers from the keyboard, assuming that no integer entered is a zero. Perform selection sort on the integers and then print them in ascending order. [15]

import java.util.*;
public class Sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
//Array creations
int num[]=new int[15];
//Input of numbers
for(int i=0;i<15;i++)
{
System.out.println("Enter integer other than zero ");
num[i]=sc.nextInt();
}
//Sorting process
int small,index,temp;
for(int i=0;i<14;i++)
{
small=num[i];
index=i;
for(int j=i+1;j<15;j++)
{
if(num[j]< small)
{
small=num[j];
index=j;
}
}
temp=num[i];
num[i]=num[index];
num[index]=temp;
}
System.out.println("Array after sorting");
for(int i=0;i<15;i++)
System.out.print(num[i]+" ");
}
}

2007

Question 6
Write a program to initialize the given data in an array and find the minimum and maximum values along with the sum of the given elements.
Numbers: 2 5 4 1 3
Output:
Minimum value : 1
Maximum value: 5
Sum of the elements: 15 [15]

public class Array
{
public static void main()
{
int a[]={2,5,4,1,3};
//To find maximum and minimum
int i,min=a[0],max=a[0];
for(i=1;i<5;i++)
{
if(a[i]>max)
max=a[i];
else if(a[i]< min)
min=a[i];
}
//To find sum of elements
int s=0;
for(i=0;i<5;i++)
{
s+=a[i];
}
//Display of results
System.out.println("Minimum value :"+min);
System.out.println("Maximum value :"+max);
System.out.println("Sum of the elements:"+s);
}
}

2008

Question 6
Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order using the Bubble Sort technique only.
INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai [15]

public class Cities
{
public static void main()
{
String city[]={"Delhi","Bangalore","Agra","Mumbai","Calcutta"};
String temp;
for(int i=0;i<5-1;i++)
{
for(int j=0;j<5-1-i;j++)
{
if(city[j].compareTo(city[j+1])>0)
{
temp=city[j];
city[j]=city[j+1];
city[j+1]=temp;
}
}
}
System.out.println( “Sorted city names”);
for(int i=0;i<5;i++)
System.out.print(city[i]+“,”);
}
}

2009

Question 9
Annual examination results of 50 students in a class are tabulated as follows.
Roll No.Subject ASubject BSubject C
----------------
Write a program to read the data, calculate and display the following:
a) Average mark obtained by each student.
b) Print the roll number and average marks of the students whose average mark is above 80.
c) Print the roll number and average marks of the students whose average mark is below 40. [15]

import java.util.*;
public class Result
{
public static void main()
{
Scanner sc=new Scanner(System.in);
//Array creations
int roll[]=new int[50];
double subA[]=new double[50];
double subB[]=new double[50];
double subC[]=new double[50];
double avg[]=new double[50];
//Input of roll and marks of three subjects
for(int i=0;i<50;i++)
{
System.out.println("Enter roll and three subjects’ marks ");
roll[i]=sc.nextInt();
subA[i]=sc.nextDouble();
subB[i]=sc.nextDouble();
subC[i]=sc.nextDouble();
}
//Finding average of each student
for(int i=0;i<50;i++)
{
avg[i]=(subA[i]+subB[i]+subC[i])/3.0;
}
System.out.println("Average of each student");
for(int i=0;i<50;i++)
{
System.out.println(roll[i]+ “\t”+avg[i]);
}
System.out.println("Roll and average of whose mark above 80");
for(int i=0;i<50;i++)
{
if(avg[i]>80)
System.out.println(roll[i]+"\t"+avg[i]);
}
System.out.println("Roll and average of whose mark below 40");
for(int i=0;i<50;i++)
{
if(avg[i]<40)
System.out.println(roll[i]+"\t"+avg[i]);
}
}
}

2010

Question 4
Write a program to perform binary search on a list of integers given below, to search for an element input by user, if it is found display the element along with its position, otherwise display the message “Search element not found”. 5,7,9,11,15,20,30,45,89,97. [15]

import java.util.*;
public class Search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
//Array initialization
int a[]={5,7,9,11,15,20,30,45,89,97};
//seach number input
System.out.print("Enter the number to be searched: ");
int snumber=sc.nextInt();
int lower=0,upper=9,middle;
boolean flag=false;
//Searching process
while(lower<=upper)
{
middle=(lower+upper)/2; //To find the middle element
if(snumber==a[middle])
{
flag=true;
break;
}
else if(snumber>a[middle])
lower=middle+1;
else
upper=middle-1;
}
if(flag==true)
System.out.println(snumber+" is present at position: "+(middle+1));
else
System.out.println("Search element not found");
}
}

Question 8
Write a program to store 6 elements in an array P and 4 elements in an array Q and produce a third array R, containing all the elements of array P and Q. Display the resultant array. [15]
Example:
Input Output
P[] Q[] R[]
4 19 4
6 23 6
1 7 1
2 8 2
3 3
7 7
19
23
7
8

import java.util.*;
public class Merge
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int P[]=new int[6];
int Q[]=new int[4];
int R[]=new int[10];
int i,k=0;
System.out.println("Input");
System.out.println("P[ ]");
for(i=0;i<6;i++)
P[i]=sc.nextInt();
System.out.println("Q[ ]");
for(i=0;i<4;i++)
Q[i]=sc.nextInt();
//Merging Process
for(i=0;i<10;i++)
{
if(i<6)
R[i]=P[i];
else
R[i]=Q[k++];
}
System.out.println("R[ ]");
for(i=0;i<10;i++)
System.out.println(R[i]);
}
}

2011

Question 5
Write a program to input and store the weight of ten people. Sort and display them in descending order using selection exchange sort technique. [15]

import java.util.*;
public class People
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double weight[] = new double[10];
double big,temp;
int i,j,index;
//Input of weight
for(i=0;i<10;i++)
{
System.out.println( “Enter weight ”);
weight[i]=Double.
}
//Sorting in descending order
for(i=0;i<9;i++)
{
big=weight[i];
index=i;
for(j=i+1;j<10;j++)
{
if(weight[j]>big)
{
big=weight[j];
index=j;
}
}
temp=weight[i];
weight[i]=weight[index];
weight[index]=temp;
}
//Display of weights
System.out.println( “Weights in descending order”);
for(i=0;i<10;i++)
System.out.println(weight[i]);
}
}

2012

Question 9
Write a program to accept the names of 10 cities in a single dimension integer array and their STD codes in another single dimensional integer array. Search for a name of a city input by the user in the list. If found display “Search successful” and print the name of the city along with its STD code, or else display the message “Search Unsucesssful, No such city in the list”. [15]

import java.util.*;
public class City
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String city[]=new String[10];
String STD[]=new String[10];
int i;
for(i=0;i<10;i++)
{
System.out.println("Enter city name and STD code: ");
city[i]=sc.next();
STD[i]= sc.next();
}
System.out.print("Enter the city name to be searched: ");
String scity=sc.next();
boolean flag=false;
for(i=0;i<10;i++)
{
if(city[i].equals(scity))
{
System.out.println("Search successful");
System.out.println(city[i]+" : "+STD[i]);
flag=true;
break;
}
}
if(flag==false)
System.out.println("Search Unsuccessful. No such city in the list.");
}
}

2013

Question 7
Write a program to input 10 integer elements into an array and sort them in descending order using the bubble sort technique. [15]

import java.util.*;
public class Sort
{
public static void main()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[10];
int i;
for(i=0;i<10;i++)
{
System.out.println("Enter number ");
a[i]=sc.nextInt();
}
//Sorting Process
int j,temp;
for(i=0;i<10-1;i++)
{
for(j=0;j<10-1-i;j++)
{
if(a[ j]>a[ j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted Array");
for(i=0;i<10;i++)
System.out.print( a[i]+"\t");
}
}

2014

Question 7
Write a program to accept the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted 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} [15]

import java.util.*;
public class Search
{
public static void main()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int year[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
System.out.println("Enter the year to be searched ");
int syear=sc.nextInt();
//Searching process
int lower=0,upper=9,middle;
boolean flag=false;
while(lower<=upper)
{
middle=(lower+upper)/2; //To find the middle element
if(syear==year[middle])
{
System.out.println(“Record exists”);
flag=true;
break;
}
else if(syear>year[middle])
lower=middle+1;
else
upper=middle-1;
}
if(flag==false)
System.out.println("Record does not exist");
}
}

2015

Question 6
Input and store roll numbers, names and marks in 3 subjects of n number students in five single dimensional array and display the remark based on averge marks as given below: (The maximum marks in the subject are 100)
Average marks = Total marks / 3. [15]
Average MarksRemarks
85 – 100EXCELLENT
75 – 84DISTINCTION
60 – 74FIRST CLASS
40 – 59PASS
Less than 40POOR

import java.util.*;
public class Marks
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of students");
int n=sc.nextInt();
int roll[]=new int[n];
String name[]=new String[n];
double mark1[]=new double[n];
double mark2[]=new double[n];
double mark3[]=new double[n];
double avg[]=new double[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter roll, name and three marks");
roll[i]=sc.nextInt();
name[i]=sc.next();
mark1[i]=sc.nextDouble();
mark2[i]=sc.nextDouble();
mark3[i]=sc.nextDouble();
}
for(int i=0;i<n;i++)
{
avg[i]=(mark1[i]+mark2[i]+mark3[i])/3.0;
}
for(int i=0;i<n;i++)
{
System.out.println("Roll number: "+roll[i]);
System.out.println("Name: "+name[i]);
System.out.print("The remark: ");
if(avg[i]>=85)
System.out.println("EXCELLENT");
else if(avg[i]>=75)
System.out.println("DISTINCTION");
else if(avg[i]>=60)
System.out.println("FIRST CLASS");
else if(avg[i]>=40)
System.out.println("PASS");
else
System.out.println("POOR");
}
}
}

Question 8
Write a program to input twenty names in an array. Arrange these names in descending order of alphabets using bubble sort technique. [15]

import java.util.*;
public class Sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String name[]=new String[20];
int i,j;
String temp;
for(i=0;i<20;i++)
{
System.out.println("Enter name");
name[i]= sc.next();
}
for(i=0;i<20-1;i++)
{
for(j =0;j<20-1-i;j++)
{
if(name[j].compareTo(name[j+1])<0)
{
temp=name[j];
name[j]= name[j+1];
name[j+1]=temp;
}
}
}
for(i=0;i<20;i++)
System.out.println(name[i]);
}
}

2016

Question 9
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, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations: MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example:
Country name: INDIA
Output: TAJ MAHAL
Country name: USA
Output: Sorry Not found! [15]

import java.util.*;
public class Wonders
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String wonder[]={"CHICHEN ITZA","CHRIST THE REDEEMER","TAJ MAHAL", "GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"};
String country[] = {"MEXICO", "BRAZIL","INDIA","CHINA","PERU","JORDAN", "ITALY"};
System.out.print("Enter the country name to be searched: ");
String scountry=sc.next();
boolean flag=false;
int i;
for(i=0;i<7;i++)
{
if(country[i].equalsIgnoreCase(scountry))
{
flag=true;
break;
}
}
if(flag==true)
System.out.println(wonder[i]);
else
System.out.println("Sorry Not Found!");
}
}

2017

Question 7
Write a program to input integer elements into an array of size 20 and perform the following operations::
(i) Display larger number from the array
(ii) Display smallest number from the array
(iii) Display sum of all elements from the array [15]

import java.util.*;
public class Array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int [20];
for(int i=0;i<20;i++)
{
System.out.println("Enter integer ");
a[i]=sc.nextInt();
}
int large=a[0],small=a[0],s=0;
for(int i=0;i<20;i++)
{
if(a[i]>large)
large=a[i];
if(a[i]< small)
small=a[i];
s+=a[i];
}
System.out.println("large: "+large);
System.out.println("Small: "+small);
System.out.println("Sum: "+s);
}
}

Question 9
Write a program to input forty words in an array. Arrange these words in descending order of alphabets using selection sort. Print the sorted array. [15]

import java.util.*;
public class Words
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[40];
for(int i=0;i<40;i++)
{
System.out.println("Enter word ");
a[i]=sc.next();
}
String large,temp; int index;
for(int i=0;i<39;i++)
{
large=a[i];
index=i;
for(int j=i+1;j<40;j++)
{
if(a[j]> large)
{
large=a[j];
index=j;
}
}
temp=a[i];
a[i]=a[index];
a[index]=temp;
}
for(int i=0;i<40;i++)
System.out.println(a[i]);
}
}

2018

Question 9
Write a program to accept name and total marks of N number of students in two single subscript array 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] [15]

import java.util.*;
public class Array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of students: ");
int N=sc.nextInt();
String name[]=new String[N];
double totalmarks[]=new double[N];
double deviation[]=new double[N};
double sum=0.0,average;
for(int i=0;i< N;i++)
{
System.out.println("Enter name and total marks: ");
name[i]= sc.next();
totalmarks[i]=sc.nextDouble();
}
for(int i=0;i< N;i++)
sum+= totalmarks[i];
average=sum/N;
for(int i=0;i< N;i++)
deviation[i]= totalmarks[i]-average;
System.out.println("Average of total marks: "+average);
System.out.println("Name\tTotal Marks\tAverage\tDeviation");
for(int i=0;i< N;i++)
System.out.println(name[i]+"\t"+totalmarks[i]+"\t"+average+"\t"+ deviation[i]);
}
}

2019

Question 6
(e) Write a program to input 15 integers in an array and sort them in ascending order using bubble sort. [15]

import java.util.*;
public class Sort
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int i;
for(i=0;i<15;i++)
{
System.out.println("Enter number ");
a[i]=sc.nextInt();
}
//Sorting Process
int j,temp;
for(i=0;i<15-1;i++)
{
for(j=0;j<10-1-i;j++)
{
if(a[ j]>a[ j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted Array");
for(i=0;i<15;i++)
System.out.print( a[i]+"\t");
}
}

2020

Question 5
Write a program to search for an integer value input by the user in the sorted list given below using binary search technique. If found display "Search Successful" and print the element, otherwise display "Search Unsuccessful" {31, 36, 45, 50, 60, 75, 86, 90} [15]

import java.util.*;
public class Search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
//Array initialization
int a[]={31, 36, 45, 50, 60, 75, 86, 90};
//seach number input
System.out.print("Enter the number to be searched: ");
int snumber=sc.nextInt();
int lower=0,upper=9,middle;
boolean flag=false;
//Searching process
while(lower<=upper)
{
middle=(lower+upper)/2; //To find the middle element
if(snumber==a[middle])
{
flag=true;
break;
}
else if(snumber>a[middle])
lower=middle+1;
else
upper=middle-1;
}
if(flag==true)
System.out.println("Search Successful");
else
System.out.println("Search Unsuccessful");
}
}