ellapt

T9.4.CountInArray

Jan 19th, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. using System;
  2.  
  3. class CountInArray
  4. {
  5. static int CntOfNumInArray (int num, int[] arr)
  6. {
  7. int cnt=0;;
  8. for (int i = 0; i < arr.Length; i++)
  9. {
  10. if (num==arr[i])
  11. {
  12. cnt++;
  13. }
  14. }
  15. return cnt;
  16. }
  17.  
  18. static void Main()
  19. {
  20. string strNum;
  21. int n;
  22. int checkNum;
  23. int count;
  24. Console.WriteLine("Count how many times given number appears in given array");
  25.  
  26. Console.WriteLine("Enter an integer number: ");
  27. checkNum=int.Parse(Console.ReadLine());
  28.  
  29. do
  30. {
  31. Console.Write("Enter array length n > 1: ");
  32. }
  33. while (!int.TryParse(strNum = Console.ReadLine(), out n) || n <= 1);
  34.  
  35. Console.WriteLine("Enter array elements:");
  36. int[] intArray = new int[n];
  37. for (int i = 0; i < n; i++)
  38. {
  39. intArray[i] = int.Parse(Console.ReadLine());
  40. }
  41.  
  42. count=CntOfNumInArray (checkNum, intArray);
  43.  
  44. Console.WriteLine("The number {0} appears {1} times in the array.", checkNum,count);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment