USING C++ Populate an array of size 100 with integers > = 0 and < = 1000. Initia
ID: 3770286 • Letter: U
Question
USING C++
Populate an array of size 100 with integers > = 0 and < = 1000. Initialize the array using a random number generator with “time” as the seed value., call it RandomNums.
*Print the elements of the array RandomNums after initialization.
*Perform selection sort using function on array RandomNums, and sort the array in non-increasing order
Use the sorted array, and
1. Print the elements of the array. (same as step 3 from previous question) 2. Use Binary Search function to find a number (passed as one of the parameters), constituted of last 3 digits of your UIN. You have to hard code this inside your code. For example: If your UIN is****123 ->Search for number 123 a. Print the number (Eg: 123) b. Report if you could find the number (Yes or No) and the array index 3. Perform Binary Search and report, whether or not the user defined value N (positive or negative) is present in the array. Also, if it is present, then print the array index where it was found. Note that, array consists of only positive integers in a specified range as in Program 2. So, even if N given by user is a negative integer, the code should ignore the sign and search for the positive part. Eg: if N =-5, the program should search for 5 in the array.Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void selectSort(int arr[], int n)
{
//pos_min is short for position of min
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j=i+1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
int binary_search(int array[],int first,int last, int search_key)
{
int index;
if (first > last)
index = -1;
else
{
int mid = (first + last)/2;
if (search_key == array[mid])
index = mid;
else if (search_key < array[mid])
index = binary_search(array,first, mid-1, search_key);
else
index = binary_search(array, mid+1, last, search_key);
} // end if
return index;
}// end binarySearch
int main()
{
srand(time(NULL));
int data[100];
int i,key = 100,index_binary,UIN_search;
for(i=0;i<100;i++)
data[i] = rand()%1001;
selectSort(data,100);
//Part a
for(i=0;i<100;i++)
cout<<data[i]<<" ";
cout<<endl;
//Part b
UIN_search = 123;
index_binary = binary_search(data,0,99,UIN_search);
if(index_binary == -1)
cout<<UIN_search<<" is not present in the array ";
else
cout<<UIN_search<<" is present at index "<<index_binary<<endl;
cout<<"Enter an element to be searched ";
cin>>key;
if(key<0)
key *= -1;
index_binary = binary_search(data,0,99,key);
if(index_binary == -1)
cout<<key<<" is not present in the array ";
else
cout<<key<<" is present at index "<<index_binary<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.