Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

OVED PROBLEMS ON SINGLE DIMENSIONAL ARRAYS (SDA)

SOLV

Write a program to accept 20 different numbers in a Single Dimensional


Prog. 1
Array (SDA). Display the sum of all the numbers which are divisible by
either 3 or 5.

display
the sum of numbers which are divisible by 3 or 5
/ To
import java.util.*;
public class Sum

public static oid main(String args[)

Scanner in=new Scanner(System.in):

int i,s=0;

int ml=new int[20];


for(i=0;i<20;i++)

System.out.print("Enter the number in the cell");


m]il=in.nextlnt();

for(i=0;i<20;i++)

if(m[ij%3==0llm[i]%5==0)
S=S+m[i];

System.out.println("The sum of numbers divisible by 3 or 5:"+s);

Execution of the program


x

2 BlueJ: Terminal Window Comp Appl 10


Options
Enter the number in the cell :33
Enter the number in the cell :44

Enter the number in the cell :12


55
Enter the number in the cell
Enter the number in the cell:37

: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

The sum of numbers divisible by 3 or 5 :178

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

public static void main(String args[])


Scanner in=new Scanner(System.in);
int i,min,max;
int m[]=new int[10];
for(i=0;i<10;i++)

System.out.print("Enter the no. in the cell :");


mil=in.nextlnt();

max=m[0];min=m[0];
for(i-0;i<l0;i++)
if(m[i]>max)
max=mfi];
if(m[il<min)
min=mi]:

System.out.println("The greatest of the array elements ="+max);


System.out.println("The smallest of the array elements ="+min);

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.

198 Understanding Computer Applications with BlueJx


To
display the
the number of students with an aggregate marks
util, *
java.
inmport
c l a s sA v e r a g e

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++)

ystem.out.println("Ma secured in Physics");


phyli = in.nextlnt);

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++)

if(phy[il+chem[i]+maths[i)/3.0 >= 80)

cl++
if(phyliJ+chem|il+maths[i])/3.0<= 34)
c2++;

System.out.println("Number of students getting 80% and abovelt"+c1);


System.out.println("Number of students getting 34% and belowlt"+c2);

Prog. 4 Write a program to accept 10 different numbers in a Single Dimensional


Array (SDA). Now, enter a number and search whether the number is
present or not in the list of array elements by using the Linear Search'

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

Enter a number to be searched: 42


The number is present

Arrays (Single Dimensional and Double Dimensional)| 199


To search a number in SDA by using Linear Search technique
import java.util.";
public class Search

public static void main(String args||)

Scanner in=new Scanner(System.in);


int i,k=0,ns;
int m[]=new int[10];
for(i=0;i<10:i++)

System.out.print("Enter the number in the cell :");


m[i]=in.nextlnt();

System.out.print("Enter the number to be searched:");


