CPP   110

max min

Guest on 20th April 2022 02:15:15 AM

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int max(int a[], int size) {
  6.     int max_item;
  7.  
  8.     // make sure there are items in the array
  9.     if(size>0) {
  10.         // First item is the max.
  11.         max_item=a[0];
  12.  
  13.         // Check all the other items if they are higher than the max value.
  14.         for(int i=1;i<size;i++)
  15.             if(a[i]>max_item)
  16.                 max_item=a[i];
  17.     }
  18.  
  19.     return max_item;
  20. }
  21.  
  22. int main () {
  23.     int a[]={5,3,9,2,1};
  24.  
  25.     int maxFound;
  26.  
  27.     maxFound=max(a,5);
  28.  
  29.     cout << maxFound << endl;
  30.  
  31.     return 0;
  32. }

Raw Paste


Login or Register to edit or fork this paste. It's free.