bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / tuition-c / P062.c

P062.c (1300B)


      1 /* Write a C program to find the smallest and largest in an array using user-defined functions.
      2 Define two functions int findSmallest(int arr[], int n) and int findLargest(int arr[], int n) that
      3 return the smallest and largest elements in an array, respectively. */
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 
      8 void inputarr(int[], int);
      9 int findSmallest(int[], int);
     10 int findLargest(int[], int);
     11 
     12 int main()
     13 {
     14     int n, *arr;
     15     printf("How many element do you want to enter: ");
     16     scanf("%d", &n);
     17     arr = (int *)malloc(n * sizeof(int));
     18     inputarr(arr, n);
     19     printf("\nSmallest element = %d", findSmallest(arr, n));
     20     printf("\nLargest element  = %d", findLargest(arr, n));
     21     free(arr);
     22     return 0;
     23 }
     24 
     25 void inputarr(int arr[], int n)
     26 {
     27     int i;
     28     for (i = 0; i < n; i++)
     29     {
     30         printf("Enter Element %d: ", i + 1);
     31         scanf("%d", &arr[i]);
     32     }
     33 }
     34 
     35 int findSmallest(int arr[], int n)
     36 {
     37     int i, smallest = arr[0];
     38     for (i = 1; i < n; i++)
     39     {
     40         if (smallest > arr[i])
     41         {
     42             smallest = arr[i];
     43         }
     44     }
     45     return smallest;
     46 }
     47 
     48 int findLargest(int arr[], int n)
     49 {
     50     int i, largest = arr[0];
     51     for (i = 1; i < n; i++)
     52     {
     53         if (largest < arr[i])
     54         {
     55             largest = arr[i];
     56         }
     57     }
     58     return largest;
     59 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror