00:01
We want to rewrite the program we have been given here so that the user can continue to input values that will be searched for until a sentinel value is entered to end the program.
00:17
And then there is the question, should a pre - or post -test loop be used? so here is the modified program where i'm going to indicate where are the changes i have introduced.
00:33
To solve the problem.
00:36
And the first thing we're going to say is that the function does not change at all.
00:39
That is we're going to do the changes, of course, in the main program, where it is the place where we ask the user to enter values to search for in this array, into the array.
00:51
So the search list function stays the same.
00:55
So we're going to concentrate on the main program up here.
01:01
And we want this search to be done at least once.
01:05
That is, we want the problem to start and as the user enter an interview value to search for.
01:12
And for that reason, we prefer to use a do -while statement.
01:17
So the test is going to be done after at least one loop of search is done.
01:24
Or in an equivalent form, we do the test after the search list function has been called.
01:34
And after that, there is a loop.
01:36
Time asking the user if she or he wants to continue searching for interiors into the array.
01:45
Okay, so the first part is the same.
01:49
We didn't change anything into the declarations of the prototype of the function, the constant integer size, or the include statements.
02:01
And inside the main, the first lines are the same and we enter a new line, here we enter a new line right here to define to declare an integer variables called sentinel which is the variable we're going to read to stop the program or to continue so it's the variable that is used to stop or continue the execution of the program and immediately after we have a do while so all the statements were given in the main program after declaration of the target variable and return zero, the last statement, all those instructions have been put inside the do -while statement here because they are going to be repeated all those instructions while the user do not enter the sentinel value.
03:10
Okay so we add also this line because we want to separate the part where we asked the user to continue from the asking of a new integer value to search for.
03:26
So i added this end line in the c out command asking the user to enter an integer to search for.
03:36
And then these two lines are the same and we see that we have the do while that means it's all the statement statements inside the do the do part of this dual statement all the those instructions are going to be executed at least one time that is the first time we enter the do the do while after that there is a condition that is going to be checked so the condition happens after the statements has been executed.
04:14
And that's correct for us because we want the problem to start asking the user to enter integers to search and then the user has to decide if he or she wants to continue.
04:27
Good.
04:28
So these two lines are the same except for the n line i add here to separate two parts.
04:37
This is the same that is we call the function.
04:40
Search list with passing into that function the array of numbers given here the size of that array which is a constant integer declared outside of the main function right here and that's the same that's not change and target is a number read from screen here or from the keyboard that user enter and it's going to be search into the array numbs and that happens inside the search list function.
05:13
Then the result is stored in this found variable that is an integer variable and remember in the search list the value negative one is returned when the number is not encounter, it's not inside the array.
05:35
When it is, we return the position into the array where the number has been found...