Untitled
unknown
plain_text
a year ago
648 B
8
Indexable
public static void Main(string[] args)
{
int[] user_input = Console.ReadLine().Split().Select(int.Parse).ToArray();
int user_target_number = int.Parse(Console.ReadLine());
//"1 2 3 4" -> "1" "2" "3" -> 1 2 3
linear_search(user_input, user_target_number);
Console.ReadKey();
}
public static void linear_search(int[] user_numbers, int target)
{
int attempts = 0;
for (int i = 0; i < user_numbers.Length; i++)
{
attempts++;
if (user_numbers[i] == target)
{
break;
}
}
Console.WriteLine($"{target} was found in the array.\nAttempts: {attempts}");
}Editor is loading...
Leave a Comment