NMDanny

bagrut 2010-9 bidimensional array

May 22nd, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //                                                 Arr.GetLength(0) - gets the length of the first dimension. Arr.GetLength(1) - gets the length of the second dimension. Arr.Length - gets the length of all objects. (Dimension A * Dimension B)
  7. namespace Bagrut2010_Targil9
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int SameDiagonals=0;
  14.             int[,] Arr = new int[4, 4];
  15.                 Console.WriteLine(Arr.Length);
  16.                 Random rand = new Random();          // Not requested in the exam, just for testing purposes
  17.                 for (int a = 0; a < Arr.GetLength(0); a++)                   // ^
  18.                 {                                              // ^
  19.                     for (int b = 0; b < Arr.GetLength(1); b++)               // ^
  20.                     {                                          // ^
  21.                         Arr[a, b] = rand.Next(0, 2);           // ^
  22.                         Console.Write("{0,6}", Arr[a, b]);               // ^
  23.                     }                                          // ^
  24.                     Console.WriteLine(); Console.WriteLine();  // ^
  25.                 }
  26.  
  27.                 for (int i = 0; i < Arr.GetLength(0); i++)
  28.                 {
  29.                     for (int p = 0; p < Arr.GetLength(1); p++)
  30.                     {
  31.                         if (IsExist(Arr, i, p) == true)
  32.                         {
  33.                             if (IsSame(Arr, i, p) == true)
  34.                                 SameDiagonals++;
  35.                         }
  36.                     }
  37.                 }
  38.                 Console.WriteLine("There are " + SameDiagonals + " diagonls whose length is 3 and are equal to 1.");
  39.  
  40.         }
  41.         static bool IsExist(int[,] Arr,int x, int y)  // x=horizontal row   y=vertical column INDEXES
  42.         { // 36x52 : 36 rows and 52 columns
  43.             if (x <=Arr.GetLength(0) - 3 &&y <=Arr.GetLength(1) - 3)
  44.                 return true;
  45.             else return false;
  46.         }
  47.         static bool IsSame(int[,] array, int x, int y)
  48.         {
  49.             if (IsExist(array,x, y) == true)
  50.             {
  51.                 if(array[x,y]==1&&array[x+1,y+1]==1&&array[x+2,y+2]==1)
  52.                     return true;
  53.                 else return false;
  54.             }
  55.             else return false;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment