Advertisement
Garey

Find mini positive number with check

Jan 6th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int find_min_positive(int[], size_t, size_t &);
  6.  
  7. int main() {
  8.  
  9.     int arr[] = { 9, -2, 3, 4, 5, -9, 11, 2, -1 };
  10.  
  11.     size_t index = 0;
  12.  
  13.     if (find_min_positive(arr, 9, index) != 0)
  14.         cout << "Minimum positive element in the array is " << find_min_positive(arr, 9, index) << " and its index is " << index << endl;
  15.     else
  16.         cout << "No positive elements in the array!\n";
  17.  
  18.     return 0;
  19. }
  20.  
  21. int find_min_positive(int array[], size_t array_size, size_t &array_index) {
  22.  
  23.     size_t counter = 0;
  24.  
  25.     for (size_t i = 0; i < array_size; i++)
  26.         if (array[i] > 0) {
  27.             array_index = i;
  28.             counter++;
  29.         }
  30.  
  31.     if (counter == 0)
  32.         return 0;
  33.  
  34.     int min = array[array_index];
  35.  
  36.     for (size_t i = 0; i < array_size; i++)
  37.         if (min > array[i] && array[i] > 0) {
  38.             min = array[i];
  39.             array_index = i;
  40.         }
  41.  
  42.     return min;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement