Icse X Arrays
Icse X Arrays
Features of an Array
1. Arrays are objects.
2. They can even hold the reference variables of other objects.
3. Arrays are created during runtime.
4. They are dynamic, created on the heap.
5. The length of an array is fixed.
This syntax indicates that it can store 10 values in the memory with different
subscripts. Let’s see the structure of a single dimentional array for the above
syntax.
Array A[]
Superscripts A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Data items 12 22 33 56 90 100 23 67 29 87
In this figure the values inside the bracket [] are actually referred as subscripts
and they help in both input and output of the array elements. Here A is
subscripted variable.
We can create array of different data types as shown below:
int s[]=new int[10] //an integer array
float f[]=new float[10] //to store fractional number
char c[]=new char[10] //to store character
String s[]=new String[10] //to store string values
How to initialize an array
We can initialize an array by providing default values but while initializing it
keep in mind that you don’t need to supply the size of an array while initializing.
Let’s tale an example:
LINEAR SEARCH
Linear Search means sequential search. It is very simple and easy to implement. The
searching begins with the starting position of an array i.e., 0th index and continues to
search the element one after another till the element has been found. Once the
element has been found, it is then checked and compared with a given data item.
Linear Search is very slow process.
Let’s see how it works:
Let’s assume that we are having an array of 10 integer value and we need to check
whether a number 30 is present in it or not.
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)
Array A[]= 12 33 30 41 4 8 23 11 89 6
Item to search=30 b=30
Implementation : A Simple Linear Search Program
class linear
{
public static void main(String args[])
{
int A[]={12,33,30,41,4,8,23,11,89,6};
int b=30, f=0;
for(int i=0;i<10;i++)
{
if(A[i]==b)
{
f=1;
break;
}
}
if(f==1)
System.out.println("Number found successfully");
else
System.out.println("Number not found");
}
}
Now let us take another example of linear search by taking all the inputs from the
user and here we will also find the position number of the elements.
import java.util.*;
class positionnumber
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A[]=new int[10];
int b, flag=0, pos=1;
Sumit Sir Chapter-3 ICSE-X
Arrays (Single Dimensional & Double Dimensional)
System.out.println("Enter 10 numbers in an array:");
for(int i=0;i<10;i++)
{
A[i]=in.nextInt();
}
System.out.println("Enter a number to be search:");
b=in.nextInt();
for(int j=0;j<10;j++)
{
if(A[j]==b)
{
flag=1;
pos=pos+j;
break;
}
}
if(flag==1)
System.out.println("Number found successfully at position:" +pos);
else
System.out.println("Number not found:");
}
}
Scenario 1: Suppose you have 10 elements in an array and you have to check
whether a particular element is present within the array or not. So, here we can
simply use the linear search because searching 10 elements is very much easier and it
will also not affect the performance of the system.
Scenario 2: Now let us assume that you have to search an element between 1 to 50
thousand and suppose the number you are searching is available at last index of the
array and now think of linear search that how much time it will take to move the
position from 0th index to last index.
Of course it will take a huge amount of time. So the developer has worked on these
and brought a concept of binary search.
Now compare the value 20 with mid i.e., 20>=30 as the condition is false the search
will take place in first half because the array has been sorted in ascending order now
the mid became the last index of the searching array.
Now compare the value 20 with mid i.e., 20>=20 and hence condition is true and the
element has been found.