jloga4
6/1/2017 - 12:47 AM

Example of how to check if an array is symmetric or not. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/

Example of how to check if an array is symmetric or not. From Fundamentals of Computer Programming with C# http://www.introprogramming.info/wp-content/uploads/2013/07/Books/CSharpEn/Fundamentals-of-Computer-Programming-with-CSharp-Nakov-eBook-v2013.pdf

Console.Write("Enter a positive integer: ");
int n = int.Parse(Console.ReadLine());
int[] array = new int[n];

Console.WriteLine("Enter the values of the array:");
for (int i = 0; i < n; i++)
{
  array[i] = int.Parse(Console.ReadLine());
}

bool symmetric = true;
for (int i = 0; i < array.Length / 2; i++)
{
  if (array[i] != array[n - i - 1])
  {
    symmetric = false;
    break;
  }
}

Console.WriteLine("Is symmetric? {0}", symmetric);

//  Enter a positive integer: 5
//  Enter the values of the array:
//  1
//  2
//  5
//  2
//  1
//  Is symmetric? True