Following is the lab9starter.cpp Parts.txt P-14748 C 24 24.8 P-14226 D 14 28.95
ID: 3602598 • Letter: F
Question
Following is the lab9starter.cppParts.txt P-14748 C 24 24.8 P-14226 D 14 28.95 P-13726 B 12 51.08 P-31722 A 31 191.32 P-33195 C 11 24 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-28690 A 45 19.34 P-34050 B 16 18.99 P-11245 D 45 0.93 P-27969 C 2 12.52 P-34512 B 2 69.17 P-20491 B 32 43.2 P-14518 D 9 5.56 P-17243 A 7 69.19 P-19780 A 33 44.66 P-28061 A 38 117.29 P-22844 D 10 0.5 P-23975 D 5 6.84 P-37542 B 39 26.43 P-29182 A 11 26.83 P-34554 A 33 11.27 P-20206 B 17 39 P-42217 D 45 16.88 P-23146 D 6 16.89
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
// Function prototype
bool readFile(vector<string> &, vector<char> &, vector<int> &, vector<double> &);
void bubbleSort(vector<string> &, vector<char> &, vector<int> &, vector<double> &);
int binarySearch(const vector<string> &, string);
int linearSearch(const vector<string> &, string);
// Program entry point
int main()
{
// Define several vectors to hold parts information
vector<string> vecPartNumber;
vector<char> vecClass;
vector<int> vecOHB;
vector<double> vecCost;
// First, read in parts file and report the status
bool bFileOK = readFile(vecPartNumber, vecClass, vecOHB, vecCost);
if(bFileOK)
{
cout<<" Parts file read successfully!";
// Add sort part number vector
bubbleSort(vecPartNumber, vecClass, vecOHB, vecCost);
// Loops till end of vecPartNumber
for(int i = 0; i < vecPartNumber.size(); i++)
{
// Displays the information
cout<<" "<<vecPartNumber[i]<<" "<<vecClass[i]<<" "<<vecOHB[i]<<" "<<vecCost[i];
} // End of for loop
}// End of if condition
// Otherwise display error message unable to open the file
else
{
cout<<" Unable to read parts file!";
}// End of else
// As long as the file was read successfully, do the user-input loop to continually compare searches
if(bFileOK)
{
string stKey;
int nRet = -1;
// Loops till stKey is not -1
do
{
// Accepts the search key
cout<<" Enter the search query: ";
cin>>stKey;
// Perform both a binary and linear search here based on the user-defined key
// make sure both functions return the number of iterations
if(stKey != "-1")
{
// Calls the function for binary search
cout<<" Performs binary search.....";
nRet = binarySearch(vecPartNumber, stKey);
// Checks the return result if it is not -1 displays the index position
if(nRet != -1)
cout<<" Part found at index "<<nRet;
// Otherwise not found
else
cout<<" Part not found";
// Calls the function for linear search
cout<<" Performing linear search.....";
nRet = linearSearch(vecPartNumber, stKey);
// Checks the return result if it is not -1 displays the index position
if(nRet != -1)
cout<<" Part found at index "<<nRet;
// Otherwise not found
else
cout<<" Part not found";
}// End of inner if condition
}while(stKey != "-1");
}// End of outer if condition
return 0;
}// End of function
// Performs Bubble sort on a input string vector
void bubbleSort(vector<string> &v, vector<char> &vc, vector<int> &ohb, vector<double> &co)
{
//Stores the length of array
int n = v.size();
//Loops till length minus one times
for (int i = 0; i < n - 1; i++)
{
//Loops till length minus i minus one times
for (int j = 0; j < n - i - 1; j++)
{
//Checks if the current position of array value is greater than the next position value then swap
if (v[j].compare(v[j+1]) > 0)
{
// Swapping part number
string tempS = v[j];
v[j] = v[j+1];
v[j+1] = tempS;
// Swapping class
char tempC = vc[j];
vc[j] = vc[j+1];
vc[j+1] = tempC;
// Swapping OHB
int tempO = ohb[j];
ohb[j] = ohb[j+1];
ohb[j+1] = tempO;
// Swapping cost
double tempCo = co[j];
co[j] = co[j+1];
co[j+1] = tempCo;
}//End of if
}//End of inner for loop
}//End of outer for loop
}// End of function
// Performs binary search with given key on the specified vector
int binarySearch(const vector<string> &name, string key)
{
// Loop through each element of the input vector until key is found or size limit is reached
int nLoop = 0;
int return_val = -1;
//Local variable to store starting , end and middle index position of the array
int Beg, End, Mid;
//Initializes the variables to zero
Beg = 0;
//Set the end to length of the array minus one because the starting index position of the array is zero
End = name.size();
//Loops till begin is less than or equals to end position
while(Beg <= End)
{
// Increase the counter for number of iteration
nLoop++;
//Calculates the mid index position
Mid = (Beg + End) / 2;
//Checks if the search number is equal to array mid index position value
if(key == name[Mid])
{
return_val = Mid;
break;
}//End of if condition
//Checks if the search number is less than the array mid index position value
else if(key < name[Mid])
//Set the end to mid minus one because mid index position is already considered above
//Array is sorted in ascending order so number is available in the second half part of the array
End = Mid - 1;
//Otherwise the search number is greater than the array mid index position value
else
//Set the beginning to mid plus one because mid index position is already considered above
//Array is sorted in ascending order so number is available in the first half part of the array
Beg = Mid + 1;
}//End of while loop
// Report the number of loops performed
cout<<" Search complete in: "<<nLoop<<" iterations.";
return return_val;
}// End of function
// Performs sequential search with given key on the specified vector
int linearSearch(const vector<string> &pn, string key)
{
// Set up index variable, return value, and boolean test variable
int i = 0, return_val;
bool found = false;
// Loop through each element of the input vector until key is found or size limit is reached
int nLoop = 0;
// Loops till end of pn
while(!found && i < pn.size() && key >= pn[i])
{
// Increase the counter for number of iteration
nLoop++;
// Checks if the key value is equals to the pn i index position value
if(key == pn[i])
// Set true to found
found = true;
// Otherwise increase the loop control variable i by one
else
i++;
}// End of while loop
// Report the number of loops performed
cout<<" Search complete in: "<<nLoop<<" iterations.";
// Determine the appropriate return value
if(found)
return_val = i;
else
return_val = -1;
return return_val;
}// End of function
// Read in the parts file and report status of read
bool readFile(vector<string> & vecNumber, vector<char> &vecClass, vector<int> &vecOHB, vector<double> &vecCost)
{
// Return value
bool bRet = false;
//Starts with the fresh set of vectors
vecNumber.clear();
vecClass.clear();
vecOHB.clear();
vecCost.clear();
// File stream variable
ifstream fin;
// Open the parts file
fin.open("Parts.txt");
// Check that it opened properly
if(fin.is_open())
{
// Set true to bRect
bRet = true;
}
// No need to read the file if it was not opened!
string tmpNum;
char tmpClass;
int tmpOHB;
double tmpCost;
// If bRect is true file is success fully opened start reading
if(bRet)
{
// Loops till end of the file
do
{
// Read the contents of file and stores it in the variable
fin>>tmpNum;
// Push the variable data to the vector
vecNumber.push_back(tmpNum);
fin>>tmpClass;
vecClass.push_back(tmpClass);
fin>>tmpOHB;
vecOHB.push_back(tmpOHB);
fin>>tmpCost;
vecCost.push_back(tmpCost);
}while(!fin.eof());
}// End of if condition
// Close the file and return the status of the file open
fin.close();
return bRet;
}// End of function
Sample Run:
Parts file read successfully!
P-11245 D 45 0.93
P-11273 D 15 21.48
P-13726 B 12 51.08
P-13987 D 17 27.24
P-14226 D 14 28.95
P-14518 D 9 5.56
P-14702 A 38 92.06
P-14748 C 24 24.8
P-17243 A 7 69.19
P-17345 B 3 33.14
P-19041 B 45 63.99
P-19780 A 33 44.66
P-20206 B 17 39
P-20491 B 32 43.2
P-22844 D 10 0.5
P-23146 D 6 16.89
P-23975 D 5 6.84
P-24991 D 34 28.48
P-25594 C 16 32.59
P-26408 F 9 0.37
P-27648 D 10 1.39
P-27850 A 46 181.07
P-27969 C 2 12.52
P-28061 A 38 117.29
P-28509 D 26 12.95
P-28690 A 45 19.34
P-29182 A 11 26.83
P-31722 A 31 191.32
P-33195 C 11 24
P-33224 D 15 8.96
P-34050 B 16 18.99
P-34512 B 2 69.17
P-34554 A 33 11.27
P-37542 B 39 26.43
P-38388 C 37 20.84
P-38757 A 30 191.64
P-38870 A 29 127.19
P-40426 D 32 11.67
P-42217 D 45 16.88
P-42259 B 29 73.98
P-42329 A 43 93.94
Enter the search query: P-22221
Performs binary search..... Search complete in: 6 iterations.
Part not found
Performing linear search..... Search complete in: 14 iterations.
Part not found
Enter the search query: P-11245
Performs binary search..... Search complete in: 5 iterations.
Part found at index 0
Performing linear search..... Search complete in: 1 iterations.
Part found at index 0
Enter the search query: P-42217
Performs binary search..... Search complete in: 6 iterations.
Part found at index 38
Performing linear search..... Search complete in: 39 iterations.
Part found at index 38
Enter the search query: P-24991
Performs binary search..... Search complete in: 4 iterations.
Part found at index 17
Performing linear search..... Search complete in: 18 iterations.
Part found at index 17
Enter the search query: -1