home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / array4.cpp < prev    next >
C/C++ Source or Header  |  1993-03-24  |  1KB  |  57 lines

  1. // C++ program that passes arrays as arguments of functions
  2.  
  3. #include <iostream.h>
  4.               
  5. const int MAX = 10;              
  6.               
  7. main()
  8. {          
  9.   int arr[MAX];
  10.   int n;
  11.   
  12.   // declare prototypes of functions
  13.   int getMin(int a[MAX], int size);
  14.   int getMax(int a[], int size);
  15.   
  16.   do { // obtain number of data points
  17.     cout << "Enter number of data points [2 to "
  18.         << MAX << "] : ";
  19.     cin >> n;
  20.     cout << "\n";
  21.   } while (n < 2 || n > MAX);
  22.  
  23.   // prompt user for data
  24.   for (int i = 0; i < n; i++) {
  25.     cout << "arr[" << i << "] : ";
  26.     cin >> arr[i];
  27.   }
  28.  
  29.   cout << "Smallest value in array is "
  30.        << getMin(arr, n) << "\n"
  31.        << "Biggest value in array is "
  32.        << getMax(arr, n) << "\n";
  33.   return 0;
  34. }
  35.  
  36.  
  37. int getMin(int a[MAX], int size)
  38. {
  39.   int small = a[0];
  40.   // search for the smallest value in the
  41.   // remaining array elements
  42.   for (int i = 1; i < size; i++)
  43.     if (small > a[i])
  44.       small = a[i];
  45.   return small;
  46.  
  47. int getMax(int a[], int size)
  48. {
  49.   int big = a[0];
  50.   // search for the bigest value in the
  51.   // remaining array elements
  52.   for (int i = 1; i < size; i++)
  53.     if (big < a[i])
  54.       big = a[i];
  55.   return big;
  56. }