Create a C++ program that will overload the function max() to allow it to return
ID: 3621084 • Letter: C
Question
Create a C++ program that will overload the function max() to allow it to return the largest of three, four, or five numbers. You should have functions for numbers that are integers and different functions for numbers that are floating point. Write a main that prompts the user for whether they want to use integer or floating point numbers, then prompt for the number of numbers, then get the numbers, call the appropriate function, and return the appropriate maximum.All of the functions should be max(), but they will have a differing number and type of parameters as well as different return types.
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int max(int,int,int);
int max(int,int,int,int);
int max(int,int,int,int,int);
float max(float,float,float);
float max(float,float,float,float);
float max(float,float,float,float,float);
int main()
{int type,num,a,b,c,d,e,maxInt;
float f,g,h,i,j,maxFloat;
cout<<"Enter 1 for integers, 2 for floating point: ";
cin>>type;
while(type<1||type>2)
{cout<<"Invalid entry ";
cout<<"Enter 1 for integers, 2 for floating point: ";
cin>>type;
}
cout<<"Enter number of numbers between 3 and 5: ";
cin>>num;
while(num<3||num>5)
{cout<<"Invalid entry ";
cout<<"Enter number of numbers between 3 and 5: ";
cin>>num;
}
if(type==1)
{cout<<"Enter number 1: ";
cin>>a;
cout<<"Enter number 2: ";
cin>>b;
cout<<"Enter number 3: ";
cin>>c;
if(num==3)
maxInt=max(a,b,c);
else if(num==4)
{cout<<"Enter number 4: ";
cin>>d;
maxInt=max(a,b,c,d);
}
else
{cout<<"Enter number 4: ";
cin>>d;
cout<<"Enter number 5: ";
cin>>e;
maxInt=max(a,b,c,d,e);
}
cout<<"The largest number is "<<maxInt<<endl;
}
else
{cout<<"Enter number 1: ";
cin>>f;
cout<<"Enter number 2: ";
cin>>g;
cout<<"Enter number 3: ";
cin>>h;
if(num==3)
maxFloat=max(f,g,h);
else if(num==4)
{cout<<"Enter number 4: ";
cin>>i;
maxFloat=max(f,g,h,i);
}
else
{cout<<"Enter number 4: ";
cin>>i;
cout<<"Enter number 5: ";
cin>>j;
maxFloat=max(f,g,h,i,j);
}
cout<<"The largest number is "<<maxFloat<<endl;
}
system("pause");
return 0;
}
int max(int a,int b,int c,int d,int e)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)
max=e;
return max;
}
int max(int a,int b,int c,int d)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
return max;
}
int max(int a,int b,int c)
{int max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}
float max(float a,float b,float c,float d ,float e)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
if(e>max)
max=e;
return max;
}
float max(float a,float b,float c,float d)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
if(d>max)
max=d;
return max;
}
float max(float a,float b,float c)
{float max;
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
return max;
}