-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmenuDriven.cpp
163 lines (147 loc) · 5.16 KB
/
menuDriven.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// This is a menu driven program which perform operations on a list .
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main(){
char choice;
int i;
vector<int> storage;
do{
cout<<"----------------------"<<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<<"F - Find Frequency of a Number in list"<<endl;
cout<<"C - To Clear the list"<<endl;
cout<<"Q - Quit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 'p':
case 'P': if(storage.size() == 0)
cout<<"[] -the list is empty"<<endl;
else{
cout<<"[";
for(int e: storage)
cout<<" "<<e;
cout<<" ]"<<endl;
}
break;
case 'a':
case 'A': int n;
cout<<"Enter number: ";
cin>>n;
storage.push_back(n);
cout<<n<<" added\n";
break;
case 'm':
case 'M': if(storage.size() == 0)
cout<<"Unable to calculate the mean - no data"<<endl;
else{
cout<< fixed << setprecision(2);
int sum = 0;
for(int e : storage)
sum += e;
cout<<"Mean: "<<(double)sum/storage.size()<<endl;
}
break;
case 's':
case 'S': if(storage.size() == 0)
cout<<"Unable to determine smallest number - list is empty"<<endl;
else{
int small = storage[0];
for(int e : storage)
if(e < small)
small = e;
cout<<"The Smallest number is "<<small<<endl;
}
break;
case 'l':
case 'L': if(storage.size() == 0)
cout<<"Unable to determine largest number - list is empty"<<endl;
else{
int large = storage.at(0);
for(int e : storage)
if(e > large)
large = e;
cout<<"The Largest number is "<<large<<endl;
}
break;
case 'f':
case 'F': if(storage.size() == 0)
cout<<"Unable to Search - list is empty"<<endl;
else{
int count = 0;
int n;
cout<<"Enter number: ";
cin>>n;
for(int e : storage)
if(e == n)
count++;
cout<< "Frequency: "<<count<<endl;
}
break;
case 'c':
case 'C': storage.clear();
cout<<"List Cleared"<<endl;
break;
case 'q':
case 'Q': cout<<"Goodbye..."<<endl;
break;
default: cout<<"Unknown Selection, Please try again"<<endl;
}
}while(choice!='Q' && choice!='q');
}
// SAMPLE OUTPUT
/*
----------------------
P - Print Numbers
A - Add a Number
M - Display Mean of the Numbers
S - Display the Smallest Number
L - Display the Largest Number
F - Find Frequency of a Number in list
C - To Clear the list
Q - Quit
Enter your choice: A
Enter number: 23
23 added
----------------------
P - Print Numbers
A - Add a Number
M - Display Mean of the Numbers
S - Display the Smallest Number
L - Display the Largest Number
F - Find Frequency of a Number in list
C - To Clear the list
Q - Quit
Enter your choice: A
Enter number: 12
12 added
----------------------
P - Print Numbers
A - Add a Number
M - Display Mean of the Numbers
S - Display the Smallest Number
L - Display the Largest Number
F - Find Frequency of a Number in list
C - To Clear the list
Q - Quit
Enter your choice: s
The Smallest number is 12
----------------------
P - Print Numbers
A - Add a Number
M - Display Mean of the Numbers
S - Display the Smallest Number
L - Display the Largest Number
F - Find Frequency of a Number in list
C - To Clear the list
Q - Quit
Enter your choice: q
Goodbye...
PS D:\C++\Challange>
*/