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
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
ADVERTISEMENT