Array Notes
Array Notes
What is an array?
- It's a data structure created in memory to store a large amount
of data.
Properties:
1) It has a fixed size.
2) The values are of similar type (either int, double, char or string).
3) The data can be accessed with the help of index number.
4) Index number always starts from 0.
5) If the size of array is n, then the index number starts from
0 to n-1.
6) The values are always stored in a linear fashion(means one
after other).
for(int i=1;i<=10;i++)
{
n=sc.nextInt();
}
for(int i=1;i<=10;i++)
{
S.o.pln(n);
}
Arrays:
- 2 types of arrays:
1) Single dimensional array :
a[], here a is single subscripted variable
2) Double dimensional array:
a[][], here a is double subscripted variable
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int i=0;i<10;i++)
{
System.out.println(a[i]+"\t"+b[i]);
}
}
}
—----------------------------------------------------------------------------------
Searching techniques:
1) Linear Search/sequential search
2) Binary Search
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter a number to search");
n=sc.nextInt();
for(int i=0;i<10;i++)
{
if(a[i]==n)
{
flag=1; //flag is an indicator. It will be on and off
break;
}
}
if(flag==1)
{
System.out.println("Search Successful");
}
else
{
System.out.println("Search Unsuccessful");
}
}
}
System.out.println("Enter 10 numbers");
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter a number to search");
n=sc.nextInt();
while(l<=u)
{
mid=(l+u)/2;
}
if(flag==1)
{
System.out.println("Search Successful");
}
else
{
System.out.println("Search Unsuccessful");
}
}
}
—----------------------------------------------------------------------------------
Sorting Techniques:
1) Selection Sort
2) Bubble Sort
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
min=i;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("Sorted Array:");
for(i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(i=0;i<10;i++)
{
for(j=0;j<(9-i);j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted Array:");
for(i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}
—----------------------------------------------------------------------------------
Double dimensional array:
- It is used to store the data in the form of rows and columns.
- m[3][4], here 3 are rows and 4 are columns.
System.out.println("Enter elements:");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
sum=sum+a[i][j];
}
}
}
}
int i,j,sumr,sumc;
System.out.println("Enter elements:");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
//row-wise sum
for(i=0;i<4;i++)
{
sumr=0;//making sumr as 0 so that in every loop previous
value of sumr should not be repeated.
for(j=0;j<3;j++)
{
sumr=sumr+a[i][j];//in every iteration value of i will remain
same, j will change
}
System.out.println(sumr);
}
//column-wise sum
for(j=0;j<3;j++)
{
sumc=0;
for(i=0;i<4;i++)
{
sumc=sumc+a[i][j];//in every iteration value of j will remain
same, i will change
}
System.out.println(sumc);
}
}
}
int i,j;
int sumleft=0,sumright=0;
System.out.println("Enter array elements:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j) //00,11,22,33
{
sumleft=sumleft+a[i][j];
}
else if(i+j==3) //03,12,21,30
{
sumright=sumright+a[i][j];
}
else
{
System.out.println(" ");
}
}
}
System.out.println("Sum of left diagonal elements: "+sumleft);
System.out.println("Sum of right diagonal elements:"+sumright);
}
}
OR
import java.util.*;
int i,j;
int sumleft=0,sumright=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<4;i++)
{
sumleft=sumleft+a[i][i]; //00,11,22,33
sumright=sumright+a[i][3-i]; //03,12,21,30
}
System.out.println("Sum of left diagonal elements: "+sumleft);
System.out.println("Sum of right diagonal elements:
"+sumright);
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Program: Write a program to accept 10 states and 10 capitals of a
country in two different single dimensional arrays.
Now, enter a state of the country to display its capital.
If it is present then display its capital otherwise, display a relevant
message.
Sample input: enter the state and the capital
Bihar
Patna
West Bengal
Kolkata and so on------------
Sample Output: enter the state whose capital is to be searched:
West Bengal
The capital is Kolkata
for(i=0;i<10;i++)
{
System.out.print("Enter states in the cell :");
m[i]=in.nextLine();
System.out.print("Enter capital in the cell :");
n[i]=in.nextLine();
}
for(i=0;i<10;i++)
{
if(m[i].equals(st))
{
flag=1;
a=i;
}
}
if(flag==1)
{
System.out.println("The capital is "+n[a]);
}
else
{
System.out.println("The state"+st+ "is not found at any location");
}
}
}
****************************************************************************