hi guys I made a small algorithm to find the smallest element and number of the element in an array,but just a question can you see any flaws in this way I did it and is there any ways I can improve this code if so how?
@AbstractionAnon
The OP didn't make it clear whether to find the smallest number or find the smallest index. Look at how OP named the function : findSmallest(){}
it prints the second number 2 as the smallest(I don't know why it does that but it does)
Look at line 8: You're setting the index the smallest number to the value of the first entry in the array. You want to set the smallest index to 0. Then at line 10, you want to start the for loop from 1. Not value[0]+1. closed account had this correct in the code he posted above. It would have been clearer if you had named "smallest", "smallest_index" instead.
As closed account also pointed out, there seems to be some confusion over whether you want to return the smallest number, or the index of the smallest entry. Line 19, you're returning the smallest index. Line 31, you're printing the smallest index, not the smallest value.
I don't think there was any confusion at all that he was returning the smallest index.
That said, a small comment at the top of the function indicating that it returns an index and not a value might be useful to readers.
Again I repeat that there is nothing wrong with your original code (except for the misplaced cout). It is a very efficient O(n).
Thanks guys for the help credit to all of you guys to help me sorting that out now I just need to solve the bigger picture using this in my insertion sort code