C++ Program to Perform Linear Search in an Array #shorts

preview_player
Показать описание
In this C++ program, we demonstrate how to perform a linear search for a specific number within an array of integers. The program uses a predefined array and searches for a target number. It then displays the index at which the number is found or a message indicating that the number is not present in the array.

Here's an overview of how the program works:

1. The program defines a function named `linear_search` responsible for conducting a linear search. It takes three arguments: the array in which to search, the target number to find, and the size of the array (n). The function iterates through the array and checks if each element matches the target number. If a match is found, it returns the index at which the number is located; otherwise, it returns -1 to indicate that the number is not in the array.

2. In the `main` function, a predefined integer array `arr` is provided along with its size `n`. Additionally, a target number `num` (in this case, 7) is specified for the search.

3. The `linear_search` function is called with the array, target number, and array size as arguments to perform the search.

4. Depending on the result of the search, the program either prints the index where the target number was found or a message indicating that the number is not present in the array.

#shortsvideo #trending #viral #shortsfeed
Рекомендации по теме
visit shbcf.ru