Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 16, Problem 9PC
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
Salesforce Assignment: You are working in company as a junior developer and the manger assign you a task to create the test class of trigger code of the following code will cover all the use case like insert, update, delete. The code is as follows: public class TriggerBasicAssignmentTriggerHelper ( // This method is used to update the vlaue of field C public static void getFiledvalue(List listofValues, Map mapofCoustomobject ){ for (Trigger_Basic_Assignments_ct :listofvalues){ if(mapofCoustomobject == null || t.Field_A_c != mapofCoustomobject.get(t.Id). Field_A_C || t.Field_8_c != mapofCoustomobject.get(t.I if(t.Operator_ '+') t.Field C_c = t.Field_A_c + t.Field_B_c; else if(t.Operator_c *') t. Field C_c = t.Field A_* t.Field_8_c; else if(t.Operator_c == /' && t.Field B_c != 0) t.Field C_c = t.Field A_c / t.Field_B_c; else t.Field C_c = t.Field_A_c - t.Field B_c; I need the test class only.
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…
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!

Chapter 16 Solutions

Starting Out with C++ from Control Structures to Objects (9th Edition)

Ch. 16.4 - Prob. 16.11CPCh. 16 - Prob. 1RQECh. 16 - Prob. 2RQECh. 16 - Prob. 3RQECh. 16 - Prob. 4RQECh. 16 - What is unwinding the stack?Ch. 16 - What happens if an exception is thrown by a classs...Ch. 16 - How do you prevent a program from halting when the...Ch. 16 - Why is it more convenient to write a function...Ch. 16 - Why must you be careful when writing a function...Ch. 16 - The line containing a throw statement is known as...Ch. 16 - Prob. 11RQECh. 16 - Prob. 12RQECh. 16 - Prob. 13RQECh. 16 - The beginning of a template is marked by a(n)...Ch. 16 - Prob. 15RQECh. 16 - Prob. 16RQECh. 16 - Write a function that searches a numeric array for...Ch. 16 - Write a function that dynamically allocates a...Ch. 16 - Make the function you wrote in Question 17 a...Ch. 16 - Write a template for a function that displays the...Ch. 16 - Prob. 21RQECh. 16 - Prob. 22RQECh. 16 - Prob. 23RQECh. 16 - Prob. 24RQECh. 16 - T F All type parameters defined in a function...Ch. 16 - Prob. 26RQECh. 16 - T F A class object passed to a function template...Ch. 16 - Prob. 28RQECh. 16 - Prob. 29RQECh. 16 - Prob. 30RQECh. 16 - Prob. 31RQECh. 16 - T F A class template may not be derived from...Ch. 16 - T F A class template may not be used as a base...Ch. 16 - Prob. 34RQECh. 16 - Prob. 35RQECh. 16 - try { quotient = divide(num1, num2); } cout The...Ch. 16 - template class T T square(T number) { return T T;...Ch. 16 - template class T int square(int number) { return...Ch. 16 - Prob. 39RQECh. 16 - Assume the following definition appears in a...Ch. 16 - Assume the following statement appears in a...Ch. 16 - Prob. 1PCCh. 16 - Prob. 2PCCh. 16 - Prob. 3PCCh. 16 - Prob. 4PCCh. 16 - Prob. 5PCCh. 16 - IntArray Class Exception Chapter 14 presented an...Ch. 16 - TestScores Class Write a class named TestScores....Ch. 16 - Prob. 8PCCh. 16 - Prob. 9PCCh. 16 - SortableVector Class Template Write a class...Ch. 16 - Inheritance Modification Assuming you have...Ch. 16 - Prob. 12PCCh. 16 - Prob. 13PC
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education