In: Computer Science
C++ The minimum function.
(a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of the smaller one according to the less-than operation. Test the function in main() using the integer and character types.
a.#include<iostream.h>
using namespace std;
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
int main()
{
int a=1;int b=4;
min(a,b);
int x=-100;int y=100;
min(x,y);
int e=7;int f=60;
min(e,f);
int m=1000;int n=50;
min(m,n);
int c=-50;int d=-90;
min(c,d);
return 0;
}
b)#include<iostream.h>
using namespace std;
char min(char a,char b);
{
if(a<b)
return a;
else
return b;
}
int main()
{
char x,y;
x='c';y='d';
min(c,d);
char m,n;
m='+';n='-';
min(m,n);
char c,d;
c='x';d='a;
min(c,d);
char u,v;
u='A';v='B';
min(u,v);
char e,f;
e='Y';f='Z';
min(e,f)_;
return 0;
}
c.#include<iostream.h>
using namespace std;
class Minimum
{
public:
int a,b;
void min()
{
if(a<b)
return a;
else
return b;
}
};
int main()
{
Minimum x;
x.a=5;x.b=6;
x.min();
Minimum m;
m.a=10;m.b=20;
m.min();
Minimum d;
d.a=-100;d.b=100;
d.min();
Minimum t;
t.a=-20;t.b=40;
t.min();
Minimum v;
v.a=-1000;v.b=-2000;
v.min();
return 0;
}