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

Program: Write, compile, and run a C++ program that generates 200 random integer

ID: 3697813 • Letter: P

Question

Program:

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. Next sort the randomly generated 200 integers using the Bubble sort algorithm. Store the sorted numbers in an array as well as an output data file, outSort.txt. Then prompt the user to enter an integer in between 1 to 200 (inclusive) out of the keyboard as a search key. Next find out the location of the search key in the sorted array (from previous step) using the binary search, and then repeat the search using the sequential search algorithms. The result returned by the search may vary for repeated elements inside the sorted array. This is acceptable. Display the results returned by the binary search and the sequential search algorithms to the standard console, and also write out the results to the output data file. If the search target does not exist among the generated integers, display a message “Key does not exist in the array.” Implement the Bubble sort algorithm, Binary Search and Sequential Search algorithms as three separate functions that will be called from the function main().

Explanation / Answer

Answer -

#include <iostream>
#include <cstdlib>/* srand, rand */
#include <fstream>
#include <stdlib.h>
#include <conio.h>   
#include <time.h>   
using namespace std;

void LSearch(int a[],int num)
{
   fstream in_s;
for(int i=0;i<200;i++)
{
if (a[i]==num)
{
cout<<" Number "<<num<<" found at "<<i<<"th position";
in_s.open("d:/outsort.txt",ios_base::app);
in_s<<" Number "<<num<<" found at "<<i<<"th position by Linear Search";
in_s.close();
getch();
}
else
{
cout<<" Number "<<num<<" is not found";
getch();
}
}
}
void BSearch(int a[],int num)
{
fstream in_s;
   int position,lowerbound=0,upperbound=199;
position = ( lowerbound + upperbound) / 2;
while((a[position] != num) && (lowerbound <= upperbound))
{
if (a[position] > num) // If the number is > number to search, ..
{ // decrease position by one.
upperbound = position - 1;
}   
else   
{ // Else, increase position by one.
lowerbound = position + 1;   
}
position = (lowerbound + upperbound) / 2;
}
if (a[position]==num)
{
cout<<" Number "<<num<<" found at "<<position<<"th position";
in_s.open("d:/outsort.txt",ios_base::app);
in_s<<" Number "<<num<<" found at "<<position<<"th position by Binary Search";
in_s.close();
getch();
}
else
{
cout<<" Number "<<num<<" is not found";
getch();
}
}

void sort(int arr1[])
{
   int num_search;
fstream in_s;
for(int i=0;i<200;++i)
{
for(int j=0;j<(200-i);++j)
{
if(arr1[j]>arr1[j+1])
{
int temp=arr1[j];
arr1[j]=arr1[j+1];
arr1[j+1]=temp;
}
}
}
for(int i=0;i<200;i++)
{
in_s.open("d:/outsort.txt",ios_base::app);
in_s<<arr1[i]<<endl;
in_s.close();
}
cout<<" Array Sorted: ";
for(int i=0;i<200;i++)
{
cout<<i<<" = "<<arr1[i]<<endl;

}
   getch();
   cout<<" Enter number to Binary Search : ";
cin>>num_search;
   BSearch(arr1,num_search);
   getch();
   cout<<" Enter number to Linear Search : ";
cin>>num_search;
   LSearch(arr1,num_search);
}
int main()
{
int arr[200];
srand (time(NULL)); /* initialize random seed: */
for(int i=0;i<200;i++)
{
arr[i] = rand() % 200 + 1; /* generate secret number between 0 and 70: */
}
cout<<" Array genrated randomly : ";
for(int i=0;i<200;i++)
{
cout<<i<<" = "<<arr[i]<<endl;
}
sort(arr);
   return 0;
}