Guest User

Problem

a guest
Feb 28th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using System;
  2.  
  3. public class ReverseInputArray
  4. {
  5.     private int[] data; // the array to reverse
  6.     public ReverseInputArray(int[] d)
  7.     {
  8.         data = d;
  9.     }
  10.  
  11.     // Uses input dialogs to input an array
  12.     public static int[] ReadIntArray()
  13.     { // Note 1
  14.         Console.Write("Enter the array size: ");
  15.         int size = int.Parse(Console.ReadLine()); // Note 2
  16.         int[] anArray = new int[size]; // Note 3
  17.         for (int i = 0; i < size; i++)
  18.         {
  19.             Console.Write("Enter anArray[" + i + "]: ");
  20.             anArray[i] = int.Parse(Console.ReadLine());
  21.         }
  22.         return anArray;
  23.     }
  24.  
  25.     // Swaps the array elements at indices left and right
  26.     public void Swap(int left, int right)
  27.     {
  28.         int temp = data[left];
  29.         data[left] = data[right];
  30.         data[right] = temp;
  31.     }
  32.  
  33.     /* Reverses the order of the array elements
  34.     * between indices left and right
  35.     */
  36.     public void Reverse(int left, int right)
  37.     {
  38.         while (left < right)
  39.         {
  40.             Swap(left, right);
  41.             right--;
  42.             left++;
  43.         }
  44.     }
  45.  
  46.     // Displays the array
  47.     public void Display(String message)
  48.     {
  49.         Console.Write(message + "{");
  50.         for (int i = 0; i < data.Length; i++)
  51.         {
  52.             if (i != 0) Console.Write(",");
  53.             Console.Write(data[i]);
  54.         }
  55.         Console.WriteLine("}");
  56.     }
  57.  
  58.     // Reverses an array and displays the result
  59.     public static void Main()
  60.     {
  61.         int[] score = ReadIntArray();
  62.         ReverseInputArray r = new ReverseInputArray(score);
  63.         r.Display("The original array is ");
  64.         r.Reverse(0, score.Length - 1);
  65.         r.Display("The reversed array is ");
  66.         Console.Read();
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment