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

Recursion vs Iterative functions. For this assignment you will make a Recursion

ID: 3785452 • Letter: R

Question

Recursion vs Iterative functions.

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

a) Implement the recursive binSearchRec algorithm presented in this chapter, starting on page 68 for a vector of vehicles. Sort first by year then take that list and sort it by make and then take that list and sort it by model. Read in the vehicles from a vehicle file (vehiclein.txt) 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 should produce the same results as your recursive function.

vehiclein.txt file:

2010

Ford

Escape

|

2014

BMW

328xi

|

2014

BMW

428xi

|

2012

Ford

Fusion SE

|

2014

Lamborghini

Gallardo

|

***********************************************************

The coding language should be in c++. Thank you!

CHAPTER 2 Recursion: The Mirrors 68 Question 4 In the previous definition of writeArrayBackward, why does the bo occur when the value of first exceeds the value of last Question 5 a recursive function that computes and returns the product of a 2 1 real numbers in an array Question 6 show how that you wrote for the previous question satisfi the function the properties of a recursive function Question write a recursive function that computes and returns the product of 7 integers in the array an Array[first last 4.2 Searching a sorted Array: The Binary Search Searching is an that occurs frequently. Often, searches are for a particular important task en n an array. We now will examine a few searching problems that have recursive solutions. Our goal is to develop further your understanding of recursion. This chapter began with an intuitive approach to a binary search algorithm by presenting high way to find a word in a dictionary. We now develop this algorithm fully and illustrate some important programming issues. ary search Recall the earlier solution to the dictionary problem: ers one of its problems at search (aDictionary: Dictionary, word string step if (aDictionary is one page in size) Scan the page for word else e middle Determine which ha of aDictionary contains word if (word is in the first half of aDictionary) search (first half of aDictionary, word) else search (second half of aDictionary, word) the Now alter the problem slightly by searching an array anArray of integers for a given value, target. The array, like the dictionary, must be sorted, or else a binary search is not applicable. Hence, assume that anArray[0] s anArray[1] s Array[2] s where size is s anArray [size 11 the size of the array. A level binary search for the array problem is binarysearch (anArray: ArrayType, target: Value Type if (anArray is of size 1) Determine if anArray's value is equal to target else Find midpoint of Determine which half of an Array contains target (target is in the first half of anArray) binary search (first half of anArray, target) else binary search (second half of anArray, target)

Explanation / Answer

#include <iostream> // library that contain basic input/output functions

#include <fstream> // library that contains file input/output functions

using namespace std;

int main()

{

int ocurrences_count = 0;

char word[20]; //this array will save user input

int array_size = 1024; // define the size of character array

            char * array = new char[array_size]; // allocating an array of 1kb

            int position = 0; //this will be used incremently to fill characters in the array

//prompting user to enter a word to be search in the file

cout << "Please enter the word to search in file : ";

cin.getline(word,19); //reading user input of max 19 characters because our word array size in 20

int word_size = 0;

//this loop is calculating the length of input word

for(int i = 0; word[i] != ''; i++)

{

    word_size++;

}

  ifstream fin("vehicle.txt"); //opening an input stream for file test.txt

            /*checking whether file could be opened or not. If file does not exist or don't have read permissions, file

stream could not be opened.*/

if(fin.is_open())

            {

    //file opened successfully so we are here

    cout << "File Opened successfully!!!. Reading data from file into array" << endl;

    //this loop run until end of file (eof) does not occur

                        while(!fin.eof() && position < array_size)

                        {

                                    fin.get(array[position]); //reading one character from file to array

                                    position++;

                        }

                        array[position-1] = ''; //placing character array terminating character

    //this loop is searching for the word in the array

                        for(int i = 0; array[i] != ''; i++)

                        {

                                    for(int j = 0; word[j] != '' && j < 20 ; j++)

      {

        if(array[i] != word[j])

        {

          break;

        }

        else

        {

          i++;

          if(word[j+1] == '')

          {

            cout << "Word Found in File at position " << (i-word_size) << endl;

            ocurrences_count++;

          }

}

      }

                        }

    cout << "Total occurences found : " << ocurrences_count << endl;

            }

            else //file could not be opened

            {

                        cout << "File could not be opened." << endl;

            }

            return 0;

}

iterative and recursive:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote