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 *, int);
- double calculate_sum_of_negative(double *, int);
- void find_min_negative(double *, int, double &, int &);
- int main() {
- int n;
- int choice, min_negative_index = 0;
- double min_negative = 0.0;
- // Vuvejda se razmer na masiva
- cout << "Enter array size: ";
- cin >> n;
- // Proverka za korektnost
- if(!cin || n < 0 || n > 15)
- return 0;
- // Deklarira se masiva s razmer ot potrebitelq zadaden
- double *array = new double[n];
- do {
- system("cls");
- cout << "Menu: \n";
- cout << "1. Input array\n";
- cout << "2. Sum of negative numbers in the array\n";
- cout << "3. Find min negative number and its index\n";
- cout << "\n0. Exit\n";
- cout << "Enter your choice: ";
- cin >> choice;
- switch(choice) {
- case 1:
- system("cls");
- // Izvikva se funkciqta za vuvejdane v masiv
- input_array(array, n);
- break;
- case 2:
- system("cls");
- cout << "Sum of negative numbers in array is " << calculate_sum_of_negative(array, n);
- _getch();
- break;
- case 3:
- system("cls");
- find_min_negative(array, n, min_negative, min_negative_index);
- cout << "Minimum negative number is " << min_negative << " and has index #" << min_negative_index;
- _getch();
- break;
- }
- } while(choice != 0);
- delete[] array;
- return 0;
- }
- double input_array(double *array, int array_size) {
- for(int i = 0; i < array_size; i++) {
- cout << "Enter element #" << i << ": ";
- cin >> array[i];
- }
- // Vrushta se kato rezultat vuvedeniqt masiv
- return *array;
- }
- double calculate_sum_of_negative(double *array, int array_size) {
- double sum_negative = 0.0;
- for(int i = 0; i < array_size; i++)
- if(array[i] < 0)
- sum_negative = sum_negative + array[i];
- // Vrushta se kato rezultat sbora ot otricatelnite chisla
- return sum_negative;
- }
- void find_min_negative(double *array, int array_size, double &min_negative, int &index) {
- min_negative = array[0];
- index = 0;
- // Zadava se index da e 0 a minimalniqt element da e purviqt ot masiva
- for(int i = 0; i < array_size; i++) {
- // Proverqva se dali minimalniqt element e po-golqm ot tekushtiqt
- // i dali tekushtiqt element e otricatelen
- // Ako e se prisvoqvat dvete stoinosti i se predavat po parametur
- if(min_negative > array[i] && array[i] < 0) {
- min_negative = array[i];
- index = i;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement