"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, March 11, 2010

Write program with overloaded prototypes of themax(), a function that displays maximum values of its arguments. Write three versions : one for single int value, one for two int values, and one for an array of int values.

/* Function Overloading :: All Function names are same but, arguments are of different types and different quantities */ 

#include<iostream.h>
#include<conio.h>

// Function 1
void themax(int a)
{
cout<< "\n The maximum number is = " << a;
}

// Function 2
int themax(int m, int n)
{
(m>n ? return m : return n);
}

// Function 3
int themax( int arr[ ])
{
int max = arr[0];
for(int i = 1; i < n ; i++)
{
if( max < arr[i] )
   max = arr[i];
}

return max;
}


void main()
{
int a[10], int x , y , max;

/* Function 1 calling ...demonstrated */

cout<< "\n Enter 2 numbers ";
cin >> x >> y;

if(x>y)
   themax(x);
else
   themax(y);

getch(); 

/* Function 2 calling ...demonstrated */

cout<< "\n Enter 2 numbers ";
cin >> x >> y;

max = themax(x,y);
cout<< The Maximum between " << x << " and " << y << " is " << max ;

getch();
clrscr();

/* Function 3 calling ...demonstrated */

for(int i = 0; i < 10 ; i ++)
{
cout<< "\n Enter an integer ";
cin >> a[i];
}

max = themax(a);                                   

cout<< "\n\n The maximum value is " << max ;

getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.