For this computer assignment, you are to write and implement a C++ program to im
ID: 3637757 • Letter: F
Question
For this computer assignment, you are to write and implement a C++ program to implement two search algorithms (linear search and binary search) on randomly generated integers.For an array with elements of type T, use a vector container, vector<T>, from the STL:
The source file of the driver program prog2.cc and the header file prog2.h are located in directory: ~onyuksel/courses/340/progs/12s/p2. Put the implementations of your routines, as described below, in your source file sub2.cc.
• void Vectors ( vector <int>& v1, vector <int>& v2, int s1, int s2 ) : Fills the elements of vectors v1 and v2 with random numbers, generated by two sets of pseudo-random numbers with the seed values of s1 and s2, where s1 is for v1and s2 is for v2. To initiate a random number generator (RNG) with the seed value seed, execute the system function srand ( seed ), and to generate a random integer in the range of 1...RND_NO_RANGE (defined in the header file prog2.h), execute “rand ( ) % RND_NO_RANGE + 1”.
• void sortVector ( vector <int>& v ) : A sort algorithm to sort the elements of vector v in ascending order. To implement this routine, simply call the sort ( ) function in the STL.
• bool linearSearch ( const vector <int>& v, int x ) : A linear search algorithm, where x is the searched item in vector v. It simply starts searching for x from the beginning of the vector v to the end, but it stops searching when there is a match. If the search is successful, it returns true; otherwise, it returns false. To implement this routine, simply call the find ( ) function in the STL.
• bool binarySearch ( const vector <int>& v, int x ) : A binary search algorithm, where x is the searched item in vector v. If the search is successful, it returns true; otherwise, it returns false. To implement this routine, simply call the binary_search ( ) function in the STL.
• void printVector ( const vector <int>& v ) : Prints the contents of vector v on stdout. It simply prints up to LINE_SIZE, defined in the header file prog2.h, items on a single line.
Programming Notes:
• You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout.
• Include the header file 340.h, by inserting the statement: #include “/home/onyuksel/courses/340/common/340.h” at the top of your source file. Every system file, you might need, is already included in this header file,
Explanation / Answer
/**
getRandomList() loads the array with random numbers
@returns an array of 50 Integers between -100 and 100
*/
public static Integer[] getRandomList() {
Integer[] list = new Integer[50];
for (int i=0; i<50; i++)
list[i] = (int)(Math.random()*201) - 100;
return list;
}
// Note: import java.util.*; is assumed
public static void main (String[] args) {
Integer[] answer = getRandomList();
Integer[] less = new Integer[answer.length];
Integer[] more = new Integer[answer.length];
System.out.println(Arrays.toString(answer)+" ");
signSort(answer, new Integer(0), less, more);
System.out.println(Arrays.toString(answer));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.