DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Reverse array without using in-built function in C#.NET

By Ashok Nalam on 20 Dec 2019 | Category: C# | Tagged: array reverse C# sorting 
In this snippet we will write a program which reverses an array with out using any in-built C#.NET function. Basically it shifts all elements from an array from left side to right side.
  Discuss Add Comment   |  
Add rating Rate this resource   

Introduction

In this snippet we will write a program which reverses an array with out using any in-built C#.NET function. Basically it shifts all elements from an array from left side to right side.

Example: If input array is {1,2,3,4,5,6} , we should print {6,5,4,3,2,1}

using System;

namespace DotNetMirror
{
    class ArrayReverse
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 4, 5, 6 };

            int startelement = 0; //start index will be 0
            int endelement = array.Length - 1; //end index will be length-1

            while (startelement < endelement)
            {
                int temp = array[endelement]; //store end element value in temporary variable for swapping
                array[endelement] = array[startelement];
                array[startelement] = temp;
                startelement++;
                endelement--;
            }
            Console.WriteLine("Result: {0}", string.Join("", array));
            Console.ReadKey();
        }
    }
}

Output:

Reverse array without using in-built function in C#.NET

  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 9026  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (2)
 
1. By ZAP on 05 Dec 2022

 
2. By ZAP on 05 Dec 2022