ns=in.nextlntO;
for(i-0;i<10;i++)
if(m[il==ns)
k=l;

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

200 | Understanding Compurer Applications with BlueJ_


ublic static void
main(String args
Scanner in=new Scanner(Syste
stem.in);
int i,k=0.p=0,ns,lb=0,ub=9:

int ml]=new int[10];


for(i-0;i<10;i++)

Sustem.out.print("Enter the number in the cell "):


m[il=in.nextlnt();

System.out.print("E er the number to be


searched");
ns=in.nextlnt();

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");

Prog. 6 Write a program to accept 10 diferent numbers in a Single Dimensional


Array (SDA). Arrange the numbers in ascending order by using 'Selection
Sort' technique and display them. ICSE 2006, 2011]
To Selection Sort technique
arrange the numbers by using
mport java.util.*;
public class Selection_Sort

public static void main(String argsll)

Arrays (Single Dimensional and Double Dimensional) | 201


Scanner in=new Scanner(System.in); for
int ijit.min; ali
int m[l=new int[10];
for(i-0;i<10;i++) for

System.out.print("Enter the number in the cell "); if(a


m[il=in.nextlnt();
if(a
for(i-0;i9;i++)
if(a
min=i,
for(j=i+1j«10:j++)
Sysc
if (mjl<m[min]) Syst
min=j; Syst

t=m[i];
mlil=m[min};
m[min]=t;, Pr

System.out.println("The numbers arranged in ascending order are:");


for(i=0;i<l10;i++) //To
System.out.println(m[i]); impo
publi

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

Scanner in = new Scanner(System.in); forg-C


int ij,.cl=0,c2=0,c3=0;
int all = new int[10]; if(mlj]-
System.out.println("Enter 20 numbers"):
t=mj]:
Computer Applications with BlueJ_x
202 I Understanding
forli=0;i<20;i++)

alil = in.nextInt();

for(i-0;i<20;i++)

if(ali]%2 == 0) {

cl++
ifali]%2 l=0)
c2++;
if(ali1%4 == 0)

c3++

Svstem.out.println(""Number of even numberslt""+c1);


System.out.printin("Number of odd numberslt"+c2);
System.out.println(""Number of multiples of 41t"+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

public static void main(String args[])

Scanner in=new Scanner(Systenm.in);


int ij.t
int m[]=new int[10];
for(i-0;i<10;i++)
System.out.print("Enter numbers in the cell ");
mi=in.nextlnt);

for(i=0;i<9;i++)

forj-0:j«(9-i)j++)
if(mj>m[j+1)
t=m[jl;

Arrays (Single Dimensional and Double Dimensional) | 203


mjl=m[j+1|
mlj+1]=t;

System.out.println("The numbers arranged in ascending order are )

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

who have given the


stored in the array to display the number of students
correct answer. Write the program in Java what the teacher would write

to solve the purpose.

/ To check the answer with Teacher's answer


import java.util.*;
class Triangle

public static void main()

Scanner in = new Scanner(System.in)

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

System.out.println In("Triangle formation is not


possible");

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

public static void main()

Scanner in new
Scanner(System.in);
=

int all =
new int[20]:
int n,s,i.j;

System.out.println( Enter integer numbers");


for(i-0;i<20;i++)
ai = in.nextlnt();

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++)

Arays (Single Dimensional and Double Dimensional) | 205


ifali]oj == 0)

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

public static void main()

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+)

Applications with BlueJx


206 | Understanding Computer
forg=0:j<(59-1)j++)

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]);

SOLVED PROGRAMS ON DOUBLE DIMENSIONAL


ARRAYS
Prog. 12 Write program in Java to store the numbers in a 3*4 matrix in a Double
a

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

To display the sum of the matrix elements


import java.util.*;
public class ddsum

public static void main(String args [ 1)

Scanner in=new Scanner(System.in);


int ij,.s;s=0;
int m[ 1[ ]=new
int[3]1[4]:
System.out.println("Enter the numbers of the matrix");
for(i-0;i<3;i++)
forj-0;j<4j++)
Arays (Single Dimensional and Double Dimensional) | 207
milll=in.nextlnt();

System.out.println("The numbers of the matrix are:");


for(i=0;i<3;i++)

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];

System.out.println("*"The sum of the elements in the matrix is "+s);

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

public static void


main(String args [ ])
Scanner in=new Scanner(System.in);
int i.j,.min,max;
min-0;max=0;
int m[ ][ =new int[4][4;

System.out.println("Enter the
numbers of the matrix")
for(i-0:i<4ji++)

208 | Understanding Computer ApPplications with BlueJ_x


f o r j = 0 j 4 j + + )

m i ] 0 l = i n . n e x t I n t ( ) ;

System.out.println(" "The numbers of the matrix are:");


for(i=0;i<4;i++)

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:

System.out.println("The lowest number in the array is "+min);


System.out.println("The highest number in the array is "+max);

Prog. 14 Write a program in Java to store the numbers in a 4*4 matrix in a


Double Dimensional Array. Find the transpose of the matrix by using
an assignment statement. The transpose of a matrix is obtained when we
change the row elements into colunmn and column elements into rows.
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 transpose of the matrix is
12 24 61 59
21 41 11 82
13 51 30 41
14 33 29 76

Arrays (Single Dimensional and Double


Dimensional) | 209
ITo find the transpose of a matrix
import java.util.*;
public class ddtranspose

public static void main(String args[])


Scanner in=new Scanner(System.in);
int ij;
int m[ I[ l=new int[4]41:
int n[ I[ l=new int[4][4]:
System.out.println("Enter the numbers of the matrix"):
for(i=0;i<4;:i++)

forj-0;j<4;j++)
m[i]jl=in.nextint();

System.out.println(*"The numbers of the matrix are:");


fori-0;i<4;i++)

forj-0:;j<4j++)

System.out.print(mfilG]+* *");

System.out.println();
for(i-0;i<4;i++)

forj=0j<4j++)

njllil=mlilG:

System.out.println("The transpose of the matrix is:");


for(i-0;i<4;i++)

forj-0j4j++)
System.out.print(n[i][jl+* ");
System.out.println);

210 Understanding Computer Applications with BlueJ-xx


Prog. 15 rite a progra
in Java to
Double Dimensional store
and the sum of the Array. Find thethe numbbers in a *4
input statement. numbers of each
sum of the matrix in a
numbers
column of of
Sample Output: the matrix each row
by using an
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
The sum of the
elements of each row
elements of 1st row
The sum of the
elements of 2nd row 60
=

The sum of the


elements of 3rd row 149
=

The sum of the


elements of 4th row 131
=

The sum of the 258


elements of each column
The sum of the elements of 1st column =156
The sum of the elements of 2nd column 155
The sum of the
=

elements of 3rd column 135


The sum of the
=

elements of 4th column 152 =

II To find the sum of rows and columns


import java.util.*;
public class ddrow_column

public static void main(String args [ ])

Scanner in=new Scanner(System.in);


int ij.r.c;
int m[ ]I =new int[4][4]:
System.out.println("Enter the numbers of the matrix:");
for(i=0;i<4;i++)
forj=0;j<4:j++)
m[illjl=in.nextnt():
System.out.println(*"The numbers of the matrix are:");
for(i=0;i<4;i++)
for(j=-0;j<4;j++)
System.out.print(m[ilj]+* ");

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:

System.out.println(The sum of the elements of"+(i+1)+"row = "+r);

System.out.println("The sum of the elements of each column:");


for(i=0;i<4;:i++)
c=0;
forj-0:j<4:j++)
c=c+mjlli]:
System.out.println(""The sum of the elements
of"+(i+1)+*column =
"+c);

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

public static void


main(String args[ 1)
Scanner in=new Scanner(System.in):

212 | Understanding Computer Applications with


BlueJ-x
i n t 1J.ld,ra,8, I0

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;

System.out.println("The sum of the right diagonal elements ="+rd);

to store the numbers in two different Double


Prog. 17 Write aprogram in Java
Find the sum of the numbers
Dimensional Arrays m[4] [4] and n[4] [4].
two arrays m and n and store them
of the corresponding elements of the
statement. Display the elements
in the array p[4] [4] by using an Input
form.
of array p in a matrix
Sample Input:
Array n
Array n Array P
33 22 8 22 31 15 20 45 64 37
12 23
44 10 11 16 30 26 31 47 74 36
20 31
8 10 17 25 33 20 42 39 41
10 25 14
41 5224 12 12 24 21 32 53 76 45 44

Arrays (Single Dimensional and


Double Dimensional) | 213
To find the sum of the corresponding elements of the two matrices
import java.io.*;
public class ddsum

public static void main(String args [ ]) throws 1OException

InputStreamReader read =new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
intij:
int m[ I[ l=new int[4]I4):
int n[ ][ J=new int[4][4]:
int p[ Il =new
int[4][4
System.out. println("Enter the numbers of the first matrix");
for(i-0;i<4;i++)

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

214 | Understanding Computer Applications with BlueJ-x


the maximum and minimum temp of the day
To display
import java.io.*;

public class dd_temp

ublic statiC void


p u b
main(String args[ ]) throws IOException
InputStreamReader read new InputStreamReader(System.in);
RufferedReader in = new BufferedReader(read);

int 1.J
String day
String m[ I =new String[7][3];
for(i-0;i<7;i++)

for(j-0:j<3:j++)

System.out.println("Enter day, maximum temp, minimum temp:");


m[i]j]=in.readLine();

System.out.println("Enter day of the week to know the temperature");


day=(in.readLine();
for(i-0;i<7;i++)

ifday.compareTo(m[i]O])==0)

System.out.println("The maximum temperature of "+day+" is "+m[i][1]);


System.out.println("The minimum temperature of "+day+" is "+mfi][2]);

A metropolitan hotel has 10 floors, numbered from 0 to 9; each floor


Prog. 19
for the name of
having 50 rooms. Using a single subscripted variable
the customers and a double subscripted variable R (F) where F and I
numbers respectively. Write a program in
represent the floor and room
Java for the room allocation work. The program should automatically
record the name of the customer and the room number. [ICSE 1995]

allocation work for a visitor


A program for room

import java.io.*;
public class Hotel

215
ATrays (Single Dimensional and Double Dimensional)|
public static void main(String args[]) throws 1OException

InputStreanmReader read =new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
int ij,k=0;
String name[]=new String[500]:
String m[=new String[10][50];
for(i=1;i<500;i++)

System.out.println("Enter the name of the visitor");


namelil=in.readLine(0:

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

II To check whether a matrix is symmetric or not


import java.util.*;
public class ddsymmetric

public static void main(String args [ ])

216 Understanding Computer Applications with BlueJ-x


Scanner in=new Scanner(System.in);
int ij.k:k=0;

=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();

System.out.println("The numbers of the matrix are:");


for(i-0;i<3;i++)

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.

memory structure created that contains a number of variables [ICSE 2012]


Ans. An array is a
variable name but different with the same
subscripts
Statement to declare an array
in order to deal with a
number of data values.
int al ] = new int [10]; OR
10 containing integers:
int [ Ja int
=
new [10);
Q.8 Name the search sort
algorithm that:
or

i)Makes several passes


through the array, selecting the next smallest item in
each time and the array
placing it where it belongs in the array.
(i) At each stage, compares the
element of the array. sought key value with the key value of the
middle
Ans. ) Selection Sort [ICSE 2012]
(ii) Binary Search
Q.9 If int n[ ] {1, 2, 3, 5, 7, 9, 13,
=

16), what are the values of x and


x
=Math.pow(n[4], n[2]); y?
y Math.sqrt(n[5]
Ans. The value of x
+
n[7]);
343 =
ICSE 2013]
The value of y = 5
oQ.10 Find the errors in the
given program
assign values to an segment and rewrite the
statements
int a = new int(5);
integer array. correctly to

for(int i=0;i<=5;i++) alil=i;


Ans. The errors are: ICSE 2014]
i) In the first line, the variable must be
new int [5 a[ ] and 5 must be inside
square brackets as

218 Understanding Computer APplications with Blue.l_x


(ii) In the second
line, the relational operator
The correct statements must be
are: <<
instead of <=
int all =
new
int[51;
for(int i=0i<5;i++) alil=i;
Q.11 ) What will this code
int arr[ print?
=
new int [15; [ICSE 2015]
i)
System.out.println(arr);
0
ii) 0000 (i) Value stored in arr [0]
Ans. (iv) Garbage value iv) garbage value
o.12 If int x [ ] 4, 3, 7, 8, 9,
=

10}; what are the values of and


G) p=x.length p q? [ICSE 2016
Ans. i) 6 (i) q= x{2] + x[5]*x[1]
i) 37

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);

3. int al ]=new int [5


a[0]=4; a[l=8; a[2]=7; a[3]=12; al4]=3;
System.out.println(a[2+1]);
4. int al4]={2,4,6,8];
for(i=0;i<=li++)

