It\'s for C++ first question Write a program with template to achieve the follow
ID: 3578719 • Letter: I
Question
It's for C++ first question Write a program with template to achieve the following functionalities. Firstly, the program adds two values. Then the program adds three values. At last, the program adds four values. Enter two values: 2.4 Their sum is 6 Enter three values:1.1 1.1 1.1 Their sum is 3.3 Enter four values: 1 2 3 4 Their sum is 10 Press any key to continue. .. Write a program to display the followings with the letter between the *s as the initial of your first name. For example, if the letter is T. the program should display the followings. The following program includes one line of code: std::coutExplanation / Answer
#include <iostream>
using namespace std;
// template function
template <class T>
T sum_2(T n1, T n2)
{
return (n1+n2);
}
template <class T1>
T1 sum_3(T1 n1, T1 n2, T1 n3)
{
return (n1+n2+n3);
}
template <class T2>
T2 sum_4(T2 p1, T2 p2, T2 p3, T2 p4)
{
return (p1+p2+p3+p4);
}
int main()
{
int i1, i2;
float f1, f2,f3;
char c1, c2,c3,c4;
cout << "Enter two integers: ";
cin >> i1 >> i2;
cout <<" Total is "<< sum_2(i1, i2) << endl;
cout << " Enter three values: ";
cin >> f1 >> f2 >> f3;
cout <<" Total is "<< sum_3(f1, f2, f3) << endl;
cout << " Enter four characters: ";
cin >> c1 >> c2 >>c3 >>c4;
cout <<" Total is "<< sum_4(c1, c2, c3, c4)<< endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.