Advertisement
Garey

krisi_2

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