Write a function in C that does the following The function m
Write a function in C++ that does the following:
The function must not use any function templates from the algorithms portion of the Standard C++ library.
int locationOfMin(const string a[], int n);
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 of such a string. Return 1 if the array has no elements. Here\'s an example:
The function must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.
Solution
Hi, Please find my function implementation
Please let me know in case of any issue:
int locationOfMin(const string a[], int n){
// if array does not have any element
if( n <= 0){
return -1;
}
int min_index = 0; // initializing min_index with first element\'s index
// iterating for other element and checking against min element
for(int i=1; i<n; i++){
// if current element of smaller than current minimum element then reassign min_index with i
if(a[i].compare(a[min_index]) < 0){
min_index = i;
}
}
return min_index;
}
