Advertisement
AvengersAssemble

DiamondMatrix

Apr 2nd, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1.         public static void CreateDiamond(int[,] arr)
  2.         {
  3.             int oneVal = 0, change = 1;
  4.             for (int i = 0; i < arr.GetLength(0); i++)
  5.             {
  6.                 for (int k = 0; k < arr.GetLength(1); k++)
  7.                 {
  8.                     if (k == arr.GetLength(1) / 2 - oneVal || k == arr.GetLength(1) / 2 + oneVal)
  9.                         arr[i, k] = 1;
  10.                     else
  11.                         arr[i, k] = 0;
  12.                 }
  13.                 if (oneVal == arr.GetLength(0) / 2)
  14.                     change = -1;
  15.                 oneVal += change;
  16.             }
  17.         }
  18.         public static bool CheckDiamond(int[,] arr)
  19.         {
  20.             int oneVal = 0, change = 1;
  21.             for (int i = 0; i < arr.GetLength(0); i++)
  22.             {
  23.                 for (int k = 0; k < arr.GetLength(1); k++)
  24.                 {
  25.                     if (k == arr.GetLength(1) / 2 - oneVal || k == arr.GetLength(1) / 2 + oneVal)
  26.                     {
  27.                         if (arr[i, k] != 1)
  28.                             return false;
  29.                     }
  30.                     else
  31.                     {
  32.                         if (arr[i, k] != 0)
  33.                             return false;
  34.                     }
  35.                 }
  36.                 if (oneVal == arr.GetLength(0) / 2)
  37.                     change = -1;
  38.                 oneVal += change;
  39.             }
  40.             return true;
  41.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement