Categories
C++

Linear Search Using Modern C++

A linear search program was written by my friend Tom that uses modern C++ approach.


I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below. https://www.unlimitedbooksph.com/


Program Listing

/*
The program will ask the use to give for example five numbers
and then the program will  ask the user what number to search
and the program will find the number and what position the number
is Located in.
*/

#include <iostream>
#include <vector>
#include <algorithm>

constexpr int N = 5;

int main()
{
   std::vector<int> numbers(N);
   int num_to_search;

   for (int i = 0; i < N; ++i)
   {
     std::cout << "Enter number #" << i + 1 << ": ";
     std::cin >> numbers[i];
     std::cout << '\n';
   }
   std::cout << "Enter a number to search for: ";
   std::cin >> num_to_search;

   //http://www.cplusplus.com/reference/algorithm/find/
   auto it = std::find(numbers.begin(), numbers.end(), num_to_search);
   if (it == numbers.end())
   {
     std::cout << "Number not found";
   }
   else
   {
     //http://www.cplusplus.com/reference/iterator/distance/
     std::cout << "Found at pos: " << std::distance(numbers.begin(), it);
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *