Finding smallest in an integer array
findSmallest() finds the smallest in the integer array and returns the smallest 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 findSmallest(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<<findSmallest ( arr1 , size1 );cout<<endl;
const int size2=10;
int arr2[size2]={85,20,30,40,50,60,60,70,75,80};
cout<<findSmallest ( arr2 , size2 );cout<<endl;
const int size3=10;
int arr3[size3]={1,2,3,4,5,6,7,8,9,0};
cout<<findSmallest ( arr3 , size3 );cout<<endl;
const int size4=10;
int arr4[size4]={10,20,30,40,50,60,70,80,90,100};
cout<<findSmallest ( arr4 , size4 );cout<<endl;
const int size5=10;
int arr5[size5]={5,10,15,20,25,30,35,40,45,50};
cout<<findSmallest ( arr5 , size5 );cout<<endl;
_getche();
return 0;
}
Output
10
20
0
10
5
0 comments:
Post a Comment