Question

Write a template function maxn() that takes as its arguments an array of items of type...

Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array.

The number of elements should take the default value of 10.

The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one tied for longest.) (See http://www.cplusplus.com/reference/string/string/).

Test the function with an array of ten ints (using the default argument) and an array of four chars. Test the specialization with an array of five strings.

Deliverables:

  • A program called functions.cpp, which contains
  • A template function maxn() as described above
  • A specialization of maxn() which takes an array of strings as described above
  • A main function that
    • declares an array of 10 ints, an array of 4 chars, and an array of 5 strings
    • passes each one to maxn() (with or without default arguments as appropriate)
    • prints the result of each call to maxn() (the largest element of each array) to the console

Homework Answers

Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

#include <iostream>
using namespace std;

template <class T>
T maxn(T arr[], int n = 10){
T largest = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] > largest)
largest = arr[i];
}
return largest;
}

//specialization for strings
template <>
string maxn(string arr[], int n ){
string largest = arr[0];
for(int i = 1; i < n; i++){
if(arr[i].length() > largest.length())
largest = arr[i];
}
return largest;
}

int main(){
int nums[10] = {2, 4, 1, 5, 9, 1, 0, 6, 8, 7};
char chars[4] = {'N', 'I', 'C', 'E'};
string strs[5] = {"hello", "good", "excellent", "morning", "beautiful"};

cout << "Largest int = " << maxn(nums) << endl;
cout << "Largest char = " << maxn(chars, 4) << endl;
cout << "Largest string = " << maxn(strs, 5) << endl;
}

Know the answer?
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
Similar Questions
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Write a function that takes in 3 arguments: a sorted array, size of the array, and...
Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question.
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
Write a function that takes an array of integers and its size as parameters and prints...
Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by...
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT