Question 2: Question 2 is a separate program from Question 1 The following short
ID: 3723422 • Letter: Q
Question
Question 2: Question 2 is a separate program from Question 1 The following short main function for Question 2 is provided on Blackboard int main() double prices[5] string, animal[5] { { 150.00, 200.00, 345.00, 999.99, 44 .00); "Dog", "cat", "Wolf", "Lion", "Fox" }; tinestwa(prices, 5); animal, 5)> return ; 3; Notice that the first timesTwo function call passes an array of type double and the size of the array. The second timesTwo function call passes an array of type string and the size of the afra Instead of writing two overloaded functions, write a function template, so the same single timesTwo function can process and print both types. The string output will be concatenated with itself, and the double output will be each value plus itself. Sample Output: Select CAUsersl latelSourcelReposlLab14 q21 DebugLab14 q2.exe 300 90 1999.98 ogDog atCat olfWolf LionLion FoxFoxExplanation / Answer
#include <iostream>
using namespace std;
template <class T>
void timesTwo(T price[], int n) {
for(int i=0;i<n;i++) {
cout<<price[i]+price[i]<<endl;
}
}
int main() {
double price[5] = {150.00,200.00,345.00,999.99,44.00};
string animal[5] = {"Dog","Cat","Wolf","Lion","Fox"};
timesTwo(price, 5);
timesTwo(animal, 5);
return 0;
}
Output:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.