You are on page 1of 7

HIRANANDANI FOUNDATION SCHOOL, THANE

PROGRAMS – Std X – ARRAYS

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

/** Program 1: Write a program to accept 10 numbers in an array. Print


the number(count) of even and odd numbers (separately). */
public class OddEvenInArray{
public static void main(String a ){
System.out.println("Odd Numbers:");
for(int i=0;i<a.length;i++){
if(a[i]%2!=0){
System.out.println(a[i]); } }
System.out.println("Even Numbers:");
for(int i=0;i<a.length;i++){
if(a[i]%2==0){
System.out.println(a[i]); } } }}

1
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS

/** Program 2: Write a program to accept 10 numbers in an array. Print


the number of positive and negative numbers (separately) in the array. */
import java.util.Scanner;
public class JavaProgram
{ public static void main(String args[])
{ int countp=0, countn=0, countz=0, i;
int arr[] = new int[10];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 Numbers : ");
for(i=0; i<10; i++)
{ arr[i] = scan.nextInt(); }
for(i=0; i<10; i++)
{ if(arr[i] < 0)
{ countn++; }
else if(arr[i] == 0)
{ countz++; }
else
{ countp++; } }
System.out.print(countp + " Positive Numbers");
System.out.print("\n" + countn + " Negative Numbers");
System.out.print("\n" + countz + " Zero"); } }

/** 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]+" "); } } } }

/** Program 4: To find total marks of 5 students using arrays */


import java.util.*;
public class Arr5
{ public static void main()
{ String str[] =new String[5]; double eng[]= new double [5];
double comp[]= new double[5]; double tot[]= new double [5];
Scanner buff=new Scanner(System.in);
for(int i=0;i<5;i++)
{ System.out.println("Enter the Name :");
str[i]= buff.nextLine();
System.out.println("Enter English marks :");
eng[i]= buff.nextDouble();
System.out.println("Enter Computer marks :");
comp[i]= buff.nextDouble();
buff.nextLine();
tot[i]= eng[i]+comp[i];}
System.out.println("\u000C");// to clear the screen
System.out.println("Name\tEng\tComp\tTotal");
for(int i=0;i<5;i++)
{ System.out.println(str[i]+"\t"+eng[i]+"\t"+comp[i]+"\t"+tot[i]); } }
}

/** Program 5: To sort string array using exchange Selection sort */


public class Stringsort
{ public void sorts()
{ int min,j,i; String t;
String arr[]={"aa","gg","ff","hh","bb"};
for(i=0;i<5;i++)
{ min=i;
for(j=i+1;j<5;j++)
{ if(arr[j].compareTo(arr[min])<0)
min=j; }
t=arr[i]; arr[i]=arr[min]; arr[min]=t; }
System.out.println("The names in ascending order are");
for(i=0;i<5;i++)
{ System.out.println(arr[i]); } } }

/**Program 6: To sort string array using exchange bubble sort */


public class stringbubble
{ public void sorts()
{ int j,i;String t="";

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

Two Dimensional Arrays:


Double dimensional arrays are used to store data in tabular format.
Syntax :
datatype arrayname[][]=new datatype[rows][cols];
Eg :
int arr[][]=new int[3][3];
Initializing an array:
int arr[][]= {{1,2,3},{4,5,6}};
(the above statement will initialize an array with 2 rows and 3 columns)

Program 1: To accept numbers in a 3x3 matrix and print the same.


import java.util.*;
public class Arr2D2
{ 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(); } }}

Practice:
2) Accept values in a 3x3 character array and print them.
3) Accept values in a 3x3 String array and print them.

Program 4: To accept numbers in a 3x3 matrix and print their sum.


import java.util.*;
public class Arr2D3
{ public static void main()

4
HIRANANDANI FOUNDATION SCHOOL, THANE
PROGRAMS – Std X – ARRAYS

{Scanner sc=new Scanner (System.in);


int a[][]=new int[3][3];int s=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();
s=s+a[i][j]; } }
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("Sum = " + s); }}

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 6: To accept numbers in a 3x3 matrix and print the transpose of


the array.(change of rows into columns and vice versa)
import java.util.*;
public class Arr2D5
{ 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(); } System.out.println("Transpose of
matrix");
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[j][i]+" "); }
System.out.println(); }}

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

You might also like