0% found this document useful (0 votes)
4 views12 pages

Array

The document provides an overview of arrays in programming, detailing their characteristics such as being index-based and containing elements of the same datatype. It covers one-dimensional and two-dimensional arrays, including their initialization, creation, and examples of operations like displaying values, transposing matrices, and performing calculations like sum and average. Additionally, it includes homework suggestions related to array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views12 pages

Array

The document provides an overview of arrays in programming, detailing their characteristics such as being index-based and containing elements of the same datatype. It covers one-dimensional and two-dimensional arrays, including their initialization, creation, and examples of operations like displaying values, transposing matrices, and performing calculations like sum and average. Additionally, it includes homework suggestions related to array manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Array

-----------
--->Array is a collection of same datatype.
--->array is Indexbased.
---->Index value starts from 0 to n-1
--->array length is starts from 1 to n.

int a=100;

int marks[]={100,89,99,78,67};

types:
----------
1)One Dimensional Array(1-D Array)
2)Two Dimensional Array(2-D Array)

1)One Dimensional Array(1-D Array)


-------------------------------------------------------------
----->It is a linear array
------->It display the data in row or column wise.

Initialization of an Array
--------------------------------------------
datatype var[]={value1,value2,value3,value4,...};

EX:
-----
int a[]={1,20,34,6,7,9,3};

a[0]--->1
a[1]--->20
a[2]--->34
a[3]--->6
....

EX:
------
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{

int i;
int x[]= {12,3,4,6,78,6};

//System.out.println(x);

for(i=0;i<6;i++)
{
System.out.println(x[i]);//x[0],x[1],x[2],x[3],x[4],x[5]
}
}
}
EX:
------
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{

int i;
int x[]= {12,3,4,6,78,6,89,78,22,56,899,4562};

//System.out.println(x);

for(i=0;i<x.length;i++)
{
System.out.println(x[i]);//x[0],x[1],x[2],x[3],x[4],x[5]
}
}
}
----------------------------------------------------------------------------
EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{

int i;
int x[]= {12,3,4,6,78,6,89,78,22,56,899,4562};

System.out.println(x[2]);

System.out.println(x[1]);

System.out.println(x[0]);

//System.out.println(x[100]);---->Error

}
}
-----------------------------------------------------------------------------------
creating an Array
------------------------------
syntax:
----------------
datatype var[]=new datatype[size];

EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{

Scanner sc=new Scanner(System.in);

int arr[]=new int[5];

System.out.println("Enter the Array values");

arr[0]=sc.nextInt();
arr[1]=sc.nextInt();
arr[2]=sc.nextInt();
arr[3]=sc.nextInt();
arr[4]=sc.nextInt();

System.out.println(arr[0]+" "+arr[1]+" "+arr[2]+" "+arr[3]+" "+arr[4]);

}
}

Ex:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{

int n,i;//n-->no.of.inputs,i--->loop

Scanner sc=new Scanner(System.in);

int arr[]=new int[100];//arr[0]..arr[99]

System.out.println("Enter the number of Array value");


n=sc.nextInt();

System.out.println("Enter the Array values");

for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();

System.out.println("Display the values");


for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}

}
}
----------------------------------------------------------------------------------
Two-Dimensional Array
------------------------------------------
----->It display the data's in row and columnwise.

syntax:
-----------------
datatype[][] var={}

Ex:
----
package Mypack1;

public class Myclass3


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

int arr[][]=
{
{1,2,3},
{4,5,6},
{7,8,9}
} ;

System.out.println(arr[1][1]);

System.out.println(arr[0][0]);

System.out.println(arr[0][2]);

System.out.println(arr[1][3]);//Error

}
}

/* 0 1 2
* 0 1 2 3
*
* 1 4 5 6
*
* 2 7 8 9
*
*
*
*
*/

EX:
-----
package Mypack1;
public class Myclass3
{
public static void main(String[] args)
{

int i,j;
int arr[][]=
{
{1,2,3},
{4,5,6},
{7,8,9}
} ;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}

}
}

