Question

In: Computer Science

Data Structure 6. Write a recursive routine that will have an integer array and an index...

Data Structure

6. Write a recursive routine that will have an integer array and an index as parameters and will return the count of all odd integers. You may assume that the index starts out at the END of the array.

12. Write the implementation function for ArrayBag in C++, called bagLess, that takes an ItemType of item as parameters and use it count items in the current bag that are less than the item (hint: use toVector)

13. Write a sum function in C++ for LinkedBag of integers (must use pointers to traverse the linked list) that will return a sum of all values.

15. Write a Grammar that starts with a letter or a group of letters (assume uppercase), has an '?' or '!', and ends with a letter or a group of letters (uppercase)

16.

Given: e + f / (a – b) * c

Write out the prefix form

Solutions

Expert Solution

12)mplementation function for ArrayBag in C++

#include "ArrayBag.h"

#include <cstddef>

  template<class ItemType>

  ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)

  {

  } // end default constructor

  template<class ItemType>

int ArrayBag<ItemType>::getCurrentSize() const

{

return itemCount;

} // end getCurrentSize

  template<class ItemType>

  bool ArrayBag<ItemType>::isEmpty() const

  {

   return itemCount == 0;

  } // end isEmpty

  template<class ItemType>

  bool ArrayBag<ItemType>::add(const ItemType& newEntry)

  {

   bool hasRoomToAdd = (itemCount < maxItems);

   if (hasRoomToAdd)

   {

   items[itemCount] = newEntry;

   itemCount++;

   } // end if

  

   return hasRoomToAdd;

  } // end add

  template<class ItemType>

  bool ArrayBag<ItemType>::remove(const ItemType& anEntry)

  {

   int locatedIndex = getIndexOf(anEntry, 0);

   bool canRemoveItem = !isEmpty() && (locatedIndex > -1);

   if (canRemoveItem)

   {

   itemCount--;

   items[locatedIndex] = items[itemCount];

   } // end if

     

   return canRemoveItem;

  } // end remove

  template<class ItemType>

  void ArrayBag<ItemType>::clear()

  {

   itemCount = 0;

  } // end clear

  template<class ItemType>

  bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const

  {

   return getIndexOf(anEntry, 0) > -1;

  } // end contains

  template<class ItemType>

  int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const

  {

   return countFrequency(anEntry, 0);

  } // end getFrequencyOf

  template<class ItemType>

  vector<ItemType> ArrayBag<ItemType>::toVector() const

  {

   vector<ItemType> bagContents;

   for (int i = 0; i < itemCount; i++)

   bagContents.push_back(items[i]);

     

   return bagContents;

  } // end toVector

  // private

  template<class ItemType>

  int ArrayBag<ItemType>::countFrequency(const ItemType& anEntry, int searchIndex) const

  {

   int frequency = 0;

   if (searchIndex < itemCount)

   {

   if (items[searchIndex] == anEntry)

   {

   frequency = 1 + countFrequency(anEntry, searchIndex + 1);

   }

   else

   {

   frequency = countFrequency(anEntry, searchIndex + 1);

   } // end if

   } // end if

   return frequency;

} // end countFrequency

// private

template<class ItemType>

int ArrayBag<ItemType>::getIndexOf(const ItemType& target, int searchIndex) const

{

int result = -1;

if (searchIndex < itemCount)

{

if (items[searchIndex] == target)

{

result = searchIndex;

}

else

{

result = getIndexOf(target, searchIndex + 1);

} // end if

} // end if

  

return result;

}

13)sum function in C++ for LinkedBag of integers

class Node

{
public:
int data;
Node* next;
};
  
/* Function to create a
new node with given data */
Node* newNode(int data)
{
Node* new_node = new Node();
new_node->data = data;
new_node->next = NULL;
return new_node;
}
  
/* Function to insert a node at the
beginning of the Singly Linked List */
void push(Node** head_ref, int new_data)
{
/* allocate node */
Node* new_node = newNode(new_data);
  
/* link the old list off the new node */
new_node->next = (*head_ref);
  
/* move the head to point to the new node */
(*head_ref) = new_node;
}
  
/* Adds contents of two linked lists and
return the head node of resultant list */
Node* addTwoLists(Node* first, Node* second)
{
  
// res is head node of the resultant list
Node* res = NULL;
Node *temp, *prev = NULL;
int carry = 0, sum;
  
// while both lists exist
while (first != NULL
|| second != NULL) {
// Calculate value of next
// digit in resultant list.
// The next digit is sum of
// following things
// (i) Carry
// (ii) Next digit of first
// list (if there is a next digit)
// (ii) Next digit of second
// list (if there is a next digit)
sum = carry + (first ? first->data : 0)
+ (second ? second->data : 0);
  
// update carry for next calulation
carry = (sum >= 10) ? 1 : 0;
  
// update sum if it is greater than 10
sum = sum % 10;
  
// Create a new node with sum as data
temp = newNode(sum);
  
// if this is the first node then
// set it as head of the resultant list
if (res == NULL)
res = temp;
  
// If this is not the first
// node then connect it to the rest.
else
prev->next = temp;
  
// Set prev for next insertion
prev = temp;
  
// Move first and second
// pointers to next nodes
if (first)
first = first->next;
if (second)
second = second->next;
}
  
if (carry > 0)
temp->next = newNode(carry);
  
// return head of the resultant list
return res;
}
  
// A utility function to print a linked list
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << endl;
}
  
/* Driver code */
int main(void)
{
Node* res = NULL;
Node* first = NULL;
Node* second = NULL;
  
// create first list 7->5->9->4->6
push(&first, 6);
push(&first, 4);
push(&first, 9);
push(&first, 5);
push(&first, 7);
printf("First List is ");
printList(first);
  
// create second list 8->4
push(&second, 4);
push(&second, 8);
cout << "Second List is ";
printList(second);
  
// Add the two lists and see result
res = addTwoLists(first, second);
cout << "Resultant list is ";
printList(res);
  
return 0;
}

15)where you want to go? ,oh! The Historic University Town

16)PREFIX : + e / f (a-b) * c


Related Solutions

What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list...
Data Structures ( Recursion ) Assignment Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only. The method should receive only one parameter (the array list) The base case is when the array list has only one element, just remove that, otherwise, you need to remove the first one and the last one then call the method, then you need to add them again after...
it should be c++ data structure Write a recursive function that draws the following shape. X...
it should be c++ data structure Write a recursive function that draws the following shape. X XXX XXXXX XXXXXXX XXXXXXXXX XXXXXXXXX XXXXXXX XXXXX XXX X The length of the longest row in the shape and the shape's character are function parameters. In above shape the longest row is 9 and the pattern's character is "X”.
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data)...
Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data) of the class (note - fields of the objects should be marked as private)” INITIAL_SIZE: the initial size of the array = 10; _innerArr: an array of integers _growthFactor: the factor by which to resize the array with to become bigger. _lastIndex: an index on the last element in the array. The class will have the constructor public DynArr() that initialzes an array of...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Write a recursive function in C++ that creates a copy of an array of linked lists....
Write a recursive function in C++ that creates a copy of an array of linked lists. Assuming: struct node { int data; node * next; }; class arrayList { public: arrayList(); ~arrayList(); private: node ** head; int size; //(this can equal 10) }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT