P062.c (1491B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 16 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a C program to find the smallest and largest in an array using user-defined functions. 9 Define two functions int findSmallest(int arr[], int n) and int findLargest(int arr[], int n) that 10 return the smallest and largest elements in an array, respectively. */ 11 12 #include <stdio.h> 13 #include <stdlib.h> 14 15 void inputarr(int[], int); 16 int findSmallest(int[], int); 17 int findLargest(int[], int); 18 19 int main() 20 { 21 int n, *arr; 22 printf("How many element do you want to enter: "); 23 scanf("%d", &n); 24 arr = (int *)malloc(n * sizeof(int)); 25 inputarr(arr, n); 26 printf("\nSmallest element = %d", findSmallest(arr, n)); 27 printf("\nLargest element = %d", findLargest(arr, n)); 28 free(arr); 29 return 0; 30 } 31 32 void inputarr(int arr[], int n) 33 { 34 int i; 35 for (i = 0; i < n; i++) 36 { 37 printf("Enter Element %d: ", i + 1); 38 scanf("%d", &arr[i]); 39 } 40 } 41 42 int findSmallest(int arr[], int n) 43 { 44 int i, smallest = arr[0]; 45 for (i = 1; i < n; i++) 46 { 47 if (smallest > arr[i]) 48 { 49 smallest = arr[i]; 50 } 51 } 52 return smallest; 53 } 54 55 int findLargest(int arr[], int n) 56 { 57 int i, largest = arr[0]; 58 for (i = 1; i < n; i++) 59 { 60 if (largest < arr[i]) 61 { 62 largest = arr[i]; 63 } 64 } 65 return largest; 66 }