/* 0 1 2
* 0 1 2 3
*
* 1 4 5 6
*
* 2 7 8 9
*
*
*
*
*/
-----------------------------------------------------------------------------------
---------
Creating an array
-------------------------------
syntax:
----------------
datatype[][] var=new datatype[size1][size2];

EX:
------
package Mypack1;

import java.util.Scanner;

public class Myclass3


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

int i,j,r,c;

int arr[][]=new int[10][10];//10*10=100 elements


Scanner sc=new Scanner(System.in);

System.out.println("Enter the row and column value");


r=sc.nextInt();
c=sc.nextInt();

System.out.println("Enter the Elements");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=sc.nextInt();
}
}

System.out.println("Display the values are");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+" ");
}

System.out.println();
}
}
}

/* 0 1 2
* 0 1 2 3
*
* 1 4 5 6
*
* 2 7 8 9
*
*
*
*
*/
-------------------------------------------------------------------------------
Transpose of a matrix
--------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


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

int i,j,r,c;

int arr[][]=new int[10][10];//10*10=100 elements

Scanner sc=new Scanner(System.in);

System.out.println("Enter the row and column value");


r=sc.nextInt();
c=sc.nextInt();

System.out.println("Enter the Elements");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=sc.nextInt();
}
}

System.out.println("Display the values are");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+" ");
}

System.out.println();
}

System.out.println("Transpose of a Matrix is");

for(j=0;j<c;j++)
{
for(i=0;i<r;i++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}

/* 0 1 2
* 0 1 2 3
*
* 1 4 5 6
*
* 2 7 8 9
*
*
*
*
*/

1)Adding two matrix

1D Array Programs
-------------------------------
1)sum and average of array elements
--------------------------------------------------------------
package Mypack1;

import java.util.Scanner;
public class Myclass3
{
public static void main(String[] args)
{
int n,i,sum=0;//n-->no.of.inputs,i--->loop

float avg;

Scanner sc=new Scanner(System.in);

int arr[]=new int[100];//arr[0]..arr[99]

System.out.println("Enter the number of Array value");


n=sc.nextInt();

System.out.println("Enter the Array values");

for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();

System.out.println("Display the values");


for(i=0;i<n;i++)
{
System.out.println(arr[i]);
sum=sum+arr[i];
}
System.out.println("Sum of array values "+sum);

avg=sum/n;

System.out.println("Average value is "+avg);

--------------------------------------------------------------------------------
2)Reverse of an Array
-------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{
int n,i;//n-->no.of.inputs,i--->loop

Scanner sc=new Scanner(System.in);


int arr[]=new int[100];//arr[0]..arr[99]

System.out.println("Enter the number of Array value");


n=sc.nextInt();

System.out.println("Enter the Array values");

for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();

System.out.println("Display the values");


for(i=0;i<n;i++)
{
System.out.println(arr[i]);

System.out.println("Reverse of an Array");
for(i=n-1;i>=0;i--)
{
System.out.println(arr[i]);
}
}

-----------------------------------------------------------------------------------
-----
3)Find the even and odd number of an array.
----------------------------------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{
int n,i,oddsum=0,evensum=0;//n-->no.of.inputs,i--->loop

Scanner sc=new Scanner(System.in);

int arr[]=new int[100];//arr[0]..arr[99]

System.out.println("Enter the number of Array value");


n=sc.nextInt();

System.out.println("Enter the Array values");

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

System.out.println("Display the values");


for(i=0;i<n;i++)
{
System.out.println(arr[i]);

for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
System.out.println(arr[i]+" is Even");
evensum=evensum+arr[i];
}
else
{
System.out.println(arr[i]+" is Odd");
oddsum=oddsum+arr[i];
}
}

System.out.println("Even Sum is "+evensum);


System.out.println("Odd Sum is "+oddsum);
}

Home Work
-----------------------
add two matrix
largest of an array
smallest of an array
find the cube of an array elements
------------------------------------------------------------------------

You might also like