Question & Answer: Good morning , please would you help me with this progam in c++:…..

Good morning , please would you help me with this progam in c++:

For this assignment you will make a Recursion class with two recursive member functions.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Good morning , please would you help me with this progam in c++:…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

a) Implement overloaded member functions called binSearchRec the recursive binSearchRec algorithm presented in this chapter, starting on page 68 for an array of vehicles. Use a vector instead of an array.

– Sort by year then call a recursive function to do a binary search which lists out the make, model and year for the first year found (or report accordingly) that matches the for a user input.

then

– Sort by make then call a recursive function to do a binary search which lists out the make, model and year for the first year found (or report accordingly) that matches the for a user input.

then

– Sort by model then call a recursive function to do a binary search which lists out the make, model and year for the first year found (or report accordingly) that matches the for a user input.

Read in the vehicles from a vehicle file (vehiclein.txtPreview the documentView in a new window) that has a year, make, and model. Each vehicle is separated with a | (straight bar) on it’s own line. (Note that make and models might have spaces in them)

b) Implement a member function with the same functional requirements, except instead of a recursive member function, use an iterative (non-recursive) binarySearchIter. Your iterative function (with loops instead of recursion) should produce the same results as your recursive function. You can place this member function in your recursion.h and recursion.cpp files.

You will need 4 files: main.cpp, recursion.cpp, recursion.h and vehiclein.txt

the Text file contain :

2010

Ford

Escape

|

2014

BMW

328xi

|

2014

BMW

428xi

|

2012

Ford

Fusion

SE

|

2014

Lamborghini

Gallardo

|

Expert Answer

 

main.cpp:

#include <iostream>

#include <fstream>

#include <sstream>

#include <string>

#include <vector>

#include <cstdlib>

using namespace std;

typedef struct Car {

int year;

string make, model;

} Car;

string trim(string& str)

{

str.erase(0, str.find_first_not_of(‘ ‘)); //prefixing spaces

str.erase(str.find_last_not_of(‘ ‘)+1); //surfixing spaces

return str;

}

void sortByModel(vector<Car>& cars) {

for(size_t i=0; i < cars.size(); i++) {

for(size_t j=i+1; j < cars.size(); j++) {

Car ci = cars[i];

Car cj = cars[j];

if(ci.model.compare(cj.model) > 0) {

Car temp = cars[i];

cars[i] = cars[j];

cars[j] = temp;

}

}

}

}

void sortByMake(vector<Car>& cars) {

for(size_t i=0; i < cars.size(); i++) {

for(size_t j=i+1; j < cars.size(); j++) {

Car ci = cars[i];

Car cj = cars[j];

if(ci.make.compare(cj.make) > 0) {

Car temp = cars[i];

cars[i] = cars[j];

cars[j] = temp;

}

}

}

}

void sortByYear(vector<Car>& cars) {

for(size_t i=0; i < cars.size(); i++) {

for(size_t j=i+1; j < cars.size(); j++) {

Car ci = cars[i];

Car cj = cars[j];

if(ci.year > cj.year) {

Car temp = cars[i];

cars[i] = cars[j];

cars[j] = temp;

}

}

}

}

int binarySearchYearUtil(vector<Car> cars, int start, int end, int userInputYear) {

if(start > end) {

return -1;

}

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.year == userInputYear) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return mid;

} else {

if(start == end) {

return -1;

}

if(midCar.year > userInputYear) {

return binarySearchYearUtil(cars, start, mid-1, userInputYear);

} else {

return binarySearchYearUtil(cars, mid+1, end, userInputYear);

}

}

}

void binarySearchYearRec(vector<Car> cars, int userInputYear) {

int index = binarySearchYearUtil(cars, 0, cars.size()-1, userInputYear);

if(index == -1) {

cout << “No result found” << endl;

}

}

void binarySearchYearIter(vector<Car> cars, int userInputYear) {

int start = 0;

int end = cars.size()-1;

while(start <= end) {

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.year == userInputYear) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return;

} else {

if(start == end) {

break;

}

if(midCar.year > userInputYear) {

end = mid-1;

} else {

start = mid+1;

}

}

}

cout << “No Results found” << endl;

}

int binarySearchMakeUtil(vector<Car> cars, int start, int end, string userInputMake) {

if(start > end) {

return -1;

}

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.make == userInputMake) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return mid;

} else {

if(start == end) {

return -1;

}

if(midCar.make.compare(userInputMake) > 0) {

return binarySearchMakeUtil(cars, start, mid-1, userInputMake);

} else {

return binarySearchMakeUtil(cars, mid+1, end, userInputMake);

}

}

}

