Professional Documents
Culture Documents
QUESTION 1
#include <iostream>
using namespace std;
int main()
{
cout << myMax<int>(3, 7) << endl; // Call myMax
for int
cout << myMax<double>(3.0, 7.0)
<< endl; // call myMax for double
cout << myMax<char>('g', 'e')
<< endl; // call myMax for char
return 0;
}
QUESTION 2
#include <iostream>
using namespace std;
public:
Calculator(T n1, T n2) {
num1 = n1;
num2 = n2;
}
void displayResult() {
cout << "Numbers: " << num1 << " and " << num2
<< "." << endl;
cout << num1 << " + " << num2 << " = " << add() <<
endl;
cout << num1 << " - " << num2 << " = " <<
subtract() << endl;
cout << num1 << " * " << num2 << " = " <<
multiply() << endl;
cout << num1 << " / " << num2 << " = " << divide()
<< endl;
}
int main() {
Calculator<int> intCalc(2, 1);
Calculator<float> floatCalc(2.4, 1.2);
return 0;
}
QUESTION 3
#include <iostream>
using namespace std;
int main()
{
int i1 = 6, i2 = 3;
float f1 = 7.2, f2 = 4.5;
char c1 = 'p', c2 = 'x';
Swap(i1, i2);
Swap(f1, f2);
Swap(c1, c2);
return 0;
}
QUESTION 4
#include <iostream>
using namespace std;
i = 2;
x = 2.2;
y = 2.2;
ii = square<int>(i);
cout << i << ": " << ii << endl;
xx = square<float>(x);
cout << x << ": " << xx << endl;
yy = square<double>(y);
cout << y << ": " << yy << endl;
yy = square(y);
cout << y << ": " << yy << endl;
return 0;
}
QUESTION 5
#include <iostream>
using namespace std;
i = 2;
x = 2.2;
y = 2.2;
ii = square<int>(i);
cout << i << ": " << ii << endl;
xx = square<float>(x);
cout << x << ": " << xx << endl;
yy = square<double>(y);
cout << y << ": " << yy << endl;
yy = square(y);
cout << y << ": " << yy << endl;
return 0;
}
QUESTION 6
#include <iostream>
using namespace std;
QUESTION 8
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
large=FindLarge(num1,num2,num3);
cout<<"\n Largest Number is : "<<large;
return 0;
}
QUESTION 10
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
// Function Call for side as
// 9 i.e., integer
square(9);
QUESTION 11
#include<iostream>
using namespace std;
int a = 0, b = size - 1;
int mid;
while ((b - a) > 1) {
mid = (a + b) / 2;
if ((ar[a] - a) != (ar[mid] - mid))
b = mid;
else if ((ar[b] - b) != (ar[mid] - mid))
a = mid;
}
return (ar[a] + 1);
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5, 6, 8 };
int size = sizeof(ar) / sizeof(ar[0]);
cout << "Missing number:" << search(ar, size);
}