Write a C# Program to rotate an Array of size 10 in left and right Direction three times Create a Class of ArrayRotate with data member of integer type array of size 10, property of that member, all constructors, destructor, user define displayArray() Method, RotateLeft(int[] arr) Method, RotateRight(int[] arr) Method. Then test the functionality of Class in the Driver Class of RotateArray.
Perform Question#1 with using Arraylist Class rather than basic Array.
namespace OneDollarTask
{
class ArrayRotate
{
ArrayList array;
public ArrayList Array
{
get
{
return array;
}
set
{
array = value;
}
}
public ArrayRotate(ArrayList array)
{
this.array = array;
}
~ArrayRotate()
{
array = null;
}
public void DisplayArray()
{
Console.Write("Your array:");
foreach (var item in array)
{
Console.Write($"{item} ");
}
Console.WriteLine();
}
public void RotateLeft()
{
for (int j = 0; j < 3; j++)
{
int temp = (int)array[0];
for (int i = 0; i < array.Count - 1; i++)
{
array[i] = array[i + 1];
}
array[array.Count - 1] = temp;
}
}
public void RotateRight()
{
for (int j = 0; j < 3; j++)
{
int temp = (int)array[array.Count-1];
for (int i = array.Count - 1; i > 0; i--)
{
array[i] = array[i - 1];
}
array[0] = temp;
}
}
}
internal class Program
{
static void Main(string[] args)
{
ArrayRotate arrayRotate = new ArrayRotate(new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
arrayRotate.DisplayArray();
arrayRotate.RotateLeft();
arrayRotate.DisplayArray();
arrayRotate.RotateRight();
arrayRotate.DisplayArray();
Console.ReadKey();
}
}
}
Comments
Leave a comment