Finding largest in an integer array
findLargest() finds the largest in the integer array and returns the largest value. It takes 2 parameter:
arr is the array.
len is the length of the array.
Code
#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"
#include "math.h"
using namespace std;
int findLargest(int arr[],int len)
{
int key=arr[0];
for(int i=0;i<len;i++)
if(arr[i]>key)
key=arr[i];
return key;
}
int main()
{
const int size1=10;
int arr1[size1]={10,20,30,40,50,60,60,70,80,90};
cout<<findLargest ( arr1 , size1 );cout<<endl;
const int size2=10;
int arr2[size2]={85,20,30,40,50,60,60,70,75,80};
cout<<findLargest ( arr2 , size2 );cout<<endl;
const int size3=10;
int arr3[size3]={1,2,3,4,5,6,7,8,9,0};
cout<<findLargest ( arr3 , size3 );cout<<endl;
const int size4=10;
int arr4[size4]={10,20,30,40,50,60,70,80,90,100};
cout<<findLargest ( arr4 , size4 );cout<<endl;
const int size5=10;
int arr5[size5]={5,10,15,20,25,30,35,40,45,50};
cout<<findLargest ( arr5 , size5 );cout<<endl;
_getche();
return 0;
}
Output
90
85
9
100
50
0 comments:
Post a Comment