1. Prompt user for search value 'k'
2. Receive 'k'
3. Initialize a loop to search the array
4. Compare 'k' to each of the values in the array
5. If 'k' is equal to any of the values in the array exit the array and display "Yes", if not display "No"
int array[40];
int k, count_k, count_array;
cout << "Enter the value you are searching for: ";
cin >> k;
count_k = 0;
count_array = 0;
while(count_k != 0 || count_array == 39)
{
if(count_array == k)
count_k++;
else
count_array++;
}
if(count_k != 0)
cout << "Yes.";
else
cout << "No.";
The program will end in finite time with an upper bound of O(n): the time it takes to go through the loop once. The program will always have an output, as either it finds the value in the array and displays "Yes", or it doesn't find the value and displays "No."
No comments:
Post a Comment