Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class ReverseInputArray
- {
- private int[] data; // the array to reverse
- public ReverseInputArray(int[] d)
- {
- data = d;
- }
- // Uses input dialogs to input an array
- public static int[] ReadIntArray()
- { // Note 1
- Console.Write("Enter the array size: ");
- int size = int.Parse(Console.ReadLine()); // Note 2
- int[] anArray = new int[size]; // Note 3
- for (int i = 0; i < size; i++)
- {
- Console.Write("Enter anArray[" + i + "]: ");
- anArray[i] = int.Parse(Console.ReadLine());
- }
- return anArray;
- }
- // Swaps the array elements at indices left and right
- public void Swap(int left, int right)
- {
- int temp = data[left];
- data[left] = data[right];
- data[right] = temp;
- }
- /* Reverses the order of the array elements
- * between indices left and right
- */
- public void Reverse(int left, int right)
- {
- while (left < right)
- {
- Swap(left, right);
- right--;
- left++;
- }
- }
- // Displays the array
- public void Display(String message)
- {
- Console.Write(message + "{");
- for (int i = 0; i < data.Length; i++)
- {
- if (i != 0) Console.Write(",");
- Console.Write(data[i]);
- }
- Console.WriteLine("}");
- }
- // Reverses an array and displays the result
- public static void Main()
- {
- int[] score = ReadIntArray();
- ReverseInputArray r = new ReverseInputArray(score);
- r.Display("The original array is ");
- r.Reverse(0, score.Length - 1);
- r.Display("The reversed array is ");
- Console.Read();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment