-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (100 loc) · 2.89 KB
/
main.cpp
File metadata and controls
115 lines (100 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
using namespace std;
//function prototypes
void display_menu(char&, vector <int>&);
void selection(char&, vector <int>&);
void print_num(vector <int>);
void add_num(vector <int>&, int&);
void mean(vector <int>, int&);
void smallest(vector <int>, int&);
void largest(vector <int>, int&);
//function declaration
void display_menu(char &c, vector <int> &x) {
cout << "enter the letter of the operation you want to perform on the list!" << endl;
cout << "P - print numbers" << endl;
cout << "A - add a number" << endl;
cout << "M - display mean of the numbers" << endl;
cout << "S - display the smallest number" << endl;
cout << "L - display the largest number" << endl;
cout << "Q - quit" << endl;
cin >> c;
selection(c, x);
}
void selection(char &c, vector <int> &x) {
if(c == 'p' || c == 'P') {
if (x.size() == 0)
cout << "[], the list is empty" << endl;
else {
print_num(x);
}
cout << endl;
} else if (c == 'a' || c == 'A') {
int a;
add_num(x, a);
} else if (c == 'm' || c == 'M') {
if (x.size() == 0)
cout << "unable to calculate average - no data found" << endl;
else {
int sum;
mean(x, sum);
}
} else if (c == 's' || c == 'S') {
if (x.size() == 0)
cout << "unable to determinate the smallest number - list is empty" << endl;
else {
int lowest;
smallest(x, lowest);
}
} else if (c== 'l' || c == 'L') {
if (x.size() == 0)
cout << "unable to determinate the smallest number - list is empty" << endl;
else {
int highest;
largest(x, highest);
}
}
}
void print_num(vector <int> x) {
cout << "[ ";
for (auto i:x)
cout << i << " ";
cout << "]";
}
void add_num(vector <int> &x,int &n) {
cout << "enter an integer to add to the list: ";
cin >> n;
x.push_back(n);
cout << n << " added." << endl;
}
void mean(vector <int> x, int &add) {
for (auto i:x)
add += i;
cout << "the average of the elements is: " << static_cast<double>(add)/x.size() << endl;
}
void smallest(vector <int> x, int &lil) {
lil = x.at(0);
for (auto i:x) {
if (i < lil)
lil = i;
}
cout << "the smallest number in the list is: " << lil << endl;
}
void largest(vector <int> x, int &big) {
big = x.at(0);
for (auto i:x) {
if (i > big)
big = i;
}
cout << "the largest number in the list is: " << big << endl;
}
//main
int main() {
vector <int> v {1, 2, 3, 4, 5};
char c{};
cout << "hello, this is your operation menu :)" << endl;
do {
display_menu(c, v);
} while (c != 'q' && c != 'Q');
return 0;
}