In: Computer Science
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Code:
#include<stdio.h>
int minimum(int[], int);
int main(){
int i, n, arr[20], min, pos;
printf("Enter no. of elements in the array: ");
scanf("%d",&n);
printf("Enter %d elements: ",n);
for(i=1;i<=n;i++){
scanf("%d",&arr[i]);
}
pos = minimum(arr , n);
min = arr[pos];
printf("Minimum is %d\n",min);
printf("Position of minimum is %d",pos);
}
int minimum(int arr[], int n){
int i, min, pos;
min = arr[0];
pos = 0;
for(i=1;i<=n;i++){
if(arr[i] < min){
pos = i;
min =
arr[i];
}
}
return pos;
}
Code in image:
Output:
Explanation: