Lab9starter.cpp Parts.txt P-17345 B 3 33.14 P-42329 A 43 93.94 P-38388 C 37 20.8
ID: 3602743 • Letter: L
Question
Lab9starter.cppParts.txt P-17345 B 3 33.14 P-42329 A 43 93.94 P-38388 C 37 20.84 P-40426 D 32 11.67 P-28509 D 26 12.95 P-27850 A 46 181.07 P-14702 A 38 92.06 P-13987 D 17 27.24 P-27648 D 10 1.39 P-24991 D 34 28.48 P-19041 B 45 63.99 P-33224 D 15 8.96 P-25594 C 16 32.59 P-26408 F 9 0.37 P-38870 A 29 127.19 P-38757 A 30 191.64 P-42259 B 29 73.98 P-11273 D 15 21.48 P-17345 B 3 33.14 P-42329 A 43 93.94 P-38388 C 37 20.84 P-40426 D 32 11.67 P-28509 D 26 12.95 P-27850 A 46 181.07 P-14702 A 38 92.06 P-13987 D 17 27.24 P-27648 D 10 1.39 P-24991 D 34 28.48 P-19041 B 45 63.99 P-33224 D 15 8.96 P-25594 C 16 32.59 P-26408 F 9 0.37 P-38870 A 29 127.19 P-38757 A 30 191.64 P-42259 B 29 73.98 P-11273 D 15 21.48 Purpose This experiment is designed to develop confidence with searching and sorting functionality using vectors. Additionally, the experiment reinforces previously introduced topics, such as functions, selection logic, and repetition. Instructions The lab this week serves as a companion for homework 9. At the end of this lab, you will have completed code for a good portion of homework 9. For this lab, write a program that implements searching and sorting functionality on the data contained in the parts.txt file located on Canvas under the Week 10 module. Eaclh row of this file is comprised of four related pieces of data: 1 part number; 2) class; 3) on-hand inventory (number of parts); and 4) cost. Use the starter code lab9starter.cpp, also listed under Week 10 module, to read in the parts.txt file. Compile the starter code and make sure the p successfully!" If it doesn't, double check where your copy of the file resides and make sure the function read File0 is reading from that location. program generates the message "Parts file read . After the file has been read in, we will be working with the string vector vecPartNum only. The remaining three vectors may be ignored. In your program, implement the following functions: 1. bubbleSort0-The pseudocode for this sort function is discussed below 2. binarySearch0 The code for a binary search function is given in the course text in Chapter 8 (pp. 497) There are already function prototypes and stubs given in the starter code. If you do not have the course text, then implement your own version of a binary search on a sorted string vector. Make sure that your search function displays the number of loops performed during the respective search. A user-input loop has been implemented for you to continually prompt the user for a search key until a sentinel value of-" is entered. Next, a binary and linear search of the vecPartNum string vector using the user- specified key is performed. Make sure to report the index where a search key was found, or that it was not found. A sample program execution is shown in the figure on the next page.
Explanation / Answer
Implementation of Bubble Sort-
void bubbleSort(vector<string> &v)
{
int i, j;
string temp;
/*iterate elements of array */
for (i = 0; i < v.size()-1; i++)
for (j = 0; j < v.size()-i-1; j++)
/* if current element is greater than next element ,Swap both*/
if (v[j].compare(v[j+1])>0){
temp=v[j];
v[j]=v[j+1];
v[j+1]=temp;
}
}
Implementation of BinarySearch-
int binarySearch(const vector<string>& name,string key)
{
int countItr=0;
int l=0;
int r=name.size()-1;
/*loop while left pointer less than or equal to right pointer*/
while (l <= r)
{
countItr++;
//calculating mid point
int m = l + (r-l)/2;
// Check if key is present at mid
if (name[m] == key){
cout<<"Performing binary search...search completes in:"<<countItr<<" iterations"<<endl;
return m;
}
// If key smaller, ignore right half
if (arr[m].compare(key)>0)
r = m - 1;
// If key is greater, ignore left half
else
l = m + 1;
}
// if we reach here, then element was not present
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.