Question

PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

PLEASE HELP ME WITH THIS HOMEWORK.

Create a template function that, given an array of elements of any template type, deletes an element on a given position.

Submit in the standard format in Dropbox Hw 4 before the deadline.

Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit)

#include <iostream>
using namespace std;

// A template function to implement element insertion on given position in array.

template <class T>
void insert(T a[], int &n,T el, int place ) {
if (place >= n )
cout << "Insertion position out of range";
else{
   for (int i = n-1; i >= place; i--)
a[i+1] = a[i];
a[place -1] = el;
n++;
}

}

// Driver Code
int main() {
   int a[5] = {10, 50, 30, 40, 20};
   int n = sizeof(a) / sizeof(a[0]);

   // calls template function
   insert(a, n, 99, 3);

   cout << " Array after insertion: ";
   for (int i = 0; i < n; i++)
       cout << a[i] << " ";
   cout << endl;

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code:

#include <iostream>
using namespace std;

// A template function to implement element insertion on given position in array.
template <class T>
void insert(T a[], int &n,T el, int place )
{
   if (place >= n )
   cout << "Insertion position out of range";
   else
   {
       n++;
        for (int i = n-1; i >= place-1; i--)
       {
           a[i+1] = a[i];
       }
       a[place -1] = el;
   }
}

// A template function to implement element deletion on given position in array.
template <class T>
void Delete(T a[], int &n, int place )
{
   if (place >= n )
   cout << "Deletion position out of range";
   else
   {
        for (int i = place-1; i<= n; i++)
       {
           a[i] = a[i+1];
       }
       n--;
   }
}

// Driver Code
int main()
{
   int a[5] = {10, 50, 30, 40, 20};
   int n = sizeof(a)/sizeof(a[0]);

   // calls template function
   insert <int> (a, n, 99, 3);
   //printing array after insertion
   cout << " Array after insertion: ";
   for (int i = 0; i < n; i++)
   {
    cout << a[i] << " ";
      
   }
   cout << endl;
  
   // calls template function delete
   Delete <int> (a, n, 3);
   //printing array after insertion
   cout << " Array after deletion: ";
   for (int i = 0; i < n; i++)
   {
    cout << a[i] << " ";
      
   }
   cout << endl;
  
  
   char b[5] = {'a', 'c', 'd', 'e', 'f'};
   int n1 = sizeof(b)/sizeof(b[0]);
  
   cout << " Initial character array : ";
   for (int i = 0; i < n; i++)
   {
    cout << b[i] << " ";
      
   }
   cout << endl;
   insert <char> (b, n1, 'b', 2);

   cout << " Array after inserting 'b' at position 2: ";
   for (int i = 0; i < n; i++)
   {
    cout << b[i] << " ";
      
   }
   cout << endl;
  
   //calling template function delete for character array
   Delete <char> (b, n, 4);
   //printing array after insertion
   cout << " Array after deletion at position 4: ";
   for (int i = 0; i < n; i++)
   {
    cout << b[i] << " ";
      
   }
   cout << endl;


   return 0;
}

Output:

Ec prog devltemplate func.exe Array after insertion: 10 50 99 30 40 20 Array after deletion: 10 50 30 40 20 Initial character

Add a comment
Know the answer?
Add Answer to:
PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • Please help me finish my code. Before the final pause in the main function, create an...

    Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...

    Must be written in C 89 Mode Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array: int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos); The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find...

    Part-1: find the longest block (subsequence of elements with same value) in an array. Part-2: find all subsequences in an array of int that add up to a given sum. */ #include <iostream> #include <cstdlib> using namespace std; void printSeq(int* a, int s, int e); // -------------------------------------------- functions to be implemented by you void longestBlock(const int* a, int n, int& s, int& e) { // s = start index of the block // e = end index of the block...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT