Professional Documents
Culture Documents
Write a C++ program to determine the largest and smallest elements in an array. Use void
min_max(float [], float &min, float &max) and void min_max(int[], int &min, int &max) as
your overloaded functions.
AIM:
C++ program to determines the largest and smallest elements in an array by function overloading.
ALGORITHM:
Step 1: Start
Step 2: Declare two array and get inputs
Step 3: Declare 1st function having same name and pass 1st array
Step 4: Declare 2nd function having same name and pass 2nd array.
Step 5: Initialise the ‘min’ and ‘max’ as first element in the array
Step 6: If min is greater than next element, then store successive element as min and
again compare with next elements
Step 7: Else, next element is max. Again, compare with next elements
Step 8: Display min and max.
Step 8: Stop
PROGRAM:
#include<iostream>
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>f[i];
max=min=f[0];
for(int i=0;i<n;i++)
{
if(f[i]>max)
{
17
Reg No: 200601049
Ex.No: 3-a Date:
max=f[i];
}
if(f[i]<min)
{
min=f[i];
}
}
cout<<"Largest of float array is "<<max;
cout<<"\nSmallest of float array is "<<min;
//your code
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
max=min=a[0];
for(int i=0;i<n;i++)
if(a[i]>max)
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
18
Reg No: 200601049
Ex.No: 3-a Date:
}
cout<<"Largest of integer array is "<<max;
cout<<"\nSmallest of integer array is "<<min;
int main()
float fmini,fmaxi;
float f[25];
int a[25],imin,imax;
min_max(f,fmini,fmaxi);
min_max(a,imin,imax);
Result:
Thus, the program to determine the largest and smallest elements in an array by using
function overloading is successfully executed and verified.
19
Reg No: 200601049