s=alil+a[3-il;
System.out.println(s);

II. Write short answers:


1. What is meant
by Dimensional Array?
2. Name the two types of Dimensional
Array.
3. What is the need of Dimensional
Array? Explain.
4. 'Array is a composite data type'. Explain this statement.
5. Define the following with their constructs:
(a) Single Dimensional Array
(b) Double Dimensional Array
IV. Differentiate between the following:
1.
Subscript and Subscripted variable
2. Char a[5] and int al5]
3. Ordinary variable and array variable
4. String a[5] and String al ]
5. Sorting and Searching
6. Linear search and [ICSE 2018
Binary search
7. (ICSE 2007, 2011]
Sequential sort and Bubble sort
8. length and length( )
[ICSE 2010]
V. Unsolved Programs on
Single and Double Dimensional Arrays:
1. Write a in
program Java to store 20 numbers (even and odd
Dimensional Array (SDA). Calculate and numbers) in a
Single
odd numbers separately. display the sum of all even numbers and all
2. Write a
program in Java to store 20
(SDA) and temperatures in °F in a Single Dimensional
display all the
temperatures after converting them into °C.
Array
Hint:=
5
f-32
9
3. Write a
program in Java to store 10 numbers
in a Single Dimensional (including positive and negative numbers)
the
Array (SDA). Display all the
negative numbers followed
positive numbers without changing the order of the numbers. by
Sample Input:
n[0 n[1] n[2] n3] n[4]
15 21 -32 41
n[5 n[6 n[{7] n8] n[9
54 61 71 -19 -44 52
Sample Output: -32, 41,-19, -44, 15, 21, 54, 61, 71, 52

220 Understanding Computer Applications with BlueJ-x


4. Write a
program in Java to store 20
Display the numbers numbers in a Single Dimensional SDA).
Array (SDA).
which
Sample Input:
are
prime.
n[0 n[1] n[2] n[3] n[4 n[5 n[6]
45 65 77 71
n[71 n[8] n[9
90 67 82 19 31 52
Sample Output: 71, 67, 19, 31
5. Write a
program to accept name and total
subscript arrays name[ ] and marks of N number of students in two
totalmarks[ ]. single
Calculate and print:
(i) The average of the total marks
obtained
(sum of total marks of all the by number of
N students.
[average =

(i) Deviation of each student's


total marks with the
students)/N]
deviation =
total marks of average.
student average]
a
6. The marks obtained by 50
[ICSE 2018
students in a subject are tabulated
Name
Marks
as
follows
'*****
'***'******''*
'***'*****
****

*****
'***
***'***''******'***'**** *****'****'****
*******************'***********'**'**** *****"**** **"
******************** ** ****'*****'* *'
******'**.

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.

ArTays (Single Dimensional and Double Dimensional) | 221

You might also like