Advertisement
vlad0

Methods 6

Jan 14th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _06.NeighboursMethod
  7. {
  8.     class NeighboursMethod
  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.             int hasNeighbours=-1;//by default there isn't bigger neighbours
  18.  
  19.              //we dont' check the first and last element because they don't have
  20.             //left or right neighbours
  21.             for (int i = 1; i < myArray.Length-1; i++)
  22.             {
  23.                 hasNeighbours = CheckNeighbours(i);
  24.                 if (hasNeighbours == 1)
  25.                 {
  26.                     Console.WriteLine("The first element with two bigger neighbours is: {0} at position: {1}",myArray[i],i);
  27.                     return;
  28.                 }
  29.             }
  30.             Console.WriteLine(hasNeighbours);    
  31.         }
  32.  
  33.         private static int CheckNeighbours(int index)
  34.         {
  35.             if (CheckLefts(index)&&CheckRights(index))
  36.             {
  37.                 return 1;
  38.             }
  39.             else
  40.             {
  41.                 return -1;
  42.             }
  43.            
  44.         }
  45.  
  46.         private static bool CheckRights(int index)
  47.         {
  48.             if (myArray[index] > myArray[index + 1])
  49.             {
  50.                 return true;
  51.             }
  52.  
  53.             return false;
  54.         }
  55.  
  56.         private static bool CheckLefts(int index)
  57.         {
  58.             if (myArray[index] > myArray[index - 1])
  59.             {
  60.                 return true;
  61.             }
  62.  
  63.             return false;
  64.         }
  65.  
  66.         private static void PrintMatrix()
  67.         {
  68.             for (int i = 0; i < myArray.Length; i++)
  69.             {
  70.                 Console.Write("{0} ", myArray[i]);
  71.             }
  72.         }
  73.  
  74.         private static void GenerateRandomMatrix()
  75.         {
  76.             Random randomNumber = new Random();
  77.  
  78.             for (int i = 0; i < myArray.Length; i++)
  79.             {
  80.                 myArray[i] = randomNumber.Next(101); //generate random number between 0 and 100
  81.             }
  82.         }
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement