You are on page 1of 8

SOFTWARE DEVELOPMENT FUNDAMENTALS

WEEK 10
ASSIGNMENT 9
1.
Output:
x=1 count=0
x=1 count=1
x=1.1 count=0
2.
Output:
2
1
3. Code:
—-------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
template <class t>
void calculator(t a , t b)
{
cout <<"Sum : " << a+b << endl
<< "Sub : " << a-b << endl
<< "Mul : " << a*b << endl
<< "Div : " << a/b << endl;
}
int main ()
{
cout << "For int (3,4) :\n";
calculator(3,4);
cout << "For Float (8.9 , 9.1) :\n";
calculator <float>(8.9,9.1);
cout << "For Double (9.13 , 5.6) : \n";
calculator <double> (9.13 , 5.6);
cout << "For long (11345 , 567839) : \n";
calculator <long> (11345 , 567839);
return 0;
}
—------------------------------------------------------------------------------------------
Output:

4. Code:
—-------------------------------------------------------------------------------
#include <iostream>
#include <stdio.h>
using namespace std;
template <class t>
t minimum(t a, t b)
{
if (a < b) { return a;}
return b;
}
template <class t1>
t1 maximum(t1 a, t1 b)
{
if (a > b) { return a;}
return b;
}
int main()
{
int ch, n;
do
{
cout << "1 : Int\n2 : Float\n3.Double\n4 : Long :: ";
cin >> n;
switch (n)
{
case 1:
int ptr , ptr1;
cout << "Enter the value :\n";
cin >> ptr >> ptr1;
cout << "Minimum : " << minimum <int>(ptr, ptr1) << endl;
cout << "Maximum : " << maximum <int>(ptr, ptr1) << endl;
break;
case 2:
float ptr2 , ptr3;
cout << "Enter the value :\n";
fflush(stdin);
cin >> ptr2 >> ptr3;
cout << "Minimum : " << minimum <float>(ptr2, ptr3) << endl;
cout << "Maximum : " << maximum <float>(ptr2, ptr3) << endl;
break;
case 3:
double ptr4 , ptr5;
fflush(stdin);
cout << "Enter the value :\n";
cin >> ptr4>> ptr5;
cout << "Minimum : " << minimum <double>(ptr4, ptr5) << endl;
cout << "Maximum : " << maximum <double>(ptr4, ptr5) << endl;
break;
case 4:
long ptr6 , ptr7;
cout << "Enter the value :\n";
cin >> ptr6 >> ptr7;
cout << "Minimum : " << minimum <long>(ptr6, ptr7) << endl;
cout << "Maximum : " << maximum <long>(ptr6, ptr7) << endl;
break;
default:
break;
}
cout << "1 : Continue\nAny other no. : exit : ";
cin >> ch;
} while (ch == 1);

return 0;
}
—------------------------------------------------------------------------------------------
Output:
5. Code:
—------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
template <class t>
class myvector
{
t *a;
int n;
public:
myvector(int p) : n(p)
{
a = new t (n);
}
void push_back(t x)
{
a[n-1] = x;
a[n++];
}
void pop_back()
{
a[n--];
}
void adder()
{
cout << "Enter the elements in the array :\n";
for (int i =0 ; i < n ;i++)
{
cin >> a[i];
}
a[++n];
}
void display()
{
for (int i=0 ; i < n ; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
};
int main()
{
myvector <int> a(5);
int n;
a.adder();
cout << "Enter the number for push_back: ";
cin >> n;
a.push_back(n);
a.display();
a.pop_back();
a.display();
myvector <float> a1(5);
a1.adder();
float n1;
cout << "Enter the number for push_back: ";
cin >> n1;
a1.push_back(n1);
a1.display();
a1.pop_back();
a1.display();
return 0;
}
—------------------------------------------------------------------------------------------
Output:

6. Code:
—-------------------------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n , n1;
cout << "Enter how many elements are there in array1 : ";
cin >> n;
cout << "Enter how many elements are there in array2 : ";
cin >> n1;
vector <int > a(n) , b(n1);
cout << "Enter the elements for array1 :\n";
for (int i=0 ; i<n ; i++)
{
cin >> a[i];
}
cout << "Enter the elements for array2 :\n";
for (int i=0 ; i<n1 ; i++)
{
cin >> b[i];
}
for(int i = n ; i < n1;i++)
{
a.push_back(b[i]);
}
sort(a.begin(),a.end());
for (int i =0 ; i< n1 ;i++)
{
cout << a[i] << " ";
}
return 0;
}
—------------------------------------------------------------------------------------------
Output:

You might also like