PROGRAM TO
PRINT THE LARGEST ELEMENT OF AN ARRAY
#include<iostream.h>
int main()
{ system("cls");
float large(float arr[], int n);
char ch;
int i=0;
float amount[50],biggest;
for(i=0; i<50;i++)
{ cout<<"Enter element
no."<<(i+1)<<"\n";
cin>>amount[i];
cout<<"More elements ? (y/n)
\n";
cin>>ch;
if(ch!='y')
break;
}
if(i<50)
i++;
biggest=large(amount,i); // in case of incomplete loop
//call
function large() bu passing
// the
array and its number of elements
cout<<"The largest element of the
array is:"<<biggest<<"\n";
return 0;
} //end of main
float large (float arr[], int n)
{
float max=arr[0];
for (int j=1;j<n;++j)
{
if(arr[j]> max)
if(arr[j]> max)
max=arr[j];
}
return max;
}
-----------Output-------------
Enter element no.1
76
More elements ?
(y/n)
y
Enter element no.2
89
More elements ?
(y/n)
y
Enter element no.3
32
More elements ?
(y/n)
n
The largest element of the array is:89
No comments:
Post a Comment