Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 16, Problem 8PC
Program Plan Intro

Modification of “SearchableVector” class

Program plan:

SimpleVector.h:

  • Include the required header files in the program.
  • Define the template class “SimpleVector”:
    • Declare the required class data members and exception function under “private” access specifier.
    • Declare the required class member functions and class constructor under “public” access specifier.
  • Define the “SimpleVector” function:
    • Get the array size and allocate the memory for corresponding array size
    • If allocation gets fail, then call the “memError()” function.
    • Else initialize the array elements.
  • Define the copy constructor of the “SimpleVector” function:
    • Copy the array size and allocate the memory for corresponding array size
    • If allocation gets fail, then call the “memError()” function.
    • Else copy the array elements.
  • Define the “~SimpleVector” class destructor:
    • Delete the allocated memory for the array.
  • Define the “getElement” function:
    • Get the array subscript and check it is in the array range.
    • If not then call the exception function.
    • Return the subscript.
  • Define the operator overload for “[]” operator
    • Get the array subscript and check it is in the array range.
    • If not then call the exception function.
    • Return the subscript.
  • Define the “push_back()” function:
    • Get the element which is going to insert.
    • Create a new array and allocate the memory for the array which is greater than 1 from an old array.
    • Copy the elements from old array to new array.
    • Insert the new element at the last of the array.
    • Delete the old array and make the pointer points the new array
    • Finally adjust the array size.
  • Define the “pop_back()” function:
    • Save the last element which is going to delete.
    • Create a new array and allocate the memory for the array which is lesser than 1 from an old array.
    • Copy the elements from old array to new array.
    • Delete the old array and make the pointer points the new array
    • Finally adjust the array size.
    • Return the deleted value.

SearchableVector.h:

  • Include the required header files in the program.
  • Define the template class “SearchableVector”, which is derived from the above class template “SimpleVector”:
    • Declare the required class member functions and class constructor under “public” access specifier.
  • Define the copy constructor of the “SearchableVector” function:
    • Call the base class copy constructor for copying the array size and allocate the memory for corresponding array size.
    • Copy the array elements.
  • Define the function “findItem”
    • Declare the required variables in the function.
    • Get the element from the function call.
    • Using while loop
      • Find the middle element of the array.
      • Check the middle element is the searching element.
        • If so, then return the array subscript.
      • Else, check the middle element is greater than the searching element.
        • If so, decrease the last index value.
      • Else, increase the starting index value.
    • If the element is not found, then return -1 as the array subscript.

Main.cpp:

  • Include the required header files in the program.
  • Create an object for the class “SearchableVector” for integer type.
  • Create an object for the class “SearchableVector” for double type.
  • Use for loop to iterate array elements
    • Insert the integer value on integer array.
    • Insert the double value on double array.
  • Using for loop, display an integer array on the output screen.
  • Using for loop, display the double array on the output screen.
  • Call the function “findItem” using the searching element which is integer.
  • Compare the result of the function and display the message according to the condition.
  • Do the same again using double value.

Blurred answer
Students have asked these similar questions
CLASSES, DYNAMIC ARRAYS AND POINTERS   Define a class called textLines that will be used to store a list of lines of text (each line can be specified as a string). Use a dynamic array to store the list.  In addition, you should have a private data member that specifies the length of the list.  Create a constructor that takes a file name as parameter, and fills up the list with lines from the file.  Make sure that you set the dynamic array to expand large enough to hold all the lines from the file.  Also, create a constructor that takes an integer parameter that sets the size of an empty list. in C++   Write member functions to: remove and return the last line from the list add a new line onto the end of the list, if there is room for it, otherwise print a message and expand the array empty the entire list return the number of lines still on the list take two lists and return one combined list (with no duplicates) copy constructor to support deep copying remember the destructor!
Computer Science //iterator() creates a new Iterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::Iterator DoublyLinkedList<ValueType>::iterator(){//return iterator(head);} //constIterator() creates a new ConstIterator over this list. It will//initially be referring to the first value in the list, unless the//list is empty, in which case it will be considered both "past start"//and "past end". template <typename ValueType>typename DoublyLinkedList<ValueType>::ConstIterator DoublyLinkedList<ValueType>::constIterator() const{//return constIterator(head);} //Initializes a newly-constructed IteratorBase to operate on//the given list. It will initially be referring to the first//value in the list, unless the list is empty, in which case//it will be…
Data Structure & Algorithum java program Do the following: 1) Add a constructor to the class "LList" that creates a list from a given array of objects.2) Add a method "addAll" to the "LList" class that adds an array of items to the end of the list. The header of the method is as follows, where "T" is the generic type of the objects in the list. 3) Write a Test/Driver program that thoroughly tests all the methods in the class "LList".

Chapter 16 Solutions

Starting Out with C++: Early Objects (9th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning