SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | #include <conio.h> | |
3 | ||
4 | using namespace std; | |
5 | ||
6 | double input_array(double *, int); | |
7 | double calculate_sum_of_negative(double *, int); | |
8 | void find_min_negative(double *, int, double &, int &); | |
9 | ||
10 | int main() { | |
11 | ||
12 | int n; | |
13 | ||
14 | int choice, min_negative_index = 0; | |
15 | ||
16 | double min_negative = 0.0; | |
17 | // Vuvejda se razmer na masiva | |
18 | cout << "Enter array size: "; | |
19 | cin >> n; | |
20 | ||
21 | // Proverka za korektnost | |
22 | if(!cin || n < 0 || n > 15) | |
23 | return 0; | |
24 | ||
25 | // Deklarira se masiva s razmer ot potrebitelq zadaden | |
26 | double *array = new double[n]; | |
27 | ||
28 | do { | |
29 | system("cls"); | |
30 | cout << "Menu: \n"; | |
31 | cout << "1. Input array\n"; | |
32 | cout << "2. Sum of negative numbers in the array\n"; | |
33 | cout << "3. Find min negative number and its index\n"; | |
34 | cout << "\n0. Exit\n"; | |
35 | cout << "Enter your choice: "; | |
36 | cin >> choice; | |
37 | ||
38 | switch(choice) { | |
39 | case 1: | |
40 | system("cls"); | |
41 | // Izvikva se funkciqta za vuvejdane v masiv | |
42 | input_array(array, n); | |
43 | break; | |
44 | ||
45 | case 2: | |
46 | system("cls"); | |
47 | cout << "Sum of negative numbers in array is " << calculate_sum_of_negative(array, n); | |
48 | _getch(); | |
49 | break; | |
50 | ||
51 | case 3: | |
52 | system("cls"); | |
53 | find_min_negative(array, n, min_negative, min_negative_index); | |
54 | cout << "Minimum negative number is " << min_negative << " and has index #" << min_negative_index; | |
55 | _getch(); | |
56 | break; | |
57 | } | |
58 | } while(choice != 0); | |
59 | ||
60 | delete[] array; | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
65 | double input_array(double *array, int array_size) { | |
66 | for(int i = 0; i < array_size; i++) { | |
67 | cout << "Enter element #" << i << ": "; | |
68 | cin >> array[i]; | |
69 | } | |
70 | // Vrushta se kato rezultat vuvedeniqt masiv | |
71 | return *array; | |
72 | } | |
73 | ||
74 | ||
75 | double calculate_sum_of_negative(double *array, int array_size) { | |
76 | double sum_negative = 0.0; | |
77 | ||
78 | for(int i = 0; i < array_size; i++) | |
79 | if(array[i] < 0) | |
80 | sum_negative = sum_negative + array[i]; | |
81 | // Vrushta se kato rezultat sbora ot otricatelnite chisla | |
82 | return sum_negative; | |
83 | } | |
84 | ||
85 | void find_min_negative(double *array, int array_size, double &min_negative, int &index) { | |
86 | min_negative = array[0]; | |
87 | index = 0; | |
88 | // Zadava se index da e 0 a minimalniqt element da e purviqt ot masiva | |
89 | ||
90 | for(int i = 0; i < array_size; i++) { | |
91 | // Proverqva se dali minimalniqt element e po-golqm ot tekushtiqt | |
92 | // i dali tekushtiqt element e otricatelen | |
93 | // Ako e se prisvoqvat dvete stoinosti i se predavat po parametur | |
94 | if(min_negative > array[i] && array[i] < 0) { | |
95 | min_negative = array[i]; | |
96 | index = i; | |
97 | } | |
98 | } | |
99 | } |