0% found this document useful (0 votes)
77 views42 pages

Array@

The document provides a comprehensive overview of arrays in programming, including definitions, types, declarations, and examples of various operations such as searching and sorting. It includes Java code snippets for implementing single and double-dimensional arrays, performing linear and binary searches, and sorting using selection and bubble sort algorithms. Additionally, it presents exercises for practicing array manipulation and calculations with user input.

Uploaded by

angshumanmarandi
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)
77 views42 pages

Array@

The document provides a comprehensive overview of arrays in programming, including definitions, types, declarations, and examples of various operations such as searching and sorting. It includes Java code snippets for implementing single and double-dimensional arrays, performing linear and binary searches, and sorting using selection and bubble sort algorithms. Additionally, it presents exercises for practicing array manipulation and calculations with user input.

Uploaded by

angshumanmarandi
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

ARRAY

Def: Array is a collection of similar kinds of values.


Example;
A[ ]={5,20,35,80}
B[ ]={“Ram”,”Shyam”,”Gopal”}
C[ ]={“Ram”,55} -------- Wrong
Types of array:
1. Single dimension array
Ex: A[ ]
2. Double dimension array
Ex: A[ ] [ ]

Array declaration:
int a[ ]=new int[10];
String B[ ]=new String[10]; Dimension
int A[ ][ ]=new int[5][5]; Dynamic memory allocation
Note: Dimension means total number of values.
Searching:
1. Linear Search
2. Binary Search
Sorting:
1. Selection sort
2. Bubble sort
A[ ]= 5 10 15 90
Position= 0 1 2 3

A[0]=5
A[1]=10
A[2]=15
A[3]=90

Program:
1. Input 10 different numbers and add them using
SDA.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int i,sum=0;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=9;i++)
{
sum=sum+a[i];
}
[Link](sum);
}
}
2. Input 10 different numbers in SDA. Now find
the sum of all the odd numbers in array.
Ans:
3. Input 10 different numbers in SDA and display
the numbers which are BUZZ number.
Ans:
4. Input 10 different numbers in SDA. Now display
all the prime numbers of the array.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int i,j,count=0,n;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=9;i++)
{
n=a[i];
for(j=1;j<=n;j++)
{
if(n%j==0)
{
count++;
}
}
if(count==2)
{
[Link](n);
}
count=0;
}
}
}
Searching:
1. Input 10 different numbers in SDA and also
input another number to be search that
number is present or not using linear search.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int i,n,k=0;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
[Link]("Enter number to be
search=");
n=[Link]();
for(i=0;i<=9;i++)
{
if(n==a[i])
{
k=1;
break;
}
}
if(k==1)
{
[Link]("Number is Present");
}
else
{
[Link]("Number is not
present");
}
}
}
2. Input 10 different names in SDA. Now also
input another name to be search that name
is present or not using Linear search.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
String a[]=new String[10];
String n;
int i,k=0;
for(i=0;i<=9;i++)
{
[Link]("Enter Name=");
a[i]=[Link]();
}
[Link]("Enter name to be
search=");
n=[Link]();
for(i=0;i<=9;i++)
{
if([Link](a[i])==true)
{
k=1;
break;
}
}
if(k==1)
{
[Link]("Name is Present");
}
else
{
[Link]("Name is not present");
}
}
}
3. Input 10 different numbers in SDA. Now also
input another number to be search that
number is present or not using Binary search
technique.
Ans:
Array= 5 6 1 2 7 9 10 3 4 8
Position= 0 1 2 3 4 5 6 7 8 9
First Mid Last
position > Position
(f) < (l)

Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int n,i,k=0,mid=0,f,l;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
[Link]("Enter number to be
search=");
n=[Link]();
f=0;
l=9;
while(f<=l)
{
mid=(f+l)/2;
if(a[mid]>n)
{
l=mid-1;
}
if(a[mid]<n)
{
f=mid+1;
}
if(a[mid]==n)
{
k=1;
break;
}
}
if(k==1)
{
[Link]("Number is Present");
}
else
{
[Link]("Number is not present");
}
}
}
4. Input 10 different names in SDA. Now also
input another name to be search that name
is present or not using Binary Search.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
String a[]=new String[10];
int i,k=0,mid=0,f,l;
String n;
for(i=0;i<=9;i++)
{
[Link]("Enter Name=");
a[i]=[Link]();
}
[Link]("Enter name to be
search=");
n=[Link]();
f=0;
l=9;
while(f<=l)
{
mid=(f+l)/2;
if(a[mid].compareTo(n)>0)
{
l=mid-1;
}
if(a[mid].compareTo(n)<0)
{
f=mid+1;
}
if(a[mid].compareTo(n)==0)
{
k=1;
break;
}
}
if(k==1)
{
[Link]("Name is Present");
}
else
{
[Link]("Name is not present");
}
}
}
Sorting:
1. Input 10 different numbers in SDA. Now sort the
number in ascending order using selection sort.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int i,j,t,min;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=8;i++)
{
min=i;
for(j=i+1;j<=9;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
t=a[i];
a[i]=a[min];
a[min]=t;
}
for(i=0;i<=9;i++)
{
[Link](" "+a[i]);
}
}
}
2. Input 10 different numbers in SDA. Now sort them
in descending order using selection sort.
Ans:

