Ask Question
4 June, 06:24

Given a vector and a starting position, find the index of / / the smallest value in the range [start, last], where last is / / vector. size () - 1. That index is returned.

+3
Answers (1)
  1. 4 June, 09:26
    0
    The solution code is written in Python

    def findSmallest (vec, start) : index = start smallest = vec[start] for i in range (start + 1, len (vec)) : if (smallest > vec[i]) : smallest = vec[i] index = i return index

    Explanation:

    Firstly we can define a function findSmallest () that takes two input parameters, a vector, vec, and a starting position, start (Line 1).

    Next, create two variables, index and smallest, to hold the current index and current value where the smallest number is found in the vector. Let's initialize them with start position and the value held in the start position (Line 3-4).

    Next, create a for-loop to traverse through the next value of the vector after start position and compare it with current smallest number. If current smallest is bigger than any next value in the vector, the smallest variable will be updated with the new found lower value in the vector and the index where the lower value is found will be assigned to variable index.

    At the end return index as output.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Given a vector and a starting position, find the index of / / the smallest value in the range [start, last], where last is / / vector. size ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers