You are on page 1of 8

PRESIDENCY UNIVERSITY

Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering

Subject: CSE2001 - Data Structures & Algorithms Semester: III


Lab Session 2: Date: 06/09/2023

1. Java code to read and print array elements:(Array Traversal, Insert at


beginning, Insert at given position, Insert at end)
import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
System.out.println(“Enter the size of array”);
n=sc.nextInt();
System.out.println(“Enter the array elements”);
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println(“The array elements are:”);
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
Output:

Note: Memory allocation after reading array elements will be as follows:


a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

1 2 3 4 5 0 0 0 0 0

➔ The unused array elements are filled with value 0

2. Inserting element in an array for given position:


Assume value 60 should be inserted at position 3(index value =2) as shown in below table

10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

pos=3, value=60 (current value of i=4 after adding 5 elements in array)


Add the below code snippet in previous java code:
int pos, value;
System.out.println("Enter the position to insert new element");
pos=sc.nextInt();
System.out.println("Enter the value");
value=sc.nextInt();
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
System.out.println(a[i+1]);
}
a[pos-1]=value;
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

After adding this code in main program the resultant final code for array traversal and
inserting element at given position is:
import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
int pos, value;
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the position to insert new element");

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
pos=sc.nextInt();
System.out.println("Enter the value");
value=sc.nextInt();
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

}
}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4
Logic:
1. Push existing element at position 5 (index value=4) to position 6 (index value=5)

10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

2. Push existing element at position 4 (index value=3) to position 5 (index value=4)

10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

3. Push existing element at position 3 (index value=2) to position 4 (index value=3)

10 20 30 40 50
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

3. Write a Java code to insert element at beginning in array:


Note: Just change one condition in the above final code to insert element at beginning

for(i=n-1;i>=0;i--)

a[i+1]=a[i];

The final code is:

import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
int pos, value;
System.out.println("Enter the size of array");
n=sc.nextInt();
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 5
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the position to insert new element");
pos=sc.nextInt();
System.out.println("Enter the value");
value=sc.nextInt();
for(i=n-1;i>=0;i--) //Only the highlighted condition is changed
{
a[i+1]=a[i];
}
a[pos-1]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

}
}

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 6
Output:

4. Write a Java code to insert element at the end in an array:


import java.util.*;
public class read_array
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n, i; //variable n is used to store size of array, variable i is used indexing
int a[]=new int[10]; // array declaration in java language
int value;
System.out.println("Enter the size of array");
n=sc.nextInt();
System.out.println("Enter the array elements");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 7
}
System.out.println("The array elements are:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("Enter the value");
value=sc.nextInt();
a[i]=value;
n++;
System.out.println("The array elements are inserting new element is:");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

}
}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 8

You might also like