3. Input 10 different names in SDA. Now sort the


name in ascending order using selection sort.
Ans:
4. Input 10 different names in SDA. Now sort the
name in descending order using selection sort.
Ans:
5. Input 10 different numbers in SDA and sort them in
ascending order using bubble sort.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[10];
int i,j,t;
for(i=0;i<=9;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<=9;i++)
{
[Link](" "+a[i]);
}
}
}
6. Input 10 different numbers in SDA. Now sort them
in descending order using Bubble sort.
Ans:
7. Input 10 different names in SDA. Now sort them in
ascending order in Bubble sort.
Ans:
8. Input 10 different names in SDA. Now sort them in
decending order in Bubble sort.
Ans:
9. Input 10 different names of cricket players and
their respective runs in another array. Now sort the
name as well as corresponding runs using Bubble
sort.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
String name[]=new String[10];
int run[]=new int[10];
int i,j,t;
String s="";
for(i=0;i<=9;i++)
{
[Link]("Enter Name=");
name[i]=[Link]();
[Link]("Enter Run=");
run[i]=[Link]();
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(name[j].compareTo(name[i])<0)
{
s=name[i];
name[i]=name[j];
name[j]=s;
t=run[i];
run[i]=run[j];
run[j]=t;
}
}
}
for(i=0;i<=9;i++)
{
[Link](name[i]+" "+run[i]);
}
}
}
Exercise:
1. WAP in java to store 20 numbers(even and odd
numbers) in SDA. Calculate and display the sum
of all even numbers and all odd numbers
separately.
Ans:
2. WAP in java to declare an array of size twenty of
double data type. Accept the elements into the
array and perform the following:
a. Calculate and print the product of all the
elements.
b. Print the square of each elements.
Ans:
3. WAP in java to store 10 numbers(including
positive and negative numbers) in a SDA. Display
all the negative numbers followed by the positive
numbers without changing the order of the
numbers.
Sample Input:
n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
15 21 -32 -41 54 61 71 -19 -44 52
Sample Output: -32, -41, -19, -44, 15, 21, 54, 61,
71, 52
Ans:
4. WAP in java to store 20 numbers in SDA. Display
the numbers which are prime.
Sample Input:
n[0] n[1] n2] n[3] n[4] n[5] … n[16] n[17] n[18] n[19]
45 65 77 71 90 67 82 19 31 52
Sample output: 71,67,….,19,31
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[20];
int i,j,k=0;
for(i=0;i<=19;ia++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=19;i++)
{
for(j=1;j<=a[i];j++)
{
if(a[i]%j==0)
{
k++;
}
}
if(k==2)
{
[Link](a[i]);
}
k=0;
}
}
}
5. WAP to accept name and total marks of N
number of students in two SDA name[ ] and
totalmarks[ ].
Calculate and print:
a. The average of the total marks obtained by N
number of students.
[average=(sum of total marks of all the
students)/N]
b. Deviation of each student’s total marks with
average.
[deviation=total marks of a student-average]
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int N,i;
double sum=0.0,dev=0.0,avg=0.0;
[Link]("Enter total number of
student=");
N=[Link]();
String name[]=new String[N];
double totalmarks[]=new double[N];
for(i=0;i<=N-1;i++)
{
[Link]("Enter Name=");
name[i]=[Link]();
[Link]("Enter Total Marks of each
student=");
totalmarks[i]=[Link]();
}
for(i=0;i<=N-1;i++)
{
sum=sum+totalmarks[i];
}
avg=sum/N;
[Link]("Average Marks="+avg);
for(i=0;i<=N-1;i++)
{
dev=totalmarks[i]-avg;
[Link](name[i]+" "+dev);
}
}
}
6. WAP in java using array:
a. To store the Roll no., Name and marks in six
subjects for 100 students.
b. Calculate the percentage of marks obtained by
each candidate. The maximum marks in each
subject are 100.
c. Calculate the Grade as per the given criteria:
Percentage Marks Grade
From 80 to 100 A
From 60 to 79 B
From 40 to 59 C
Less than 40 D
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int R[]=new int[100];
String name[]=new String[100];
int A[]=new int[100];
int B[]=new int[100];
int C[]=new int[100];
int D[]=new int[100];
int E[]=new int[100];
int F[]=new int[100];
int i;
double p;
String s="";
for(i=0;i<=99;i++)
{
[Link]("Enter Roll=");
R[i]=[Link]();
[Link]("Enter Name=");
name[i]=[Link]();
[Link]("Enter 1st Subject
marks=");
A[i]=[Link]();
[Link]("Enter 2nd Subject
marks=");
B[i]=[Link]();
[Link]("Enter 3rd Subject
marks=");
C[i]=[Link]();
[Link]("Enter 4th Subject
marks=");
D[i]=[Link]();
[Link]("Enter 5th Subject
marks=");
E[i]=[Link]();
[Link]("Enter 6th Subject
marks=");
F[i]=[Link]();
}
for(i=0;i<=99;i++)
{

p=((A[i]+B[i]+C[i]+D[i]+E[i]+F[i])/600.00)*100;
if(p>=80 && p<=100)
{
s="Grade A";
}
else if(p>=60 && p<=79)
{
s="Grade B";
}
else if(p>=40 && p<=59)
{
s="Grade C";
}
else if(p<40)
{
s="Grade D";
}
[Link](R[i]+" "+name[i]+" "+p+"
"+s);
}
}
}
7. WAP to accept a list of 20 integers. Sort the first
10 numbers in ascending order and next 10
numbers in descending order by using Bubble
Sort technique. Finally print the complete list of
integer.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[]=new int[20];
int i,j,t=0;
for(i=0;i<=19;i++)
{
[Link]("Enter Number=");
a[i]=[Link]();
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[j]<a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=10;i<=18;i++)
{
for(j=i+1;j<=19;j++)
{
if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<=19;i++)
{
[Link](" "+a[i]);
}
}
}
8. Define a class pin code and store the given pin
codes in SDA. Sort these pin codes in ascending
order using selection sort technique only. Display
the sorted array.
Sample Input:
[110061,110001,110029,110023,110006,110019,1
10033]
Sample Output:
[110001,110006,110019,110023,110029,110033,1
10061]
Ans:
9. WAP to store 20 numbers in SDA. Now display
only those numbers that are perfect squares.
Ans:
10. To get promotion in a Science stream, a
student must pass in English and should pass in
any of the two subjects(i.e Physics, Chemistry or
Maths). The passing marks in each subject is 35.
Write a program in SDA to accept the roll
numbers and marks secure in the subjects for all
the students. The program should check and
display the Roll numbers along with a message
whether “Promotion is Granted” or “Promotion is
not Granted”. Assume that there are 40 students
in the class.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int R[]=new int[40];
int E[]=new int[40];
int P[]=new int[40];
int C[]=new int[40];
int M[]=new int[40];
int i;
for(i=0;i<=1;i++)
{
[Link]("Enter Roll Number=");
R[i]=[Link]();
[Link]("Enter Marks of English=");
E[i]=[Link]();
[Link]("Enter Marks of Physics=");
P[i]=[Link]();
[Link]("Enter Marks of
Chemistry=");
C[i]=[Link]();
[Link]("Enter Marks of Maths=");
M[i]=[Link]();
}
for(i=0;i<=1;i++)
{
if(E[i]>=35 &&((P[i]>=35 &&
C[i]>=35)||(P[i]>=35 && M[i]>=35)
||(C[i]>=35 && M[i]>=35)))
{
[Link](R[i]+" "+"Promotion is
Granted");
}
else
{
[Link](R[i]+" "+"Promotion is
not Granted");
}
}
}
}
12.

P Q
0 0
1 1
2 2
3 3
4
5

R
0 0
1 1
2 2
3 3
4 4
5 5
6 0
7 1
8 2
9 3

Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int P[]=new int[6];
int Q[]=new int[4];
int R[]=new int[10];
int i;
for(i=0;i<=5;i++)
{
[Link]("Enter Number=");
P[i]=[Link]();
}
for(i=0;i<=3;i++)
{
[Link]("Enter Number=");
Q[i]=[Link]();
}
for(i=0;i<=5;i++)
{
R[i]=P[i];
}
for(i=6;i<=9;i++)
{
R[i]=Q[i-6];
}
for(i=0;i<=9;i++)
{
[Link](R[i]);
}
}
}
13.
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int g,f,l,mid=0,k=0,p=0;
int
a[]={1982,1987,1993,1996,1999,2003,2006,2007,20
09,2010};
[Link]("Enter year of graduation to be
search=");
g=[Link]();
f=0;
l=9;
while(f<=l)
{
mid=(f+l)/2;
if(a[mid]>g)
{
l=mid-1;
}
if(a[mid]<g)
{
f=mid+1;
}
if(a[mid]==g)
{
k=1;
p=mid;
break;
}
}
if(k==1)
{
[Link]("Record Exist"+p);
}
else
{
[Link]("Record does not Exist");
}
}
}
14.
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int n,i,avg=0;
[Link]("Enter number of student=");
n=[Link]();
int R[]=new int[n];
String name[]=new String[n];
int A[]=new int[n];
int B[]=new int[n];
int C[]=new int[n];
for(i=0;i<=n-1;i++)
{
[Link]("Enter Roll=");
R[i]=[Link]();
[Link]("Enter name=");
name[i]=[Link]();
[Link]("Enter 1st Sub. Marks=");
A[i]=[Link]();
[Link]("Enter 2nd sub. Marks=");
B[i]=[Link]();
[Link]("Enter 3rd sub. Marks=");
C[i]=[Link]();
}
for(i=0;i<=n-1;i++)
{
avg=(A[i]+B[i]+C[i])/3;
if(avg>=85 && avg<=100)
{
[Link](R[i]+" "+name[i]+"
"+"Excellent");
}
else if(avg>=75 && avg<=84)
{
[Link](R[i]+" "+name[i]+"
"+"Distinction");
}
else if(avg>=60 && avg<=74)
{
[Link](R[i]+" "+name[i]+" "+"First
Class");
}
else if(avg>=40 && avg<=59)
{
[Link](R[i]+" "+name[i]+"
"+"Pass");
}
else if(avg<40)
{
[Link](R[i]+" "+name[i]+"
"+"Poor");
}
}
}
}
2D Array:
Define 2d array:
int a[ ][ ]=new int [3][4];
for(i=0;i<=2;i++)
{
for(j=0;j<=3;j++)
{

1. Create a 4 x 4 matrix and find the sum of all the


values of the matrix.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[][]=new int[4][4];
int i,j,sum=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link]("Enter Number=");
a[i][j]=[Link]();
}
}
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link](" "+a[i][j]);
}
[Link]();
}
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
sum=sum+a[i][j];
}
}
[Link](sum);
}
}
2. Create a 4x4 matrix and find sum of all the even
number and odd number separately.
Ans:
3. Create a 4x4 matrix and input another number to
be check how many times the input number
present within the matrix.
4. Create a 4x4 matrix. Now find the sum row wise
of the matrix.
Ex:
10 20 30 40
20 30 40 50
30 40 50 60
40 50 60 70
1st Row sum= 10+20+30+40
2nd Row sum= 20+30+40+50
3rd Row sum= 30+40+50+60
4th Row Sum= 40+50+60+70
Ans:
5. Create a 4x4 matrix. Now find the sum of column
wise of the matrix.
Ans:
6. Create a 4x4 matrix. Now find the sum of left and
right diagonal values.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[][]=new int[4][4];
int i,j,sum=0,sum1=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link]("Enter Number=");
a[i][j]=[Link]();
}
}
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link](" "+a[i][j]);
}
[Link]();
}
for(i=0;i<=3;i++)
{
sum=sum+a[i][i];
}
[Link]("Left diagonal sum="+sum);
for(i=0;i<=3;i++)
{
sum1=sum1+a[i][3-i];
}
[Link]("Right diagonal
sum="+sum1);
}
}
15. A double dimensional array is defined as
N[4][4] to store numbers. Write a programs to
find the sum of all even numbers and product of
all odd numbers of the elements store in Double
Dimension Array(DDA).
Ans:
18. A class teacher wants to keep the records of
40 students of her class along with their names
and marks obtained in English, Hindi, Maths,
Science and Computer Science in a double
Dimensional array(DDA) as M[40][5].
When the teacher enter the name of a student as
an input, the program must display the name,
marks obtained in the 5 subjects and the total.
Write a program in java to perform the task.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
String name[]=new String[40];
int M[][]=new int[40][5];
int i,j,k=0,sum=0;
String a="";
for(i=0;i<=39;i++)
{
[Link]("Enter Name=");
name[i]=[Link]();
for(j=0;j<=4;j++)
{
[Link]("Enter Subject Marks=");
M[i][j]=[Link]();
}
}
[Link]("Enter Name to be Search=");
a=[Link]();
for(i=0;i<=39;i++)
{
if([Link](name[i])==true)
{
k=i;
break;
}
}
[Link]("Name="+name[k]);
for(i=0;i<=4;i++)
{
[Link](M[k][i]);
sum=sum+M[k][i];
}
[Link]("Total Marks="+sum);
}
}
20.
Ans:
import [Link].*;
public class Tech
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int a[][]=new int[4][4];
int i,j,max=0,min=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link]("Enter Number=");
a[i][j]=[Link]();
}
}
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
[Link](" "+a[i][j]);
}
[Link]();
}
max=a[0][0];
min=a[0][0];
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
if(a[i][j]<min)
{
min=a[i][j];
}
}
}
[Link]("Greater element="+max);
[Link]("Smaller element="+min);
[Link]("Difference between Greater
and Smaller="+(max-min));
}
}

You might also like