Advertisement
Garey

Homework Exercise

Oct 16th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int count;
  4.  
  5. using namespace std;
  6.  
  7. void array_input(double array[]) {
  8.  
  9.     system("cls");
  10.  
  11.     cout << "Enter the array size: ";
  12.     cin >> count;
  13.  
  14.     if(!cin || count > 25)
  15.       cout << "Error: Try again";
  16.     else {
  17.       for (size_t i = 0; i < count; i++) {
  18.         cout << "Enter number: ";
  19.         cin >> array[i];
  20.       }
  21.     }
  22.  
  23.     system("cls");
  24. }
  25.  
  26. void array_output(double array[]) {
  27.  
  28.   system("cls");
  29.  
  30.   for (size_t i = 0; i < count; i++)
  31.     cout << "Index: " << i << " | Value: " << array[i] << endl;
  32.  
  33.   cout << endl << endl << endl;
  34. }
  35.  
  36. void calculate_average(double array[]) {
  37.  
  38.   system("cls");
  39.  
  40.   double sum = 0;
  41.   int min, max, ticks = 0;
  42.  
  43.   cout << "Enter minimum range to check array: " << endl;
  44.   cin >> min;
  45.   cout << "Enter maximum range to check array: " << endl;
  46.   cin >> max;
  47.  
  48.  
  49.     for (size_t i = 0; i < count; i++) {
  50.       if(array[i] >= min && array[i] <= max) {
  51.         sum += array[i];
  52.         ticks++;
  53.       }
  54.     }
  55.  
  56.     cout << "Average number: " << sum / ticks << endl << endl << endl;
  57. }
  58.  
  59. void calculate_diff_than_zero_and_zero(double array[]) {
  60.  
  61.   system("cls");
  62.  
  63.   int zeros_count = 0, non_zeros_sum = 0;
  64.  
  65.   for (size_t i = 0; i < count; i++) {
  66.     array[i] == 0 ? zeros_count++ : non_zeros_sum += array[i];
  67.   }
  68.  
  69.   cout << "Zeros count in the array: " << zeros_count << " | Different than zeros sum: " << non_zeros_sum << endl << endl << endl;
  70. }
  71.  
  72. void find_minimum_positive_number(double array[]) {
  73.  
  74.   system("cls");
  75.  
  76.   int min = array[0] >= 0 ? array[0] : 0;
  77.   int array_index = 0;
  78.  
  79.   for (size_t i = 0; i < count; i++) {
  80.     if(array[i] <= min && array[i] >=0) {
  81.         min = array[i];
  82.         array_index = i;
  83.     }
  84.   }
  85.  
  86.   cout << "Minimum positive integer: " << min << " | Index: " << array_index << endl << endl << endl;
  87. }
  88.  
  89. int main() {
  90.  
  91.   double arr[5];
  92.   int choice;
  93.  
  94. do {
  95.   cout << "Menu:" << endl;
  96.   cout << "1. Enter array and its size" << endl;
  97.   cout << "2. Calculate the average of all the numbers in the array" << endl;
  98.   cout << "3. Calculate the summary of the numbers different than zero and the count of the zero numbers" << endl;
  99.   cout << "4. Find the minimum positive number in hte massive and his index" << endl;
  100.   cout << "5. Show me the array" << endl;
  101.   cout << "6. Exit" << endl << endl;
  102.  
  103.   cout << "Make your choice: ";
  104.   cin >> choice;
  105.  
  106.   switch (choice) {
  107.     case 1:
  108.       array_input(arr);
  109.       break;
  110.     case 2:
  111.       calculate_average(arr);
  112.       break;
  113.     case 3:
  114.       calculate_diff_than_zero_and_zero(arr);
  115.       break;
  116.     case 4:
  117.       find_minimum_positive_number(arr);
  118.       break;
  119.  
  120.     case 5:
  121.       array_output(arr);
  122.       break;
  123.  
  124.   }
  125. } while(choice != 6);
  126.  
  127.   return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement