Question

Transform the find function of Question 4 into a function template. Here is the program used...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #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:-

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 Picture:-

#include <iostream> #include <string> //#include find.h using namespace std; #define NUM ELEMENTS(a) (sizeof(a) / sizeof(a[

Add a comment
Know the answer?
Add Answer to:
Transform the find function of Question 4 into a function template. Here is the program used...
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
  • I have to type and explain in class each code in every detail filled with //...

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

    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 )...

  • Can someone explain how this C++ program runs? A line by line explanation/commentation would be great,...

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

    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;...

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

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

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • Question 19 4 pts Using the program below, please complete the program by adding 2 functions...

    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:...

  • 1. In ANSII standard C++, there is no library function to convert an integer to a...

    1. In ANSII standard C++, there is no library function to convert an integer to a string. Your program should develop such a function. In other words complete the following program using your itos function. (Use of other C functions is prohibitted) // this program will read in an integer and convert it to a string #include <iostream> #include <cstdlib> #include <string> using namespace std; string itos(int n) { } int main(int argc, char* argv[]){ int n = atoi(argv[1]); cout...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

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