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;
}
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:
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...
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...
(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. #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 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 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 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...
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;...
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...
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...
C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...