Lab Task Name Sabihudin Sap Id 70067968 Section (T)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

LAB TASK

NAME SABIHUDIN

SAP ID 70067968

SECTION (T)

QUESTION 2

Write a c++ program to find maximum of two data items using function template

#include<iostream>
using namespace std;
template<class T>
T getMaximun(T x, T y) {
if (x > y)
return x;
else
return y;
}
int main() {
int a, b, i;
float c, d, j;
cout << "Function Template Programs : Get Maximum Number \n";
cout << "Enter A,B values(integer):";
cin >> a>>b;
i = getMaximun<int>(a, b);
cout << "Result Max Int : " << i;
cout << "\n\nEnter C,D values(float):";
cin >> c>>d;
j = getMaximun<float>(c, d);
cout << "Result Max Float : " << j;
}
QUESTION 3
Write a class template to represent a generic vector. Include member functions to perform the following
task a. To create a vector b. Sort the elements in ascending order c. Display the vector

#include<iostream>
using namespace std;
template<class T>
class vector
{
T *v;
int size;
public:
vector(int m)
{
v=new T[size=m];
for(int i=0;i<size;i++)
v[i]=0;
}
void create()
{
for(int i=0;i<size;i++)
{
cout<<"v["<<i<<"] = ";
cin>>v[i];
}
}
void modify()
{
int pos;
cout<<"enter position to make changes :";
cin>>pos;
cout<<"Enter new Value :";
cin>>v[pos];
}
void display()
{
int i;
cout<<"(";
for(i=0;i<size-1;i++)
{
cout<<v[i]<<",";
}
cout<<v[i]<<")\n";
}
};
int main()
{
int size;
cout<<"enter size of vector: ";
cin>>size;
vector<int> vi(size);
vi.create();
vi.display();
vi.modify();
vi.display();
}
QUESTION 4
Write a c++ program to find the data type of 5 data items inserted by the user (using a single function
template)?
#include <iostream>
using namespace std;
template <class T>
T Large(T n1, T n2)
{
}
int main()
{
int i1 ;
float f1 ;
char c1 ,c2;
cout << "Enter integers:\n";
cin >> i1;
cout << "\nEnter floating-point numbers:\n";
cin >> f1 ;
cout << "\nEnter character:\n";
cin >> c1 ;
cout << "\nEnter character:\n";
cin >> c2 ;
return 0;
}

You might also like