Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
648 B
2
Indexable
Never
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}");
}
Leave a Comment