Icse Computer Application: Topic 5-Array
Icse Computer Application: Topic 5-Array
TOPIC 5- ARRAY
Array: Array is the most important thing in any programming language. By definition, array is
the static memory allocation. It allocates the memory for the same data type in sequence. It
contains multiple values of same types. It also store the values in memory at the fixed size.
Multiple types of arrays are used in any programming language such as: one -
dimensional, two - dimensional or can say multi - dimensional.
Declaration of an array:
int num[]; or int num = new int[2];
Some times user declares an array and it's size simultaneously. You may or may not be define the
size in the declaration time. such as:
int num[] = {50,20,45,82,25,63};
Ques 3:- How one dimension array is different from double dimension array?
Ans:- One Dimension:- A list of fixed number of homogeneous data elements.
Double Dimension:- An array in which each element itself an array.
Ques 5:- What is the difference between linear and binary search?
Ans:- Linear:- Each element of the array is compared with the given item to be searched for, one
by one.
Binary:- Searches for the given item in a sorted array the search segment reduces to half at every
successive stage.
note:- array can be initialized at the time of declaration otherwise we need to initialize
each element separately .
Ques 8 What do you mean be arrayindexoutof Bound
Ans :- It is an Exception which occurs when we try to access any element which is beyond 0 to
size-1.
Ques 9:- Define Selection Sorting.
Ans: if the array is to be arranged in ascending order, the smallest element needs to be selected
and exchanged with the first position element in the array. Again, the smallest element from the
remaining elements is selected and exchanged with second array. The process continues till all
elements are arranged. If the array needs to be arranged in descending order. Select the largest
element.
Ques 10;- Define Bubble Sort.
Ans:- The adjacent elements of the array are compared. If array needs to be arranged in
ascending order and the left element(say a[0]) is greater than the right element(say a[1]) they
need to be exchanged.
Ques 11 Which element is num[9] of the array num .[2005]
Ans:- num [9] will be the 10th element of the array because index of an array start from 0.
1. Data grouped together into a single unit for a specific purpose is known as data structure.
2. Array is a data structure where similar types of data are put together, stored in continuous
locations and is referred to by a single name.
3. Arrays occupy a group of contiguous memory locations all having the same name and type.
4. To refer to a particular location, we specify the name of the array and its position number.
5. Its position number is called an element or a subscript or an index of the array.
6. Subscript of an array must be an integer.
7. In Java the first element of an array is its zeroth element and the last element 1 less than the
total elements in an array.
8. Subscripts are enclosed within square brackets next to the name of the array.
9. To retrieve an individual array element, array name along with its subscript should be
specified.
10. An array of smaller size can be initialised using an initializer list.
11. Newly created array of integers is automatically filled with default values: 0 for numbers,
false for boolean, and null for objects.
12. Searching and sorting are two basic array-processing techniques that are frequently used.
13. Sorting refers to rearranging the data of an array in some particular order increasing or
decreasing.
14. Searching refers to finding an item in an array, depending on some specific criterion.
15. Linear searching is the simplest searching method and is done when the required element is
searched sequentially in an array.
16. Binary searching is a faster way as it eliminates one half part every time while the search is
carried on.
Program 5- To sort numbers in ascending order using selection sort technique [2006]
import java.util.*;
public class selectionSort{
public static void main(String a[]) throws Exception
{
int i,j;
int a[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] > a[j]){
t = a[j];
a[j]=a[i];
a[i]=t;
}
}
}
System.out.print("Values after the sort:\n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
}
}
Program 7- Write a menu driven program to find sum of prime numbers or find smallest
and largest number or array.
import java.util.*;
class array_menu
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int arr[]={35,34,67,87,45,33,11,91,2,44,32};
Program 8- Write a program to store number in a matrix and make is intractive to find
import java.util.*;
class matrix_menu
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
tot=tot+marks[i];
}
System.out.println(“Average marks of the student”+(tot/50));
System.out.println(“Highest mark is ”+high+” highest scores is “+name[pos]);
}
}
int I,j,k=1,m;
i=--a[0];
j=a[2]++;
m=a[++i];
System.out.println( i+j+m);
f) int a[]=new int[5];
int I;
for(i=0;i<<5;i++)
a[i]=3*i;
for(i=0;i<5;i++)
System.out.println( a[i]);
a) char ch[][]=new int[6][6];
int I,j;
for( i=0;i<6;i++)
for(j=0;j<6;j++)
{
If(i==j || i+j==5)
ch[i][j]==’*’;
else
ch[i][j]=’ ‘;
}
for( i=0;i<6;i++)
for(j=0;j<6;j++)
System.out.print( ch[i][j])
System.out.println( );
H) int i,j,c=2;
int a[]={1,1,1}, b[]={2,3,4};
for(i=0;i<=2;i++)
{
J=a[i]+b[c];
c--;
System.out.println( j*j);
}
2.)WAP to enter 11 names of the cricketers & their countries in two arrays . Then Enter the
names of a country of your choice and print all those names of cricketers Who belong to that
country. Repeat the process according to the users choice.
3) WAP to open two arrays and store 10 numbers in each. Store their sum in array S And product
in another array P. Print array S and P.
4) WAP to enter any 20 numbers in an array. Print the largest and the smallest numbers Along
with their locations.
5) Write a program to create a single dimension array to input 10 integers. Print array elements in
a single line. Alsoe print sum of those elements which are divisible by 4.
6) A company produces the different products A1 and A2 …………….. A10 with P1, P2
………………..P10 as per prices per unit and Q1,Q2,…………….Q10 as the the quantities
Produced respectively.
(i) Store the data in 10 * 20 matrix as follows :
COST/UNIT(RS) QUANTITY
P1 Q1
P2 Q2
: :
: :
P10 Q10
7.) Wap to accept 10 integer from a user in a single dimension array and sort the array using
bubble sort.
8) Wap to accept 10 integer from a user in a single dimension array and sort the array using
Selection sort.
12) Design a function void showtax(double tax[]). The function takes single subscripted variable
tax[] as function argument with income tax amount of 8 salarid persons. Print average tax of all
the persons along with maximum and minimum tax amount paid by the persons,
13) Write a program to initialize the following integer arrays. Print a suitable message whether
the two array are identical or not. Make use of Boolean data type to decide whether the two
arrays are same or not.
Ar[]={146,236,346,456} and Br={146,236,346,456}
15) Wap to display frequency table from an array marks[] which hold marks of 100 students
ranges are as given 0-10,11-20,21-30…..91-100
16.) Wap to input date in dd,mm,yy and display corresponding date of the year. Eg 21-7-2004
process:- 31+29+31+30+31+30+21=203
output 203
17) Write a program to input a single dimension array ar[] of 10 integer elements store only
single digit integers in another array br[]. Print array ar[] and br[] in two different lines.
18) Write a program to inititalize the given temperatures in an array. Find the minimum and
maximum temperatures along with the average of all the temperatures.
Temperatures: 23.4, 44.5,12.5,28.7,34.5,6.7
Output: Minimum Temperature:- 6.7
Maximum Temperature: 44.5
Average Temperature:- XXX
19) Define a function void findmarks(float m[]). The function takes single subscripted variable
m[] as function argument with marks of 10 students.Print average of 10 students along with
highest and lowest marks scored by the student with suitable message.
20) Wap to reverse the array without using another array.
22) wap to store numebr in a single dimension array and store them even number in array e[] and
odd numbers in array o[].
23) Wap to input decimal number and convert it into binary equivalent.
24) Write a program to input numbers in two array A[] and array B[] of size m and n. create
another array C[] that contains common elements of both array A[] and B[].
25) WAP to store 20 numbers in a 1D array and count the sum of all numbers and the sum of all
evrn numbers of two digits, which are exactly divisible by 7.
26) WAP to store 10 alphabets in a 1D array X and copy all small letters in array Y and capital
letters in array Z.
27) WAP to store numbers in array A and sort it in ascending or descending order according to
user choice.
28) WAP to store n small alphabets in an array. If the user enters it in capital then store them in
the array after converting them in small. And sort them in descending order using selection sort.
29) WAP to store n numbers in an array and copy all prime numbers in array PRIME and perfect
numbers in array PERFECT.
30) Write a program to read a set of N values from a user and store it in a one dimensional array.
Compute and display AM standard Deviation(SD) and Variance(var) for the N values. N is a
user input.
31) Write a program to read a set of N arbitrary integer values from a user and store it in a single
dimensional array. Check whether the number is prime or composite while displaying the array
of numbers. N is a user defined constant.
32) Write a program to input n numbers in an array and sort them using selection sort technique
in descending order. Input a number n and search for the number in the array using binary search
technique.
33) Write a program to input n number in an array display frequency for each number. As well as
display mode. Mode is the number which occurs most frequently in the array.
34)Write a program to input name and marks of 3 subjects of 50 students. Perform the given
process.
Display name and grade of students in tabular form. Where grade is assigned as per given rule
For percentage >=75 grade ‘A’, for percentage between 60 to 75 grade ‘B’, for percentage
between 40 to 59 grade ‘C’, for percentage below 40 grade ‘D’.
Display the name of the student who scored highest percentage
35) 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 listed”.
36. WAP to input marks out of 100 of 100 students. Now form four arrays first[ ], second[ ],
third[ ] and fourth[ ]. Now transfer the marks in given arrays depending upon following
condition.
MARKS ARRAY
Above 59 First
Between 45 and 59 Second
Between 33 and 44 Third
array
Tick the correct answer
Question 1
1. int a[-40]
2. int a[40]
3. float a[0 - 40]
4. None
Question 2
1. 10th
2. 9th
3. 11th
4. None
Question 3
1. packets
2. blocks
3. subscripts
4. compartments
Question 4
1. subscripted variable
2. actual variable
3. compound variable
4. none
Question 5
An array element can be accessed through:
1. dots
2. element name
3. index number
4. none
Question 6
Indicate the error message which displays, if the following statement is executed :
int a[5] = {28,32,45,68,12};
1. Insufficient cells
2. Array index out of bounce
3. Elements exceeding cells
4. None
Question 7
1. assigns 37 to code[1]
2. assigns 25 to code[1]
3. assigns 38 to code[3]
4. assigns 42 to code[0]
Question 8
1. from 1 to 50
2. from 0 to 49
3. from 1 to 51
4. none
Question 9
1. m.size of (a)
2. m.elements of (m)
3. m.length
4. None
Question 10
A Single Dimensional array contains N elements. What will be the last subscript?
1. N-1
2. N
3. N+1
4. None
Question 1
Output
4 6
Explanation
Question 2
Output
Sum = 27
Explanation
a[0]=23 assigns 23 to the first element of the array. a[3]=a[1] assigns the value of second
element of the array which is 4 to the fourth element of the array. After the execution of these
two statements array looks like this:
{23, 4, 6, 4, 10}
a[0]+a[1] ⇒ 23 + 4 ⇒ 27
Question 3
12
Explanation
a[2+1] ⇒ a[3] ⇒ 12
Question 4
int a[4]={2,4,6,8};
for(i=0;i<=1;i++)
{
s=a[i]+a[3-i];
System.out.println(s);
}
Output
10
10
Explanation
i Output Remark
a[0] + a[3]
0 ⇒2+8 First Iteration
⇒10
a[1] + a[2]
1 ⇒4+6 Second Iteration
⇒10
Question 1
Question 2
Question 3
Variables are useful for keeping track of a single piece of information but as we collect
more and more information, keeping the variables organized can be complicated. In such
situations, we need arrays to solve the problems in a much better and efficient way.
Question 4
The data type that represents a number of similar or different data under single
declaration is called as composite data type. An array is a group or a collection of same
type of variables. Hence, Array is a composite data type.
Question 5
A Single Dimensional Array contains one row and one or more columns. The syntax of
declaring a Single Dimensional Array is:
<type> <array-variable>[] = new <type>[<size>];
OR
<type> [] <array-variable> = new <type>[<size>];
Double Dimensional Array contains multiple rows and multiple columns. The syntax of
declaring a Double Dimensional Array is:
<type> <array-variable>[][] = new <type>[<rows>][<columns>];
OR
<type> [][] <array-variable> = new <type>[<rows>][<columns>];