c code to find smallest and largest

  • c
  • write a c code to find smallest and largest in random number generated in array

    Write a program to store random numbers into an array of n integers and then find out the smallest and largest number stored in it. n is the user input.

    Code

    #include <stdio.h> #include <stdlib.h> #include <time.h> void findsmalllarge(int *arr, int n) { int min = INT_MAX; int max = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] < min) { min = arr[i]; } if (arr[i] > max) { max = arr[i]; } } for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\nlargest and samllest are : %d and %d", min, max); } int main() { clock_t t; t = clock(); int n; printf("enter the size of array : "); scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { arr[i] = rand(); } findsmalllarge(arr, n); t = clock() - t; double time_taken = ((double)t) / CLOCKS_PER_SEC; printf("\nfun() took %f seconds to execute \n", time_taken); return 0; }