Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- int input_array(int *, size_t);
- int calculate_positive_and_negative(int *, size_t, int &);
- void find_min_positive(int *, size_t, int &, size_t &);
- void output_array(int *array, size_t array_size);
- int main() {
- size_t array_size, choice, min_positive_index = 0;
- int min_positive = 0, multiplication = 1;
- cout << "Enter array size: ";
- cin >> array_size;
- if (!cin || array_size < 0 || array_size > 15)
- return 0;
- int *array = new int[array_size];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "1. Input array\n";
- cout << "2. Sum of positive numbers & Multiplication of negatives\n";
- cout << "3. Find min and its index\n";
- cout << "4. Output array";
- cout << "\n\n0. Exit\n";
- cout << "Enter your choice: ";
- cin >> choice;
- switch (choice) {
- case 1:
- system("cls");
- input_array(array, array_size);
- break;
- case 2:
- system("cls");
- cout << "The sum of positive numbers is " << calculate_positive_and_negative(array, array_size, multiplication) << " and the multiplication of negatives is " << multiplication;
- _getch();
- break;
- case 3:
- system("cls");
- find_min_positive(array, array_size, min_positive, min_positive_index);
- cout << "Minimum positive is " << min_positive << " and has index #" << min_positive_index;
- _getch();
- break;
- case 4:
- system("cls");
- output_array(array, array_size);
- _getch();
- break;
- }
- } while (choice != 0);
- delete[] array;
- return 0;
- }
- int input_array(int *array, size_t array_size) {
- for (size_t i = 0; i < array_size; i++) {
- cout << "Enter element #" << i << ": ";
- cin >> array[i];
- }
- return *array;
- }
- void output_array(int *array, size_t array_size) {
- for (size_t i = 0; i < array_size; i++) {
- cout << array[i] << endl;
- }
- }
- int calculate_positive_and_negative(int *array, size_t array_size, int &multiplication) {
- size_t br = 0;
- int sum = 0;
- for (size_t i = 0; i < array_size; i++)
- if (array[i] < 0)
- multiplication *= array[i];
- else
- sum += array[i];
- return sum;
- }
- void find_min_positive(int *array, size_t array_size, int &min_positive, size_t &index) {
- index = 0;
- size_t min_counter = 0;
- size_t min_index = 0;
- for (size_t i = 0; i < array_size; i++)
- if (array[i] > 0) {
- min_counter++;
- min_index = i;
- }
- if (min_counter == 0)
- cout << "No positive numbers";
- else
- min_positive = array[min_index];
- for (size_t i = 0; i < array_size; i++) {
- if (min_positive > array[i] && array[i] > 0) {
- min_positive = array[i];
- index = i + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement