From 2dddebc51674c7c9ab2f655bd916d0c3a5dbaafd Mon Sep 17 00:00:00 2001 From: shefali kanojia <56781823+shefali12-ab@users.noreply.github.com> Date: Fri, 22 Oct 2021 18:30:43 +0530 Subject: [PATCH] chore(CPlusPlus): add index of smallest element in array (#584) Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com> --- .../index-of-smallest-element-of-array.cpp | 35 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 36 insertions(+) create mode 100644 algorithms/CPlusPlus/Arrays/index-of-smallest-element-of-array.cpp diff --git a/algorithms/CPlusPlus/Arrays/index-of-smallest-element-of-array.cpp b/algorithms/CPlusPlus/Arrays/index-of-smallest-element-of-array.cpp new file mode 100644 index 00000000..f46587cc --- /dev/null +++ b/algorithms/CPlusPlus/Arrays/index-of-smallest-element-of-array.cpp @@ -0,0 +1,35 @@ +//index of smallest element of array +/* Example : +size of array =5 +Let array be ,arr[5]={9,4,1,5,8} +here smallest element of array is 1 which is at index 2 of array +output = 2 that is index of smallest element of array. +Time complexity = O(n) +*/ + +#include +using namespace std; +int main() +{ + int size,j; + cout<<"enter the size of array"; + cin>>size; + int arr[size]; + for(int i=0;i>arr[i]; + } + //considering the element at zero index to be smallest + int min=arr[0]; + int smallest_index=0; + //comparing min to all other elements + for(j=0;jarr[j]) + { + min=arr[j]; + smallest_index=j; + } + } + cout<<"index of smallest element of array is"<