Transform the find function of Question 4 into a function template.
Here is the program used to test your template, followed by the output of that program:
#include <iostream>
#include <string>
#include "find.h"
using namespace std;
#define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
int main() {
cout << "int"
<< endl;
cout << "---"
<< endl;
int arr1[] = {1, 2, 3,
4, 5, 6, 7, 8, 9};
cout << "3 is at
location " << find(arr1, NUM_ELEMENTS(arr1), 3) <<
endl;
cout << "10 is at
location " << find(arr1, NUM_ELEMENTS(arr1), 10) <<
endl;
cout << "string"
<< endl;
cout << "-----"
<< endl;
string arr2[] = {"ABC",
"DEF", "GHI", "JKL"};
cout << "JKL is at
location " << find(arr2, NUM_ELEMENTS(arr2), string("JKL"))
<< endl;
cout << "FGH is at
location " << find(arr2, NUM_ELEMENTS(arr2), string("FGH"))
<< endl;
return 0;
}
------------------
int
---
3 is at location 2
10 is at location -1
string
-----
JKL is at location 3
FGH is at location -1
Template Function Code included:-
#include <iostream>
#include <string>
//#include "find.h"
using namespace std;
#define NUM_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
//Transformed find function into function template
template <class T>
int find(T arr[],long unsigned int n, T y)
{
for(int i =0 ;i<n;++i)
{
if(arr[i]==y)
return i;
}
return -1;
}
int main() {
cout << "int" << endl;
cout << "---" << endl;
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << "3 is at location " << find(arr1, NUM_ELEMENTS(arr1), 3) << endl;
cout << "10 is at location " << find(arr1, NUM_ELEMENTS(arr1), 10) << endl;
cout << "string" << endl;
cout << "-----" << endl;
string arr2[] = {"ABC", "DEF", "GHI", "JKL"};
cout << "JKL is at location " << find(arr2, NUM_ELEMENTS(arr2), string("JKL")) << endl;
cout << "FGH is at location " << find(arr2, NUM_ELEMENTS(arr2), string("FGH")) << endl;
return 0;
}
Output:-

Template Function Code Picture:-

Transform the find function of Question 4 into a function template. Here is the program used...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
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 )...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...
Can someone explain how this C++ program runs? A line by line explanation/commentation would be great, as well as the purpose of the program and functions/classes involved. #include <iostream> #include <vector> using namespace std; // template function vector<int> removeEvenIndexedVals(vector<int> vec); // main int main() { static const int arr[] = { 2,5,7,9,1,3,6 }; vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0])); // call function vec = removeEvenIndexedVals(vec); // print cout << "Displaying the Vector Elements:"...
For this part of the lab make a template out of the myMax function and test it on different data types. myMaxTemplate.cpp : /**************************************************** * * FileName: maxTemplate.cpp * Purpose: Demonstrate the use of function templates * ********************************************************/ #include <iostream> #include <string> using namespace std; //Make a template out of the prototype int myMax(int one, int two); int main() { int i_one = 3, i_two = 5; cout << "The max of " << i_one << " and " <<...
Find the problems with this program #include <iostream> using namespace std; struct Student { string name; int grade; } int main() { struct Student mary; Mary:name = "Mary"; Mary:grade = 100; cout << mary:name " got a " << mary:grade << endl; return 0; }
4. Write a function that returns a value to the main program illustrated below. In the function, the variable passed by the main program needs to be evaluated using a switch statement. The value returned is determined by the case that it matches. (10 points) If value is: return 10 return 20 3 return 30 Anything else, return 0 Main program: #include <iostream> using namespace std; int findValue (int); int main) { Int numb, retvalue cout << "In Enter a...
Program a clean_string function that combines both the convert_lower and remove_punct functionality in one function. In other words, take a string as a parameter and return a clean version of the string that has no punctuation, no spaces and is all lower case. #include <iostream> #include <string> using namespace std; // TODO: Add your clean_string function here int main() { string line; getline(cin, line); while(line != "quit") { cout << clean_string(line) << ": " << line << endl;...
previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() { //create Animal object Animal animal("Dog","Mammal",4); cout<<"Animal object details"<<endl; cout<<"Name: "<<animal.getName()<<endl; cout<<"Type: "<<animal.getType()<<endl; cout<<"# of Legs: "<<animal.getLegs()<<endl; cout<<endl<<endl; //create Cat object Cat cat("byssinian cat","carnivorous mammal",4,"short...
Question 19 4 pts Using the program below, please complete the program by adding 2 functions as follow: 1. A function named getAverage that calculates and returns the Average. 2. A function named getMaximum that returns the maximum of the three numbers. Make sure to use the proper data types for all variables and functions. #include <iostream> using namespace std; //function getAverage //function getMaximum int main(int argc, char** argv) { int x, y, z; cout << "Enter three whole numbers:...