In: Computer Science
In C language:
Write the fnFindMin function which can receive a float array, an integer number(number of elements) as input arguments and finds the minimum value in the array. This function should return the array index (subscript) of the minimum value.
C code:
#include <stdio.h>
//initializing fnFindMin function
int fnFindMin(float numbers[],int n){
//initializing minimum element as the first number
int min=numbers[0];
//initializing index of minimum element as 0
int min_index=0;
//loop to check all numbers
for(int i=1;i<n;i++){
//checking if the current element is less than min element
if(numbers[i]<min){
//initializing current element as min
min=numbers[i];
//initializing min_index as current index
min_index=i;
}
}
//returning min_index
return min_index;
}
int main()
{
//initializing a sample array containing numbers
float numbers[]={2.1,3.2,1.1,5.1};
//calling fnFindMin function
printf("%d",fnFindMin(numbers,4));
return 0;
}
Screenshot:
Output: