0% found this document useful (0 votes)
28 views24 pages

Array Notes

The document provides a comprehensive overview of arrays in programming, detailing their types (primitive and non-primitive), properties, and various operations such as creation, accessing elements, and searching techniques. It includes example programs for single and double-dimensional arrays, demonstrating input, output, sorting, and searching methods like linear and binary search. Additionally, it covers the use of arrays to store and manipulate data in a structured format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views24 pages

Array Notes

The document provides a comprehensive overview of arrays in programming, detailing their types (primitive and non-primitive), properties, and various operations such as creation, accessing elements, and searching techniques. It includes example programs for single and double-dimensional arrays, demonstrating input, output, sorting, and searching methods like linear and binary search. Additionally, it covers the use of arrays to store and manipulate data in a structured format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ARRAY

- 2 types of data: Primitive and Non Primitive


- Primitive: int, char, float, double
- Non Primitive/Composite: Array (derived from primitive)

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

- Why need arrays?


For ex:
I want 10 numbers from a user and display those 10 numbers.

for(int i=1;i<=10;i++)
{
n=sc.nextInt();
}
for(int i=1;i<=10;i++)
{
S.o.pln(n);
}

What is the output??


Ans: Only the last number will be printed.

Arrays:

Here, a[0], a[1], a[2].. are known as subscripts/Index


numbers.

- 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

- Creating arrays: (Because it is non primitive, so we have to


create it)
int a[]=new int[10];
char c[]=new char[35];
double d[]=new double[20];
String s[]=new String[10];

- Storing and accessing array elements:


1) Using direct assignment:
int a[]={1,56,3,4,70};

2) Using parameterised input:


psvm(int a[])

3) Using scanner class:


int a[]=new int[10];
a[2]=5;
a[5]=a[2]*2;

- Note: The subscript/index number always starts from 0.

- Program: WAP to accept 10 numbers and then print them.

Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 numbers:");

int a[]=new int[10];

for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}

System.out.println("Array Elements are:");


for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}

- Program: Accept 10 numbers in Single Dimensional array.


Display the array elements at even subscript.

Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 numbers:");

int a[]=new int[10];


for(int i=0;i<10;i++) //i are the index numbers
{
a[i]=sc.nextInt();
}
for(int i=0;i<10;i=i+2)
{
System.out.println(a[i]);
}
}
}

- Program: Accept 10 numbers in a single dimensional array.


Display the greatest number of the array.
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 10 numbers:");

int a[]=new int[10];

for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}

int max=a[0];//lets store the first value in max


for(int i=1;i<10;i++)// i=1 because max already has 0th value
{
if(a[i]>max)
{
max=a[i];
}
}
System.out.println(max);
}
}

- Program: Accept 10 numbers in SDA. Create another array to


store the squares of this array. Display both the arrays in the
given format:
Array 1 Array 2
4 16
3 9
. .
. .
5 25
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

int a[]=new int[10]; //to store 10 numbers


int b[]=new int[10]; //to store their squares

System.out.println("Enter 10 numbers in Array a:");


for(int i=0;i<10;i++)
{
a[i]=sc.nextInt(); //to input the numbers
b[i]=a[i]*a[i]; //to find their squares
}

System.out.println("Array 1 \t Array 2");

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

Linear Search Binary Search

1) It works on sorted and 1) It works only on sorted


unsorted, both types of array elements.
array elements.

2) Search begins at the start 2) An array is divided into halves


of an array. and then the desired data item
is searched either in the first half
or second half.
- Program: Input 10 numbers in SDA.Now enter a number and
search whether it is present in the given array or not using
linear search technique.
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{
int n,flag=0;
Scanner sc = new Scanner(System.in);

int a[]=new int[10];

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

- Program: Input 10 numbers in SDA.Now enter a number and


search whether it is present in the given array or not using
binary search technique.
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{
int mid,n,flag=0;
Scanner sc = new Scanner(System.in);

int a[]=new int[10];

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

int l=0,u=9;//l is lower limit, u is upper limit

while(l<=u)
{
mid=(l+u)/2;

if(a[mid]==n)//if the middle number is same as n


{
flag=1;
break;
}
if(n>a[mid])//if n is bigger than middle number then shift l
{
l=mid+1;
}
if(n<a[mid])//if n is smaller than middle number then shift u
{
u=mid-1;
}

}
if(flag==1)
{
System.out.println("Search Successful");
}
else
{
System.out.println("Search Unsuccessful");
}
}
}

—----------------------------------------------------------------------------------
Sorting Techniques:
1) Selection Sort
2) Bubble Sort

- Program to arrange 10 numbers in ascending order using


selection sort.
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);


int i,j,min,temp;

int a[]=new int[10];

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

for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
}

for(i=0;i<10;i++)
{
min=i;

for(j=i+1;j<10;j++) // for the next elements in the array.


{
if(a[j]<a[min])
{
min=j;
}
}

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

- Program to arrange 10 numbers in ascending order using


bubble sort.
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);


int i,j,temp;

int a[]=new int[10];

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.

Array Type Data Type Format


Integer array int int m[][]=new int[3][4];

Character array char char m[][]=new char[3][4];

String array String String m[][]=new String[3][4];

Floating point float float m[][]=new float[3][4];


array

- Storing and accessing array elements:


1) Using direct assignment:
int a[][]={{2,4,6},{5,6,7},{10,2,3}};
char c[][]={{‘a’, ‘c’},{‘d’, ‘e’},{‘r’, ‘t’}};
String s[][]={{“&”, “*”},{“%”, “#”}};

2) Using Scanner object:


Scanner sc=new Scanner(System.in);
int a[][]=new int[3][5];
for(int i=0;i<3;i++)//this is for rows
{
for(j=0;j<5;j++)//this is for columns
{
a[i][j]=sc.nextInt();
}
}

- Program: Input integers in DDA of size 3x4. Display the


following:
i) Array in matrix format
ii) sum of all the elements
Ans: import java.util.*;

public class PrintNumbers


{
public static void main(String args[])
{

Scanner sc = new Scanner(System.in);

int a[][]=new int[3][4];


int i,j,sum=0;//i=rows,j=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];
}
}

for(i=0;i<3;i++) //this for is to print the elements in matrix form


{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}

System.out.println("Sum of all the elements: "+sum);

}
}

- Program: Input integers in DDA of size 4*3(4 rows and 3


columns).
Display the following:
a) Sum of each row
b) Sum of each column

Ans: import java.util.*;

public class matrix


{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int a[][]=new int[4][3];

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

- Program: WAP to input elements in 4X4 DDA. Calculate and


display
a) Sum of all elements in the left diagonal
b) Sum of all elements in the right diagonal
Ans: consider this example:
Index number of left diagonal: 00,11,22,33 - Means here, the row
index no. and column index no. are
same.
Index number of right diagonal: 03,12,21,30 - Means here, the
addition of row index no. and
column index no is 3.
Here, 3 is size of matrix-1; 4-1=3

Ans: import java.util.*;

public class matrix


{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int a[][]=new int[4][4];

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 calculating the sum of left and right diagonals

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.*;

public class matrix


{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

int a[][]=new int[4][4];

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 calculating the sum of left and right diagonals

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

Ans: import java.util.*;


public class Capital
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,a=0,flag=0;
String st;
String m[]=new String[10];
String n[]=new String[10];

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

System.out.print("Enter the states whose capital to be


searched");
st=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");
}
}
}
****************************************************************************

You might also like