Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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.  
  7. namespace Linearsearch2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array
  14.  
  15. var targetvalue = 77; //Establishes what number the search will attempt to find.
  16. var targetpos = -1; //Establishes the position in the array of the target.
  17. var targetnumber = 0; //Establishes the counter for the number of times the target appears.
  18. bool found = false; //Decides wether to change the number or use a counter method.
  19.  
  20. var foundpositions = new int[] { }; //Establishes an array which will hold the positions of located items
  21.  
  22. for (var i = 1; i < array.Length; i++)
  23. {
  24. if (found == true && array[i] == targetvalue)
  25. {
  26. targetnumber = targetnumber + 1;
  27. }
  28.  
  29. if (found == false && array[i] == targetvalue) //If the target value has not been found yet
  30. {
  31. foundpositions.Add(i); //This is the line i need help with. I dont know how to add a value to an array properly.
  32. found = true;
  33. }
  34. }
  35.  
  36. if (targetpos != -1){ //If the target number was found
  37. Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome.
  38. }
  39. else //If the target number was not found
  40. {
  41. Console.WriteLine("The number " + targetvalue + " did not appear in this array."); // Prints the final outcome.
  42. }
  43. }
  44. }
  45. }
  46.  
  47. var foundpositions = new int[] { };
  48.  
  49. var foundpositions = new List<int>();
  50.  
  51. var targetpos = -1;
  52.  
  53. if (targetpos != -1){ //If the target number was found
  54. Console.WriteLine("The number " + targetvalue + " appeared " + targetnumber + " times, at positions " + foundpositions + "."); // Prints the final outcome.
  55. }
  56.  
  57. public static int LinearSearch(int[] items, int target)
  58. {
  59. if (items == null) throw new ArgumentNullException("argument items has null reference");
  60. if (items.Length == 0) return -1; // return -1 if the item is not found
  61. for (int i = 0; i < items.Length; i++)
  62. if (items[i] == target) return i;
  63. return -1; // return -1 if the item is not found
  64. }
  65.  
  66. private static void FindTargets() {
  67. var array = new int[] { 1, 31, 10, 9, 420, -5, 77, 420, 300, 99, 1, 31, 10, 9, 420, -5, 77, 420, 300, 99 }; //Sets up the array
  68. int target = 77;
  69. List<int> foundTargets = GetPositions(target, array);
  70. StringBuilder sb = new StringBuilder();
  71. sb.Append("There are " + foundTargets.Count + " int(s) that match " + target + Environment.NewLine);
  72. sb.Append("The array indexs for the target ints " + target + " are: ");
  73. int count = 0;
  74. foreach (int curInt in foundTargets) {
  75. sb.Append(curInt);
  76. if (count < foundTargets.Count - 1)
  77. sb.Append(", ");
  78. count++;
  79. }
  80. Console.WriteLine(sb.ToString());
  81. }
  82.  
  83. private static List<int> GetPositions(int target, int[] intArray) {
  84. List<int> foundTargets = new List<int>();
  85. for (int i = 0; i < intArray.Length; i++) {
  86. if (intArray[i] == target) {
  87. foundTargets.Add(i);
  88. }
  89. }
  90. return foundTargets;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement