w3resource

C#: Insert New value in the array (unsorted list )

C# Sharp Array: Exercise-14 with Solution

Write a C# Sharp program to insert another value in the array (unsorted list).

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise14  
{  
    public static void Main() 
    {
        int[] arr1 = new int[10]; // Declare an array to store integers
        int i, n, p, x; // Declare variables for array size, counting, position, and new value

        // Display a message prompting the user to input the size of the array
        Console.Write("\n\nInsert New value in the unsorted array : \n");
        Console.Write("-----------------------------------------\n");   
        Console.Write("Input the size of array : ");
        n = Convert.ToInt32(Console.ReadLine()); // Read the size of the array entered by the user

        /* Store values into the array */
        Console.Write("Input {0} elements in the array in ascending order:\n", n);
        for (i = 0; i < n; i++)
        {
            Console.Write("element - {0} : ", i);
            arr1[i] = Convert.ToInt32(Console.ReadLine()); // Store user input in the array
        }

        Console.Write("Input the value to be inserted : ");
        x = Convert.ToInt32(Console.ReadLine()); // Read the value to be inserted
        Console.Write("Input the Position, where the value to be inserted :");
        p = Convert.ToInt32(Console.ReadLine()); // Read the position where the value will be inserted

        Console.Write("The current list of the array :\n");
        for (i = 0; i < n; i++)
            Console.Write("{0} ", arr1[i]); // Display the existing array

        /* Move all data at the right side of the array */
        for (i = n; i >= p; i--)
            arr1[i] = arr1[i - 1]; // Shift elements to make space for the new value

        /* Insert the value at the given position */
        arr1[p - 1] = x;

        Console.Write("\n\nAfter Insert the element, the new list is :\n");
        for (i = 0; i <= n; i++)
            Console.Write("{0} ", arr1[i]); // Display the updated array with the inserted value

        Console.Write("\n\n");
    }
}

Sample Output:

Insert New value in the unsorted array :                                                                      
-----------------------------------------                                                                     
Input the size of array : 2                                                                                   
Input 2 elements in the array in ascending order:                                                             
element - 0 : 5                                                                                               
element - 1 : 15                                                                                              
Input the value to be inserted : 10                                                                           
Input the Position, where the value to be inserted :2                                                         
The current list of the array :                                                                               
5 15
After Insert the element the new list is :                                                                    
5 10 15

Flowchart:

Flowchart: Insert New value in the array (unsorted list )

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to insert New value in the array (sorted list ).
Next: Write a program in C# Sharp to delete an element at desired position from an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.