Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9.  
  10.  
  11.  
  12. int LinearSearch(int [], int, int);
  13.  
  14. int main()
  15. {
  16. again:
  17. clock_t start = clock ();
  18. system("cls");
  19. int N;
  20. cout<<"How many random numbers do you want?\nEnter:";
  21. cin>>N;
  22. int array[N];
  23. srand(time(0));
  24. for(int i=0; i<N; i++)
  25. {
  26. array[i]=rand();
  27. cout<<array[i]<<" ";
  28. }
  29. int key, sub;
  30. cout<<"\nEnter the number you want to search: ";
  31. cin>>key;
  32.  
  33. sub = LinearSearch(array, N, key);
  34.  
  35. if(sub > -1)
  36. cout<<"\nThe number you are searching is at index "<<sub;
  37. else
  38. cout<<"\nThe number you are searching is not in the list";
  39.  
  40. clock_t timeElapsed = ( (clock() - start )*1000) / CLOCKS_PER_SEC;
  41.  
  42.  
  43. char x;
  44. cout<<"\nDo you like to try again?[Y/N]";
  45. cin>>x;
  46. if(x == 'y' or x == 'Y')
  47. goto again;
  48.  
  49.  
  50. return 0;
  51. }
  52.  
  53. int LinearSearch(int list[], int size,int search)
  54. {
  55. int i;
  56. for(i=0; i < size; i++)
  57. {
  58. if (list[i] == search)
  59. return i;
  60. }
  61. return -1;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement