Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- double input_array(double *array, size_t array_size);
- double calculate_average(double *, size_t);
- void find_min_positive(double *, size_t, double &, int &);
- void output_array(double *array, size_t array_size);
- int main() {
- size_t array_size;
- int choice, min_positive_index = 0;
- double min_positive = 0.0;
- cout << "Enter array size: ";
- cin >> array_size;
- if(!cin || array_size < 0 || array_size > 15)
- return 0;
- double *array = new double[array_size];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "1. Input array\n";
- cout << "2. Average number of negatives in the array\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 << "Average of negative numbers in the array is " << calculate_average(array, array_size);
- _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;
- }
- double input_array(double *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(double *array, size_t array_size) {
- for(size_t i = 0; i < array_size; i++) {
- cout << array[i] << endl;
- }
- }
- double calculate_average(double *array, size_t array_size) {
- double average = 0.0;
- int br = 0;
- for(size_t i = 0; i < array_size; i++)
- if(array[i] < 0) {
- average += array[i];
- br++;
- }
- return (average / br);
- }
- void find_min_positive(double *array, size_t array_size, double &min_positive, int &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