OK, so how do we do this searching problem?
Well, we're going to go over the most basic, rudimentary style of search, which is called linear
search or sequential search and all.
It is literally just moving one by one from left to right through an array of vector and looking for
the thing that we're interested in.
So this is pretty straightforward.
You probably were able to figure this out.
This one was quite a bit easier than the other ones.
And you know, that's the most straightforward way to find something is just linear search.
So let's go ahead and go in here.
I've already written most of a program because this is stuff we've already done.
So what I do is I just make a vector here.
I have an integer that I read into for those five numbers that go into the vector.
Then I have number one and number two, which is the two numbers that get taken from the user to make
the sum.
So in this loop right here, it's just what we've been doing over and over so far, which is getting
five numbers and reading them into the vector here.
Then we say into two integers to be summed and we read in two more integers.
And then what I check here is, I say if.
Linear search and I send this vector and the sum of number one, plus them to both as arguments to this
and linear search function, and that function is up here, which I have not written yet, but you notice
that it takes a vector of integers by reference here, passing it by reference and then we have an integer
in this pass by value.
So it says if this function returns true, then this whole statement here will be replaced by true and
it will evaluate to true, and it will run the code inside here, which says that no one less known
to equals the sum is in the list.
Else we're going to say the same thing to say is not in the list.
All right.
So pretty straightforward, I'm just going to go ahead and implement this function is a very straightforward
function.
It's just going to be simple.
I live where I do the same style.
Live or die with zero is less than the.
I'm going to say V because we're passing the vector as the variable V.
Some say we that size I + + and then I'm just going to have some f check in here.
And I'm just going to say if v ie
is equal to N, then I'm just going to return true.
And I'm going to live through this whole vector, and so if I ever see.
That the current position in the vector is equal to the number that was passed to this function.
Then I'm going to immediately return from the function and I'm going to return truth to notice that
this is a Boolean type function.
This is the return type it's bore.
So I'm going immediately, just break out of the whole function and return true.
If I'm able to successfully make it through the whole loop, so up to, you know, we got size minus
one basically and I stopped the loop.
That means that I never found an inside of the vector.
And so in that case, I'm going to basically be down here after this last bracket for the loop.
And so I can just return false at this point because that means that I did not find the item I was looking
for.
And so this will return either true or false.
And that will set us up nicely right here to check to see whether the number was in the list or not.
So let's go ahead and run this.
So I'm just going to for a plus linear search.
So I'll say, let's start out and I'll run this and let's just
put some numbers in here.
So we have four seven eight one three, and I'm just going to say, how about we do five and three?
So five plus three is eight is in the list, right?
Because eight is here.
So let's go ahead and try this again, and I'll put the same one I to save for seven eight one three.
And let's put something like, you know.
Nine and three.
So, Nancy is one that is not in the list.
All right, so just have those two examples there, it's often best to do a lot more input and testing
with your programs just to be sure that everything is working correctly, but I'm just going to leave
it at this.
So if you are able to figure that out, great.
If not, you know, it's you're probably just overthinking it because really, you can just use that
linear surge to loop through with just a basic foil loop and you don't have to do anything else besides
that, although there is a more efficient way to search for items in a list, and that's what we're
going to be going over in the next lecture, and it actually requires us to first sort list.
So with that, I will see you in the next lecture.