Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- int LinearSearch(int [], int, int);
- int main()
- {
- again:
- clock_t start = clock ();
- system("cls");
- int N;
- cout<<"How many random numbers do you want?\nEnter:";
- cin>>N;
- int array[N];
- srand(time(0));
- for(int i=0; i<N; i++)
- {
- array[i]=rand();
- cout<<array[i]<<" ";
- }
- int key, sub;
- cout<<"\nEnter the number you want to search: ";
- cin>>key;
- sub = LinearSearch(array, N, key);
- if(sub > -1)
- cout<<"\nThe number you are searching is at index "<<sub;
- else
- cout<<"\nThe number you are searching is not in the list";
- clock_t timeElapsed = ( (clock() - start )*1000) / CLOCKS_PER_SEC;
- char x;
- cout<<"\nDo you like to try again?[Y/N]";
- cin>>x;
- if(x == 'y' or x == 'Y')
- goto again;
- return 0;
- }
- int LinearSearch(int list[], int size,int search)
- {
- int i;
- for(i=0; i < size; i++)
- {
- if (list[i] == search)
- return i;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement