×

VB.Net Programs

VB.Net program to insert an item into a one-dimensional array

By Nidhi Last Updated : November 15, 2024

Insert an item into an array

Here, we will create an array of integers and then read elements from the user. After that, we will insert an item in the sorted array.

Program/Source Code:

The source code to insert an item into a one-dimensional array is given below. The given program is compiled and executed successfully.

VB.Net code to insert an item into a one-dimensional array

'VB.Net program to insert an item into a sorted array.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(10) {}
        Dim n As Integer = 0
        Dim item As Integer = 0

        Console.Write("Enter size of array: ")
        n = Integer.Parse(Console.ReadLine())

        Console.WriteLine("Enter array elements: ")
        For i = 0 To n - 1 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        Console.Write("Enter Item: ")
        item = Integer.Parse(Console.ReadLine())

        For i = 0 To 5 Step 1
            If (arr(i) >= item) Then
                For j = n To i Step -1
                    arr(j + 1) = arr(j)
                Next
                arr(i) = item
                GoTo OUT
            End If
        Next
OUT:
        Console.WriteLine("Elements of array after insertion: ")
        For i = 0 To n Step 1
            Console.WriteLine("{0}", arr(i))
        Next
    End Sub

End Module

Output

Enter size of array: 5
Enter array elements:
Element[0]: 10
Element[1]: 20
Element[2]: 30
Element[3]: 40
Element[4]: 50
Enter Item: 35
Elements of array after insertion:
10
20
30
35
40
50
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(). In the Main(), we created an array arr of 10 elements and we also created more variables item, n which are initialized with 0.

Console.Write("Enter size of array: ")
n = Integer.Parse(Console.ReadLine())

Console.WriteLine("Enter array elements: ")
For i = 0 To n - 1 Step 1
    Console.Write("Element[{0}]: ", i)
    arr(i) = Integer.Parse(Console.ReadLine())
Next

Console.Write("Enter Item: ")
item = Integer.Parse(Console.ReadLine())

In the above code, we read the size of the array then input the array elements from the user.

For i = 0 To 5 Step 1
    If (arr(i) >= item) Then
        For j = n To i Step -1
            arr(j + 1) = arr(j)
        Next
        arr(i) = item
        GoTo OUT
    End If
Next
OUT:
Console.WriteLine("Elements of array after insertion: ")
For i = 0 To n Step 1
    Console.WriteLine("{0}", arr(i))
Next

In the above code, we found the location in the sorted array to insert the item, if we found the array element greater than the given item, then we perform shift operation one position ahead. After that, we insert the item into the array and then print the update array on the console screen.

VB.Net Array Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.