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

Write the fillRandom. linearSearch, and binary Search functions in the standard

ID: 3692406 • Letter: W

Question

Write the fillRandom. linearSearch, and binary Search functions in the standard way to the fallowing code fragment illustrates how they are used: const int SIZE =1234567;/* size of the arry */int a [SIZE/* declare the arry */fillRandom (a, SIZE, 100, 200);/*fill with random integers */int loc444LS = linearSearch (a,SIZE, 444);/* get index of ele - 444 */sort (a, SIZE);/*don't worry about this*/int loc444BS =binarySearch (a,SIZE,444);/* get index of ele = 444; Finish this function. It fills the array with integers from first to last, both ends included. void fillRandom (int x[], int size, int first, int last) {

Explanation / Answer

Answer:

void fillRandom(int x[],int size,int first,int last)
{
Random r = new Random ();
int i = 0;
while (i < size)
{

x[i]= r.nextInt(last-first) + first;

i = i + 1;
}
}
int linearSearch(int array[], int arraysize,int keyvalue)
{
int temp=-1;
for(int count=0;count<arraysize;count++)
{
if(keyvalue==array[count])
{
temp=count;
break;
}
}
return temp;
}

int binary_search(int array[],int arraysize,int keyvalue)
{
int begin=0;
int finish=arraysize-1;
int center;
int location=-1;
center=(begin+finish)/2;
do
{
if(keyvalue<array[center])
finish=center-1;
elseif(keyvalue>array[center])
begin=center+1;
center=(begin+finish)/2;
}
while(begin<=finish && array[center]!=keyvalue);
if(array[center]==keyvalue) location=center;
return location;
}