APC - Arrays 2 Programs
APC - Arrays 2 Programs
APC - Arrays 2 Programs
SOLV
display
the sum of numbers which are divisible by 3 or 5
/ To
import java.util.*;
public class Sum
int i,s=0;
for(i=0;i<20;i++)
if(m[ij%3==0llm[i]%5==0)
S=S+m[i];
:91
Enter the number in the cell
Enter the number in the cell :83
:77
Enter the number in the cell
:53
Enter the number in the cell
:78
Enter the number in the cell
Dimensional) | 197
Arrr (Singlo Dimoncional
and Double
Prog. 2 wte a program to accept 10 different numbers in a Single Dimensional
Array (SDA). Display the greatest and the smallest numbers of the array
elements. ICSE 2007,2017]
Sample Input:
n[0 n[l] n[2] n[3] n[4] n[5) n[6] n[7] n[8] n[9]
43 45 92 87 64 76 81 65 12 31
Sample Output:
Greatest element: 92
Smallest element: 12
ITo display the greatest and the smallest numbers in SDA
import java.util.*;
public class Max_Min
max=m[0];min=m[0];
for(i-0;i<l0;i++)
if(m[i]>max)
max=mfi];
if(m[il<min)
min=mi]:
Prog. 3 Write a
program to accept the marks in
secured by 40 students of a class in Physics, Chemistry and Maths
and display
the
a
Single Dimensional Array. Find
following:
i. Number of students securing 80% and above in
ii. Number of students securing 34% and below in aggregate.
aggregate.
void main()
statiC
public
=
new Scanner(System.in);
in
nner
int ijcl=0,.¢2=0;
new int[40;
intphyl= new int[40;
intchem||= = new int[40];
maths||
int
for(i=0:i<40;i++)
secured in Chemistry'"):
Svstem.out.println("Marks
chem[i = in.nextlnt);
System.out.println(""Marks secured in Maths"):
maths[i] = in.nextlnt();
fori-0;ic40:i++)
cl++
if(phyliJ+chem|il+maths[i])/3.0<= 34)
c2++;
technique.
Sample Input:
n[O] n[l] n[21 n[3] n[4] n[5] n[6] n[7 n[8] n[9]
74 56 62 77 46 67 59 80 42 26
ifk=1)
System.out.println("The number is present");
else
System.out.println("The number is not present");
Prog. 5 Write a
program
accept 10 different numbers in a Single Dimensional
to
Array (SDA). Now, enter a number and by using
check whether or not the number is binary search technique,
present in the list of
If the number is array elements.
present then display the message "Search
otherwise, display "Search successful"
unsuccessful ICSE 2010]
Sample Input:
n[0] n[1] n[2] n[3] n4] n[5] n[6] n[71 n[8] n9]
43 45 92 87 64 76 81 65 12 31
Enter a number to be
searched: 87
The search successful and
the number is
present
I To search a number in SDA
by using Binary Search
import java.util.*; technique
public class B_Search
while (lbe=ub)
p-(Ib+ub)/2;
if(m[pl<ns)
lb=p+1;
if(mlpl>ns)
ub=p-1;
if(mlpl=ns)
k=l;
break;
if(k=1)
System.out.println("The search successful and the number is present");
else
System.out.println("The search unsuccessful and the number is not present");
t=m[i];
mlil=m[min};
m[min]=t;, Pr
publi-
Prog. 7 Write a program to accept 20 integer numbers in a Single Dimensional
Array. Find and display the following: Scan
i. Number of even numbers. int ij
ii. Number of odd numbers. int m
iii. Number of multiples of 4.
for(i=
I To display number of even, odd and multiples of 4
import java.util. *; Syster
class Number m[i]
}
public static void main() for(i=C
alil = in.nextInt();
for(i-0;i<20;i++)
if(ali]%2 == 0) {
cl++
ifali]%2 l=0)
c2++;
if(ali1%4 == 0)
c3++
Prog.8 Write a
program to
accept a set of 10 integers in a Single Dimensional
Array (SDA). Sort the numbers in ascending order by using the "Bubble
Sort' technique. Display the sorted array. ICSE 2005, 2013]
IITo arrange the numbers by using Bubble Sort technique
import java.util.*;
public class Ascending
for(i=0;i<9;i++)
forj-0:j«(9-i)j++)
if(mj>m[j+1)
t=m[jl;
for(i-0;i<10;i++)
System.out.println(m[i|):
to her students to
Prog. 9 In a class, the Mathematics teacher gave an assignment
find the area of a scalene triangle. However, she writes a program taking
area. Further, she enters
the length of the sides as input and calculates its
the answers obtained by all 40 students in a Single Dimensional Array.
The program compares the area calculated by the
teacher and the answers
double s,s1,s2,53,area;
int c=0,i;
double ans[] = new double[40]:
d
System.out.printin("Enter the sides of the triangle"):
sl = in.nextDouble();
$2 = in.nextDouble();
s3 = in.nextDouble():
System.out.println("Enter the answers of 40 students");
for(i=0;i<40;:i++)
S
ans[il=in.nextDouble(0
if(s1+82)>s3 && (s2+s3)>s1 && (sl+s3)>s2)
ca
s=(s1+s2+s3)/2.0;
area-Math.sqrt(s*(s-s l)*(5-s2)*(s-s3);
for(i=0;ic40;i++)
if(ansli] == area)
C++
Appications with BlueJ-x
Computer
204 I Understanding
yStemi.out.piilnln( Number
S y S
of students
adents en
correc answeert"+c);
else
Prog. 10 Write a
program to accept a set of 20
Array. Using menu driven integers in
choice: Single Dimensional
approach display the following,
a
as per user's
i. All the
ii.
perfect numbers
All the buzz stored in the
numbers stored array.
[A number whose in the
sum of factors array.
itself) is the same is
said
(including 1 and excluding the number
divisible by 7 or has the to be last
a
perfect number. A number that is
II To
digit as 7, is said to be a buzz number
display perfect numbers and buzz numbers
import java.util.*,
class Number
Scanner in new
Scanner(System.in);
=
int all =
new int[20]:
int n,s,i.j;
System.out.println("Menu Items"
System.out.println("1.To display all the perfect numbers");
System.out.println("1.To display all the Buzz numbers");
System.out.println("Enter your choice");
n=in.nextlnt();
switch(n)
case 1:
for(i-0;i<20;i++)
s=0;
forj=1j<a[il:j++)
S S +
=
J
if(s== a[i))
System.out.println(a[i)
break;
case 2:
for(i=0;i<20;i++)
if(afi]%7 == 0 l ali1%10 = = 7)
System.out.println(a[i]D;
break;
default
System.out.println("Invalid choice");
Prog. 11 Write a
program to accept roll numbers and the marks secured in
Computer Applications' by 40 students of a class in a Single Dimensional
Array. Sort these marks obtained in descending order.
the roll number and Finally, display
marks of each student in sorted order.
ITo
display the marks in descending order
import java.util.*;
class Number
Scanner in =
new
Scanner(System.in);
int ij.t
introll[]= new int[40];
int marks[] =new
int[40];
System.out.println("Enter roll number and marks");
for(i=0;i<40;i++)
roll[i] = in.nextlnt():
marks[i] = in.nextlnt()
for(i=0;i<39;it+)
ifmarksjl<marks[j+1])
t=roll[jl:
rolljl=roll[j+1];
rolli+l]=t;
t=marks[ji];
marks[jl=marks[j+1];
marks[j+1]=t;
System.out.printin("Roll No.t"+"Marks");
for(i=0:i<40;i++)
System.out.println(rolli]+"\t"+marks[i]);
Dimensional Array. Find the sum of all the numbers of the matrix and
display the sum using an input statement.
Sample Output:
The numbers of the matrix are
12 21 13 14
24 41 51 33
61 11 30 29
The sum of the elements in the matrix is 340
forj=0j<4:j++)
System.out.print(m[i][jl+* ");
System.out.printin();
for(i=-0;i<3;i++)
{
forj=0j<4;j++)
S=S+m[i][j];
Prog. 13 Write a
program in Java to store the numbers in a 4*4 matrix in
a
Double Dimensional Array. Find the
the matrix by using an highest and the lowest numbers of
input statement.
Sample Output:
The numbers of the matrix are
12 21 13 14
24 41 51 33
61 11 30 29
59 82 41 76
The lowest number in the
array is 11
The greatest number in
the array is 82
I To find the max.
and min. of the matrix
import java.util.*; elements
public class ddMax_Min
System.out.println("Enter the
numbers of the matrix")
for(i-0:i<4ji++)
m i ] 0 l = i n . n e x t I n t ( ) ;
forý=0j<4j++)
{
System.out.print(m[i][j]+* *):
System.out.printn);
min=m[0][0];
max=m[0][0;
for(i-0:ic4;i++)
forj=0j<4j++)
if(min>m[i|0)
min=m[i]j]l:
ifmax<mli]GD
max=m1|Gl:
forj-0;j<4;j++)
m[i]jl=in.nextint();
forj-0:;j<4j++)
System.out.print(mfilG]+* *");
System.out.println();
for(i-0;i<4;i++)
forj=0j<4j++)
njllil=mlilG:
forj-0j4j++)
System.out.print(n[i][jl+* ");
System.out.println);
ystem.out.println0;
211
Arrays (Single Dimensional and Double Dimensional)|
System.out.println(The sum of the elements of each row: );
for(i=0;i<4;i++)
r=0;
for(j=0;j<4:j++)
r+m[iljl:
Prog. 16 Write a
program in Java to store
the numbers in 4*4
Dimensional Array. Find the sum of matrix in a Double
and the sum of the the numbers of
numbers of the left
an input statement. the right diagonal of the diagonal
matrix by using
Sample Output
The numbers of the
12 21 13 14
matrix are
24 41 51 33
61 11 30 29
59 82 41 76
The sum of the left
The sum of the right
diagonal elements =
159
II To find the
sum of left
diagonal elements =
135
and right diagonal elements
import java.util.*;
public class dddiagonals
m =neW int[4][4];
int
System.out.println n("Enter the number of the
matrix:");
f o r i = 0 : i 4 i + + )
forj=0:j<4j++)
mlilll=in.nextlnt();
Syster
em.out.println('* numbers of the matrix are:");
for(i=0;i<4;i++)
forij=0j<4:j++)
System.out.print(m[i]ljl+* ");
System.out.println();
for(i-0;i<4;i++)
ld=ld+m[i|:
System.out.println("The sum of the left diagonal elements = "+ld);
k=3;
for(i-0;i<4;i++)
rd=rd+m[i]|kl
k=k-l;
fortj=-0;j<4j++)
m[i]0]=Integer.parseInt(in.readLine0):
System.out.println("Enter the numbers of the second matrix ");
for(i=0;i<4;i++)
forj=0j<4j++)
n[i]g]=Integer.parselnt(in.readLine();
System.out.println("Sum of the array elements is");
for(i=0;i<4;i++)
forg=0j<4j++)
pliIG]=m[ilG+n[ilGl:
System.out.print(p[i]j]1+* ");
System.out.println);
Prog. 18 Write a
program to store a
day, its maximum
temperature in a Double Dimensional Array of temperature,
its minimum
7*3. The program
a particular
day of the week and displays the maximum and accepts
temperature of that day. Use an Input statement for the same. minimum
int 1.J
String day
String m[ I =new String[7][3];
for(i-0;i<7;i++)
for(j-0:j<3:j++)
ifday.compareTo(m[i]O])==0)
import java.io.*;
public class Hotel
215
ATrays (Single Dimensional and Double Dimensional)|
public static void main(String args[]) throws 1OException
for(i-0;i<10;i++)
forj-0j<50j++)
m[i]jl=name[k];
k=k+l;
for(i-0;i<10;i++)
for(j-0;j<50;j++)
System.out.print(m[i]G]):
System.out.println();
Prog. 20 Write a
program in Java to create a 3*3 Square Matrix and
in it. The store numbers
programmer wants to check whether the Matrix is
or not. A Symmetric
Square Matrix is said to be Symmetric if the element
ith row and jth column is of the
equal to the element of the jth row and ith
column. e.g., A Symmetric Matrix:
123
24 5
3 56
=new int[3][31;
int m[ Il
int[3][3]:
int n[ ll J=new
System.out.printlh the numbers of the matrix:");
for(i=0:i<3:it+)
for(j-0j<3j++)
mlilljl=in.nextlnt();
for(j-=0:j<3:j++)
System.out.print(m[i]g]+* ");
System.out.println();
for(i-0;i<3;i++)
forj-0:j<3:j++)
ifm[ilj]!=m[jlli))
k=l;
if(k-=0)
System.out.println("It is a Symmetric Matrix");
else
System.out.println("lt is not a Symmetric Matrix");
Dimensional)| 217
Arrays (Single Dimensional and Double
REVIEWinsight
Q.1 What is meant by direct initialization of an array? [ICSE Model]
Ans. Direct initialization of an array refers to assigning the values to the array at the time of
its declaration.
e.g.: int arr{] = {2,5,4,8,12.
Q.2 What is meant by the subscript of an array? [ICSE Modell
Ans. The cell number of the array used within square bracket along with array variable is said
to be the subscript of the array.
e-g: arrl6]. Here, 6 is the subscript of array arr
Q.3 Name a technique that can be used to concatenate (join) the elements of two different
arrays into a single array. [ICSE Model]
Ans. Merging
Q4 Write the difference between length and length() functions.
[ICSE 2010]
Ans.Length function is used to find the length (number of elements) of an array.
eg: int all=124,6,8,10};
Hence, a.length results in 5.
Length() function is used to find the length (number of characters) of string.
e.g.: String str = "Computer";
Here, str.length() results in = 8.
Q.5 Write one difference between Linear Search and
Ans. Linear search is Binary Search. [ICSE 2011
applicable to sorted as well as unsorted data items, whereas binary search
takes place only on sorted items.
Q.6 State the total size in bytes of the arrays a[4] of char data type and p[4] of float data
type.
Ans. i) char a[4] contains 8
[ICSE 2011]
bytes (i) float p[4] contains 16 bytes
Q.7 What is an
array? Write a statement to declare an
integer array of 10 elements.
EXERCISES
I. Tick the correct answer:
1. Which of the
(a) int al-40]
following
is the correct
usage?
(6) int a[40] (c) float a[0 - 40]
(d) None
2. Which element is
(a) 10th
represented by a[10]?
(6)gth )11th (d) None
3. Cell numbers of a dimensional
array are also known as:
(a) packets (b) blocks (c) subscripts
4. A dimensional
(d) compartments
array is also known as:
(a) subscripted variable (6) actual variable
(c) compound variable (d) none
5. An array element can be accessed
through:
(a) dots (b) element name (c) index number (d) none
6. Indjcate the error message which displays, if the following statement is executed
int a[5] = {28,32,45,68,12);
(a) Insufficient cells 6) Array index out of bounce
(c) Elements exceeding cells (d) None
7. The folowing statement: int code[ ]= {(25,37,38,42;
(a) assigns 37 to code[1] (6) assigns 25 to code[1]
(c) assigns 38 to code[3] (a) assigns 42 to code[0]
8. The elements of array[50] are numbered:
(a) from 1 to 50 (b) from 0 to 49 C) from 1 to 51 (a) none
9. Which of the following function finds the size of array
char ]m =
{'R,;A',T,E,N,D',R', A'};?
(a) m.size of (a) (b) m.elements of (m)
(c)m.length (d) None
10. A Single Dimensional array contains N elements. What will be the last subscript?
(a) N-1 (6) N
() Nt1 (d) None
II. Give the output of the following:
1. int m[] {2,4,6,81;
System.out.println(m[1] + * ' + m[2]);
219
Arays (Single Dimensional and Double Dimensional) |
int al =|2,4A6,8,10};
af0]=23;
al3]=a[l};
int c= a[01+al1];
System.out.println("Sum = "+c);
s=alil+a[3-il;
System.out.println(s);
*****
'***
***'***''******'***'**** *****'****'****
*******************'***********'**'**** *****"**** **"
******************** ** ****'*****'* *'
******'**.
Write a to
program input the names and marks of the students in the
subject.
Calculate and display:
(a) The subject average marks (subject
average marks =subject total/50.
(b) The highest marks in the subject and the name of the student.
marks in the subject are 100.) (The maxinmum
[ICSE 2006]
7. Write a program in Java
using arrays:
(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
Less than 40 D
8. Write a program to accept a list of 20 integers. Sort the first 10 numbers in ascending
order and next the 10 numbers in descending order by using Bubble Sort technique.
Finally, print the complete list of integers.
9. The class teacher wants to store the marks obtained in English, Maths and Science
of her class having 40 students. Write a program to input marks in Eng, Science and
Maths by using three single dimensional arrays. Calculate and print the following
information:
(i) Average marks secured by each student.
(i) Class average in each subject.
[Hint: Class average is the average marks obtained by 40 students in a particular subject.]
10. Write a program in Java to accept 20 numbers in a single dimensional array ar|[20].
Transfer and store all the even numbers in an array even[ ] and all the odd numbers
in another array odd[ ]. Finally, print the elements of both the
arrays.