Professional Documents
Culture Documents
Introduction:
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 stores the values in memory at the fixed size.
An array is a collection of variable of the same type that are referenced by a
common name.
They are of two type single dimension and multi dimension.
Array is a data structure (Data grouped together into a single unit for a
specific purpose is known as datastructure.) where similar types of data
are put together, stored in continuous locations and is referred to by a
single name.
Index:
Also called subscript.
The number designating its position in arrays ordering.
If there is an array of size N then index number will be from 0 to N-1.
Index/Subscript
Value/data/Element
1
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
/** Program 3: WAP to accept 10 numbers in an array. Find the even and
odd numbers and store them in 2 separate arrays. */
import java.util.Scanner;
public class ExArrayEvenOdd
{ public static void main( )
{ // initializing and creating object.
int n;
Scanner s = new Scanner(System.in); // enter number for
elements.
System.out.print("Enter no. of elements you want in array : ");
n = s.nextInt(); int a[] = new int[n]; // enter all elements.
System.out.println("Enter all the elements : ");
for (int i = 0; i < n; i++)
{ a[i] = s.nextInt(); }
System.out.print("Odd numbers in the array are : ");
for(int i = 0 ; i < n ; i++)
{ if(a[i] % 2 != 0)
{ System.out.print(a[i]+" "); } }
System.out.println("");
System.out.print("Even numbers in the array are : ");
for(int i = 0 ; i < n ; i++)
2
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
{ if(a[i] % 2 == 0)
{ System.out.print(a[i]+" "); } } } }
3
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
String arr[]={"sanjay","vijay","ajay","riya","priya"};
for(i=0;i<5;i++)
{ for(j=0;j<4;j++)
{ if(arr[j].compareTo(arr[j+1])>0)
{ t=arr[j]; arr[j]=arr[j+1]; arr[j+1]=t; } } }
System.out.println("The nos are");
for(i=0;i<5;i++)
System.out.println(arr[i]); } }
Practice:
2) Accept values in a 3x3 character array and print them.
3) Accept values in a 3x3 String array and print them.
4
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
Program 5: To accept numbers in a 3x3 matrix and print the sum of the
numbers of each row and each column.
import java.util.*;
public class Arr2D4
{ public static void main()
{Scanner sc=new Scanner (System.in);
int a[][]=new int[3][3];int srow=0;
System.out.println("Enter elements");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ a[i][j]= sc.nextInt(); } }
System.out.println("Display in matrix format");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ System.out.print(a[i][j]+" "); }
System.out.println(); }
System.out.println();
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ srow=0;
for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ srow=srow+a[i][j]; }
System.out.println("Sum of "+(i+1)+" row = "+ srow); }
for(int i=0;i<3;i++)
{ int scol=0;
for(int j=0;j<3;j++)
{ scol=scol+a[j][i]; }
System.out.println("Sum of "+(i+1)+" column = "+ scol); }}}
5
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
Program 7: To accept numbers in 2 3x3 matrices and print the sum of each
element of the array and store it in the third array.
import java.util.*;
public class Arr2D6
{ public static void main()
{Scanner sc=new Scanner (System.in);
int a[][]=new int[3][3]; int b[][]=new int[3][3]; int c[][]=new int[3][3];
System.out.println("Enter elements for first array");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ a[i][j]= sc.nextInt(); } }
System.out.println("Enter elements for second array");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ b[i][j]= sc.nextInt();
c[i][j]=a[i][j]+b[i][j]; } }
System.out.println("Display sum of elements in matrix format");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ System.out.print(c[i][j]+" "); }
System.out.println(); } }}
6
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS
Program 8: To accept numbers in a 3x3 matrix and print the sum of the
numbers of the right and left diagonal.
import java.util.*;
public class Arr2D7
{ public static void main()
{Scanner sc=new Scanner (System.in);
int a[][]=new int[3][3];
System.out.println("Enter elements");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ a[i][j]= sc.nextInt(); } }
System.out.println("Display in matrix format");
for(int i=0;i<3;i++)//for(int i=0;i<a.length;i++)
{ for(int j=0;j<3;j++)//for(int j=0;j<a[i].length;j++)
{ System.out.print(a[i][j]+" "); }
System.out.println(); }
int ld=0,rd=0; // sum of left diagonal
for(int i=0;i<3;i++)
{ ld=ld+a[i][i]; }
System.out.println("sum of the left diagonal elements :"+ld);
// sum of right diagonal
int k=2;
for(int i=0;i<3;i++)
{ rd=rd+a[i][k];
k--; }
System.out.println("sum of the right diagonal elements :"+rd); } }