Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in C++ , Notice that the search did not have to look through all of the 50 numbe

ID: 653677 • Letter: I

Question

in C++ , Notice that the search did not have to look through all of the 50 numbers to not find the number 200. As soon as the program found 293, it could have known that the number 200 was not in the list. Modify your program to take advantage of this fact.

UNSORTED:

42 8469 6335 6503 9171 5726 1480 9361 6965 4467

5706 8148 3284 6829 9962 492 2996 1944 4828 5437

2395 4606 3903 154 293 2384 7423 8718 9720 9897

5448 1729 4773 1540 1870 9914 5670 6302 7037 9895

8706 3814 1326 337 7675 4665 5143 7712 8256 6869

SORTED:

42 154 293 337 492 1326 1480 1540 1729 1870

1944 2384 2395 2996 3284 3814 3903 4467 4606 4665

4773 4828 5143 5437 5448 5670 5706 5726 6302 6335

6503 6829 6869 6965 7037 7423 7675 7712 8148 8256

8469 8706 8718 9171 9361 9720 9895 9897 9914 9962

Enter a number to search for: 200

Not found in 3 comparisons.

Explanation / Answer

#include<iostream.h>
#include<conio.h>


void main()
{
   int a[]={42,8469,6335,6503,9171,5726,1480,9361,6965,4467,5706,8148,3284,6829,9962,492, 2996,1944,4828,5437,2395,4606,3903,154,293,2384,7423,8718,9720,9897,5448,1729,4773,1540,1870,9914,5670,6302,7037,9895,8706,3814,1326,117,7675,4665,5143,7712,8256,6869};
   int i,j,b=1,c=1,n;

   clrscr();

   for(i=0;i<5;i++)
   {
       for(j=0;j<=i;j++)
       {
           if(a[j]>a[i])
           {
               c=a[j];
               a[j]=a[i];
               a[i]=c;
           }
       }
   }

   cout<<" Enter the number to search for:-";
   cin>>n;

   for(i=0;i<50;i++)
   {

       if(a[i]==n)
       {
           cout<<"Item Found in "<<b<<"comparisions";
           break;

       }
       else if(a[i]>n)
       {
           cout<<"Item not Found in "<<b<<"comparisions";
           break;
       }
       b++;
   }

   getch();
}