void binarySearchMakeRec(vector<Car> cars, string userInputMake) {

int index = binarySearchMakeUtil(cars, 0, cars.size()-1, userInputMake);

if(index == -1) {

cout << “No result found” << endl;

}

}

void binarySearchMakeIter(vector<Car> cars, string userInputMake) {

int start = 0;

int end = cars.size()-1;

while(start <= end) {

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.make == userInputMake) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return;

} else {

if(start == end) {

break;

}

if(midCar.make.compare(userInputMake) > 0) {

end = mid-1;

} else {

start = mid+1;

}

}

}

cout << “No Results found” << endl;

}

int binarySearchModelUtil(vector<Car> cars, int start, int end, string userInputModel) {

if(start > end) {

return -1;

}

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.model == userInputModel) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return mid;

} else {

if(start == end) {

return -1;

}

if(midCar.model.compare(userInputModel) > 0) {

return binarySearchModelUtil(cars, start, mid-1, userInputModel);

} else {

return binarySearchModelUtil(cars, mid+1, end, userInputModel);

}

}

}

void binarySearchModelRec(vector<Car> cars, string userInputModel) {

int index = binarySearchModelUtil(cars, 0, cars.size()-1, userInputModel);

if(index == -1) {

cout << “No result found” << endl;

}

}

void binarySearchModelIter(vector<Car> cars, string userInputModel) {

int start = 0;

int end = cars.size()-1;

while(start <= end) {

int mid = (start + end)/2;

Car midCar = cars[mid];

if(midCar.model == userInputModel) {

cout << “Found at index ” << mid << ” : ” << midCar.make << “, ” << midCar.model << “, ” << midCar.year << endl;

return;

} else {

if(start == end) {

break;

}

if(midCar.model.compare(userInputModel) > 0) {

end = mid-1;

} else {

start = mid+1;

}

}

}

cout << “No Results found” << endl;

}

void printCars(vector<Car> cars) {

cout << “t : YeartMaketModel” << endl;

for(size_t i=0; i < cars.size(); i++) {

Car c = cars[i];

cout << “index ” << (i) << ” : ” << c.year << “, ” << c.make << “, ” << c.model << endl;

}

}

int main (int argc, char *argv[]) {

vector<Car> cars;

string fileName = “vehiclein.txt”; // CHANGE YOUR FILENAME HERE IF REQUIRED

ifstream infile(fileName.c_str());

string line, bar = “|”;

do {

if(bar != “|”) {

break;

}

Car c;

if(getline(infile, line)) {

c.year = atoi(line.c_str());

} else {

break;

}

if(getline(infile, c.make)) {

c.make = trim(c.make);

} else {

break;

}

if(getline(infile, c.model)) {

c.model = trim(c.model);

} else {

break;

}

cars.push_back(c);

// process pair (a,b)

} while (getline(infile, bar));

cout << “File Read: ” << endl;

printCars(cars);

cout << endl << “Sorting by model: ” << endl;

sortByModel(cars);

printCars(cars);

cout << “Searching for model: 328xi” << endl;

cout << “Recursive: “;

binarySearchModelRec(cars, “328xi”);

cout << “Iterative: “;

binarySearchModelIter(cars, “328xi”);

cout << “Searching for model: ABCD” << endl;

cout << “Recursive: “;

binarySearchModelRec(cars, “ABCD”);

cout << “Iterative: “;

binarySearchModelIter(cars, “ABCD”);

cout << endl << “Sorting by make: ” << endl;

sortByMake(cars);

printCars(cars);

cout << “Searching for make: Ford” << endl;

cout << “Recursive: “;

binarySearchMakeRec(cars, “Ford”);

cout << “Iterative: “;

binarySearchMakeIter(cars, “Ford”);

cout << “Searching for make: ABCD” << endl;

cout << “Recursive: “;

binarySearchMakeRec(cars, “ABCD”);

cout << “Iterative: “;

binarySearchMakeIter(cars, “ABCD”);

cout << endl << “Sorting by year: ” << endl;

sortByYear(cars);

printCars(cars);

cout << “Searching for year: 2014” << endl;

cout << “Recursive: “;

binarySearchYearRec(cars, 2014);

cout << “Iterative: “;

binarySearchYearIter(cars, 2014);

cout << “Searching for year: 2019” << endl;

cout << “Recursive: “;

binarySearchYearRec(cars, 2019);

cout << “Iterative: “;

binarySearchYearIter(cars, 2019);

}

vehiclein.txt:
2010
Ford
Escape
|
2014
BMW
328xi
|
2014
BMW
428xi
|
2012
Ford
Fusion SE
|
2014
Lamborghini
Gallardo
|

=============================================Question & Answer: Good morning , please would you help me with this progam in c++:..... 1

Still stressed from student homework?
Get quality assistance from academic writers!