Advertisement
vlad0

Methods 5

Jan 14th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _05.TwoNeighbours
  7. {
  8.     class TwoNeighbours
  9.     {
  10.         static int[] myArray = new int[20];
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             GenerateRandomMatrix(); //generates random matrix
  15.             PrintMatrix();
  16.             Console.WriteLine();
  17.             Console.Write("Choose position of the element: ");
  18.             int index = int.Parse(Console.ReadLine());
  19.             CheckNeighbours(index);
  20.         }
  21.  
  22.         private static void CheckNeighbours(int index)
  23.         {
  24.             if (index>= myArray.Length || index <= 0)
  25.             {
  26.                 Console.WriteLine("Wrong Position!");
  27.             }
  28.             else
  29.             {
  30.                 if (index == myArray.Length-1) //this is the last element we check only the left neighbour
  31.                 {
  32.                     Console.WriteLine("One Left Neighbour!");
  33.                     CheckLefts(index);
  34.                    
  35.                 }
  36.                 else if (index == 0)
  37.                 {
  38.                     Console.WriteLine("One Right Neighbour!");
  39.                     CheckRights(index);
  40.                 }
  41.                 else
  42.                 {
  43.                     Console.WriteLine("Two neighbours!");
  44.                     CheckLefts(index);
  45.                     CheckRights(index);
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private static void CheckRights(int index)
  51.         {
  52.             if (myArray[index] > myArray[index + 1])
  53.             {
  54.                 Console.WriteLine("The right neighbour {0} is bigger than {1}", myArray[index + 1], myArray[index]);
  55.             }
  56.             else if (myArray[index] < myArray[index + 1])
  57.             {
  58.                 Console.WriteLine("The right neighbour {0} is smaller than {1}", myArray[index + 1], myArray[index]);
  59.             }
  60.             else
  61.             {
  62.                 Console.WriteLine("The right neighbour {0} is equal {1}", myArray[index + 1], myArray[index]);
  63.             }
  64.         }
  65.  
  66.         private static void CheckLefts(int index)
  67.         {
  68.             if (myArray[index] > myArray[index - 1])
  69.             {
  70.                 Console.WriteLine("The left neighbour {0} is bigger than {1}", myArray[index - 1], myArray[index]);
  71.             }
  72.             else if (myArray[index] < myArray[index - 1])
  73.             {
  74.                 Console.WriteLine("The left neighbour {0} is smaller than {1}", myArray[index - 1], myArray[index]);
  75.             }
  76.             else
  77.             {
  78.                 Console.WriteLine("The left neighbour {0} is equal {1}", myArray[index - 1], myArray[index]);
  79.             }
  80.         }
  81.  
  82.         private static void PrintMatrix()
  83.         {
  84.             for (int i = 0; i < myArray.Length; i++)
  85.             {
  86.                 Console.Write("{0} ",myArray[i]);
  87.             }
  88.         }
  89.  
  90.         private static void GenerateRandomMatrix()
  91.         {
  92.             Random randomNumber = new Random();
  93.  
  94.             for (int i = 0; i < myArray.Length; i++)
  95.             {
  96.                 myArray[i] = randomNumber.Next(11); //generate random number between 0 and 10
  97.             }
  98.         }
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement