Q:

C# program for swapping of two arrays

belongs to collection: C# Basic Programs | array programs

0

C# program for swapping of two arrays

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to swap of two arrays is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare to manage index of arrays
            int index = 0;

            //Temporary variable two swap arrays
            int temp = 0;

            //Declare array that contains 5 integer elements
            int[] arr1 = new int[5];
            int[] arr2 = new int[5];

            //Now read values for first array elements.
            Console.WriteLine("Enter value of array1 elements...");
            for (index = 0; index < arr1.Length; index++)
            {
                Console.Write("Element arr1[" + (index + 1) + "]: ");
                arr1[index] = int.Parse(Console.ReadLine());
            }

            //Now read values for second array elements.
            Console.WriteLine("Enter value of array2 elements...");
            for (index = 0; index < arr2.Length; index++)
            {
                Console.Write("Element arr2[" + (index + 1) + "]: ");
                arr2[index] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("\n\nBefore Swapping...");
            Console.WriteLine("Array 1 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr1[index] + " ");
            }
            Console.WriteLine("\nArray 2 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr2[index] + " ");
            }

            Console.WriteLine();
            Console.WriteLine();

            //Now we swap two arrays
            for (index = 0; index < 5; index++)
            {
                temp        = arr1[index];
                arr1[index] = arr2[index];
                arr2[index] = temp;

            }
            
            Console.WriteLine("\n\nAfter Swapping...");
            Console.WriteLine("Array 1 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr1[index]+" ");
            }
            Console.WriteLine("\nArray 2 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr2[index] + " ");
            }
            
            Console.WriteLine();
        }
    }
}

Output:

Enter value of array1 elements...
Element arr1[1]: 10
Element arr1[2]: 20
Element arr1[3]: 30
Element arr1[4]: 40
Element arr1[5]: 50
Enter value of array2 elements...
Element arr2[1]: 11
Element arr2[2]: 12
Element arr2[3]: 13
Element arr2[4]: 14
Element arr2[5]: 15


Before Swapping...
Array 1 Elements

10 20 30 40 50
Array 2 Elements

11 12 13 14 15     



After Swapping...
Array 1 Elements   

11 12 13 14 15
Array 2 Elements

10 20 30 40 50              

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to insert an item into a sorted array... >>
<< C# program to interchange the columns of the matri...