Member-only story

findMin method: Return the position of a string in the array such that that string is <= every string in the array

Marika Lam
1 min readMay 4, 2022

--

Return the position of a string in the array such that that string is <= every string in the array. If there is more than one such string, return the smallest position number of such a string. Return −1 if the function should consider no elements to be part of the array. Here’s an example:

string people[5] = { "juliet", "sierra", "sierra", "echo", "november" };
int q = findMin(people, 5); // returns 3, since echo is earliest
// in alphabetic order

Psuedocode

int findMin(const string a[], int n){int currentMinPosition=0;for (unsigned int i=0; i<n; i++){//if a[i] is less than a[currentMinPosition], to check which is alphabetically before the other//if it matches, then set currentMinPosition to i}return currentMinPosition;}

Solution

int findMin(const string a[], int n){if (n<=0){return -1;}int currentMinPosition=0;for (unsigned int i=0; i<n; i++){if (a[i]<a[currentMinPosition]){currentMinPosition=i;

--

--

No responses yet