Promi_38

Linear search-find the smallest and largest data of a dataset

Nov 16th, 2020 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int n;
  6.     printf("Enter the size of an array: ");
  7.     scanf("%d", &n);
  8.    
  9.     int a[n], i;
  10.     printf("Enter the elements: ");
  11.     for(i = 0; i < n; i++) scanf("%d", &a[i]);
  12.    
  13.     int max = a[0], min = a[0];
  14.    
  15.     for(i = 1; i < n; i++)
  16.     {
  17.         if(max < a[i]) max = a[i];
  18.         if(min > a[i]) min = a[i];
  19.     }
  20.    
  21.     printf("The maximum element is %d and the minimum element is %d\n", max, min);
  22. }
Add Comment
Please, Sign In to add comment