-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathcreatervikas
31 lines (31 loc) · 839 Bytes
/
creatervikas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
int findSmallestElement(int arr[], int n){
/* We are assigning the first array element to
* the temp variable and then we are comparing
* all the array elements with the temp inside
* loop and if the element is smaller than temp
* then the temp value is replaced by that. This
* way we always have the smallest value in temp.
* Finally we are returning temp.
*/
int temp = arr[0];
for(int i=0; i<n; i++) {
if(temp>arr[i]) {
temp=arr[i];
}
}
return temp;
}
int main() {
int n;
cout<<"Enter the size of array: ";
cin>>n; int arr[n-1];
cout<<"Enter array elements: ";
for(int i=0; i<n; i++){
cin>>arr[i];
}
int smallest = findSmallestElement(arr, n);
cout<<"Smallest Element is: "<<smallest;
return 0;
}