Advertisement
Garey

stf_zad_ktrl

Nov 24th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3.  
  4. using namespace std;
  5.  
  6. double input_array(double *array, size_t array_size);
  7. double calculate_average(double *, size_t);
  8. void find_min_positive(double *, size_t, double &, int &);
  9. void output_array(double *array, size_t array_size);
  10.  
  11. int main() {
  12.  
  13.     size_t array_size;
  14.  
  15.     int choice, min_positive_index = 0;
  16.  
  17.     double min_positive = 0.0;
  18.  
  19.     cout << "Enter array size: ";
  20.     cin >> array_size;
  21.  
  22.     if(!cin || array_size < 0 || array_size > 15)
  23.         return 0;
  24.  
  25.     double *array = new double[array_size];
  26.  
  27.     do {
  28.         system("cls");
  29.         cout << "Menu: \n";
  30.         cout << "1. Input array\n";
  31.         cout << "2. Average number of negatives in the array\n";
  32.         cout << "3. Find min and its index\n";
  33.         cout << "4. Output array";
  34.         cout << "\n\n0. Exit\n";
  35.         cout << "Enter your choice: ";
  36.         cin >> choice;
  37.  
  38.         switch(choice) {
  39.         case 1:
  40.             system("cls");
  41.             input_array(array, array_size);
  42.             break;
  43.  
  44.         case 2:
  45.             system("cls");
  46.             cout << "Average of negative numbers in the array is " << calculate_average(array, array_size);
  47.             _getch();
  48.             break;
  49.  
  50.         case 3:
  51.             system("cls");
  52.             find_min_positive(array, array_size, min_positive, min_positive_index);
  53.             cout << "Minimum positive is " << min_positive << " and has index #" << min_positive_index;
  54.             _getch();
  55.             break;
  56.         case 4:
  57.             system("cls");
  58.             output_array(array, array_size);
  59.             _getch();
  60.             break;
  61.         }
  62.     } while(choice != 0);
  63.  
  64.     delete[] array;
  65.  
  66.     return 0;
  67. }
  68.  
  69. double input_array(double *array, size_t array_size) {
  70.     for(size_t i = 0; i < array_size; i++) {
  71.         cout << "Enter element #" << i << ": ";
  72.         cin >> array[i];
  73.     }
  74.  
  75.     return *array;
  76. }
  77.  
  78. void output_array(double *array, size_t array_size) {
  79.     for(size_t i = 0; i < array_size; i++) {
  80.         cout << array[i] << endl;
  81.     }
  82. }
  83.  
  84. double calculate_average(double *array, size_t array_size) {
  85.     double average = 0.0;
  86.     int br = 0;
  87.  
  88.     for(size_t i = 0; i < array_size; i++)
  89.         if(array[i] < 0) {
  90.             average += array[i];
  91.             br++;
  92.         }
  93.  
  94.     return (average / br);
  95. }
  96.  
  97. void find_min_positive(double *array, size_t array_size, double &min_positive, int &index) {
  98.    
  99.     index = 0;
  100.     size_t min_counter = 0;
  101.     size_t min_index = 0;
  102.  
  103.     for(size_t i = 0; i < array_size; i++)
  104.         if(array[i] > 0) {
  105.             min_counter++;
  106.             min_index = i;
  107.         }
  108.  
  109.     if(min_counter == 0)
  110.         cout << "No positive numbers";
  111.     else
  112.         min_positive = array[min_index];
  113.  
  114.     for(size_t i = 0; i < array_size; i++) {
  115.         if(min_positive > array[i] && array[i] > 0) {
  116.             min_positive = array[i];
  117.             index = i+1;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement