Write, compile, and run a C++ program that generates 200 random integers in betw
ID: 3572024 • Letter: W
Question
Write, compile, and run a C++ program that generates 200 random
integers in between 1 to 200. Some of the randomly generated integers may be
repeated as well. Next sort the randomly generated 200 integers using the Bubble
sort algorithm. Store the sorted numbers in an output data file. Then prompt the
user to enter an integer in between 1 to 200 (inclusive) out of the keyboard as a
key element. Next find out the location of the key element in the sorted array using
both the binary search and the sequential search algorithms. The result returned by
binary search and the sequential search algorithm may vary for repeated elements
inside the sorted array. This is acceptable. Please write out the results returned by
the binary search and the sequential search algorithms to the output data file. Also,
please implement your Bubble sort algorithm, Binary Search and Sequential Search
algorithms as three separate functions that will be called from the function main().
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int seqsearch(int dataset[],int target,int n){
bool found=false;
int pos=-1;
for(int i=0;i<n && found!=true;i++)
if(target==dataset[i]){pos=i;found=true;}
return pos;
}
int main(){
int arr[5]={23,2,3,34,6}; //unsorted data set
int pos;
pos=seqsearch(arr,23,5);
if(pos!=-1)
cout<<"The target item was found at location:"<<pos<<".";
else cout<<"The target item was not found in the list."<<endl;
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.