Programmingempire
The following code example demonstrates Rotating an Array in C#. In fact, array rotation is a programming exercise that helps you improve programming logic. Moreover, it also requires students to develop critical thinking and to find think about logic to solve a problem in an efficient manner. In other words, the application logic needs to be developed in such a way that requires the least time to execute.
Apart from developing programming logic, we use array rotation in many applications. For instance, we can use it in transformation, image processing, and so on.
Problem Statement
Given an array consisting of numbers, and an integer k, such that 0<=k<=n, where n is the total number of elements in the array. The output should be an array of the same size as the input array with its elements shifted k times to the right end of the array and the last k elements should be placed in the first k positions from the left side.
For example, let array = [7, 9, 1, 2, 8, 5, 3, 12, 6, 19] and k=1, then the output array = [19, 7, 9, 1, 2, 8, 5, 3, 12, 6]
Another example: let our array is the same as given in the previous example, but now k=3. The output array = [12, 6, 19, 7, 9, 1, 2, 8, 5, 3]
Program Code for Rotating an Array in C#
At first, we initialize the array. However, we can also take input from the user. Also, we take the number of rotations as input. After that, the outer for loop iterates for the number of rotations times. In each iteration, we can shift the elements of the array to the next position. Besides, we need to copy the last element of the array at the index 0. Hence, it will complete one rotation. The same process is repeated for the required number of rotations as specified in the outer for loop.
using System;
namespace RotateArray
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 2, 1, 7, 9, 12, 5, 18, 20 };
Console.WriteLine("Original Array...");
foreach (int i in arr)
Console.Write(i + " ");
Console.WriteLine("\nLength of Array: "+arr.Length);
Console.WriteLine("Enter no. of steps to rotate (Must be <=" + arr.Length + ")");
int s = Int32.Parse(Console.ReadLine());
if(s>arr.Length)
{
Console.WriteLine("Wrong Value!");
Environment.Exit(0);
}
for(int i=0;i<s;i++)
{
int t = arr[arr.Length - 1];
for(int j=arr.Length-2;j>=0;j--)
{
arr[j + 1] = arr[j];
}
arr[0] = t;
}
Console.WriteLine("Rotated Array...");
foreach (int i in arr)
Console.Write(i + " ");
}
}
}
Further Reading
How to Create Instance Variables and Class Variables in Python
Comparing Rows of Two Tables with ADO.NET
Example of Label and Textbox Control in ASP.NET
One Dimensional and Two Dimensuonal Indexers in C#
Private and Static Constructors in C#
Programs to Find Armstrong Numbers in C#
One Dimensional and Two Dimensional Indexers in C#
Generic IList Interface and its Implementation in C#
Creating Navigation Window Application Using WPF in C#
Find Intersection Using Arrays
An array of Objects and Object Initializer
Performing Set Operations in LINQ
Data Binding Using BulletedList Control
Understanding the Quantifiers in LINQ
Deferred Query Execution and Immediate Query Execution in LINQ
Examples of Query Operations using LINQ in C#
An array of Objects and Object Initializer
Language-Integrated Query (LINQ) in C#
Examples of Connected and Disconnected Approach in ADO.NET
IEnumerable and IEnumerator Interfaces
KeyValuePair and its Applications
Learning All Class Members in C#
Examples of Extension Methods in C#
How to Setup a Connection with SQL Server Database in Visual Studio
Understanding the Concept of Nested Classes in C#
A Beginner’s Tutorial on WPF in C#
Explaining C# Records with Examples
Everything about Tuples in C# and When to Use?
Linear Search and Binary search in C#
Examples of Static Constructors in C#