Finished up the last few problems.
[mcarberry/cisp301.git] / main.cpp
CommitLineData
569fc9e9 1#include <iostream>
c24c0743
MC
2#include <cstdlib>
3#include <ctime>
4d582974 4#include <vector>
c24c0743 5
569fc9e9
MC
6using namespace std;
7
4d582974
MC
8void ch4pe2()
9{
10 for(int i = 99; i>95; i--){
11 cout << i << " bottles of rootbeer on the wall" << endl;
12 cout << i << " bottles of rootbeer" << endl;
13 cout << "Take one down and pass it around" << endl;
14 cout << "One less bottle of rootbeer on the wall" << endl << endl;
15 }
16}
17
18void ch4pe4()
c24c0743
MC
19{
20 int A, B;
21
22 for (A=1; A < 10; A++)
23 {
24 for (B=1; B <= A ; B++)
25 {
26 cout << B << " ";
27 }
28 cout << " " << endl;
29 }
30}
31
245837be
MC
32void ch4pe5(void){
33 for(int i=1; i < 10; i++){
34 for(int j=0; j < i; j++){
35 cout << i << " ";
36 }
37 cout << endl;
38 }
39}
c24c0743 40
569fc9e9
MC
41void ch4pe6(void)
42{
43 for (int X=0;X<10;X++)
44 {
45 for (int R=0;R<(9-X);R++)
46 {
47 cout<<" ";
48 }
49 for(int D=0; D<X; D++)
50 {
51 cout<<"o";
52 }
53 cout<<endl;
54 }
55}
56
245837be
MC
57void ch4pe7(void){
58 int lines = 10;
59 for(int i=1; i <= lines; i++){
60 for(int j=0; j < (lines-i); j++) cout << " ";
61 for(int j=1; j < i; j++) cout << "0";
62 cout << "0";
63 for(int j=1; j < i; j++) cout << "0";
64 cout << endl;
65 }
66}
67
569fc9e9
MC
68void ch4pe8(void)
69{
70 for (int X=0;X<10;X++)
71 {
72 for (int R=0;R<(9-X);R++)
73 {
74 cout<<" ";
75 }
76 for(int D=0; D<X; D++)
77 {
78 cout<<"o";
79 }
80 for(int E=0;E<(X-1);E++)
81 {
82 cout<<"o";
83 }
84 cout<<endl;
85 }
86 for(int X=10;X>0;X--)
87 {
88 for(int D=0;D<(11-X);D++)
89 {
90 cout<<" ";
91 }
92 for(int R=0;R<(X-3);R++)
93 {
94 cout<<"o";
95 }
96 for(int E=0;E<(X-2);E++)
97 {
98 cout<<"o";
99 }
100 cout<<endl;
101 }
102}
103
4d582974
MC
104void ch5pe2(){
105 vector<int> input (5);
106 int tot = 0;
107 float avg;
108
109 for(int i=0;i<5;i++){
110 cout << "Please input a number: ";
111 cin >> input[i];
112 tot += input[i];
113 }
114 avg = float(tot)/5.0;
115
116 cout << "The average is " << avg << endl << endl;
117 cout << "The following are greater than the average: " << endl;
118 for(int i=0; i<5; i++){
119 if(input[i]>avg) cout << input[i] << endl;
120 }
121}
122
245837be
MC
123void ch5pe3(){
124 vector<int> input (10);
125 int target = 0;
126 int count = 0;
127
128 for(int i=0;i<10;i++){
129 cout << "Please input a number: ";
130 cin >> input[i];
131 }
132 cout << "Please input target value: ";
133 cin >> target;
134
135 for(int i=0;i<10;i++){
136 if(input[i] = target) count++;
137 }
138
139 cout << "The target value appears " << count << " times in the array";
140}
141
cf2d4387
MC
142void ch5pe4()
143{
144 int arrayPosi[9]={0}, Position, HitorMiss, Bat, n, Leave;
145 double BatAvrg, arrayBat[9]={0};
146
147 Leave=0;
148 n=0;
149 HitorMiss = 0;
150 Bat = 0;
151
152 while (Leave != -1)
153 {
154 for (n=0; n <9; n++)
155 {
156 Position = arrayPosi[n];
157 cout <<"For Position " << n+1 << ", input a 1 for a hit and a 0 for a miss.";
158 cin >> HitorMiss;
159 arrayBat[n] = HitorMiss+arrayBat[n];
160 }
161 Bat++;
162 cout << "Done? Input 0 To Input Another Set, Input -1 To Exit. ";
163 cin >> Leave;
164 }
165 for (n=0; n<9; n++)
166 {
167 BatAvrg = arrayBat[n]/Bat;
168 cout << "Position " << n+1 << " batting average is " << BatAvrg << "" << endl;
169 }
170}
171
245837be
MC
172void ch5pe5(){
173 vector<int> atBats(9);
174 vector<int> hits(9);
175 vector<int> bases(9);
176 int pos, rec;
177
178 bool over = false;
179
180 while(!over){
181 cout << "Position: ";
182 cin >> pos;
183 if(pos != -1){
184 cout << "Record: ";
185 cin >> rec;
186 if(rec > 0){
187 hits[pos-1]++;
188 bases[pos-1] += rec;
189 }
190 atBats[pos-1]++;
191 }
192 else{
193 over = true;
194 }
195 }
196
197 cout << "The batting average for each position was: " << endl;
198 for(int i = 0; i < 9; i++){
199 cout << "Position " << i+1 << " batting average is " << float(hits[i])/float(atBats[i]) << endl;
200 }
201 cout << endl << "The slugging percentage for each position was: " << endl;
202 for(int i = 0; i < 9; i++){
203 cout << "Position " << i+1 << " slugging percentage is " << float(bases[i])/float(atBats[i]) << endl;
204 }
205}
206
c24c0743
MC
207void ch5pe6()
208{
209 int T=0;
210 double D=0,B=0,N=0,I=0;
211 double R;
212 double Total[12]={};
213 srand(time(NULL));
214
215
216 cout<<"How many rolls of the dice? ";
217 cin>>N;
218
219 for(int C=0;C<N;C++)
220 {
221 D=(rand()%6)+1;
222 B=(rand()%6)+1;
223 T=D+B-2;//-2 so that the number 2 will match with array slot 0
224 Total[T]++;//to increase the count of the slot matching the number
225 }
226 for(int C=0;C<11;C++)
227 {
228 R=(Total[C]/N)*100;
229 // cout<<Total[C]<<" "<<N<<" "<<R<<endl;// for debugging
230 cout<<"The number "<<C+2<<" was rolled "<<Total[C]<<" times or "<<R<<" percent of the time"<<endl;
231 }
232}
233
245837be
MC
234void ch5pe7(){
235 vector<string> usr(10);
236 vector<string> pass(10);
237 string inputuser, inputpass;
238 bool complete = false;
239 bool usrfnd = false;
240
241 for(int i=0;i<10;i++){
242 cout << "Please enter a username: ";
243 cin >> usr[i];
244 cout << "Please enter a password for " << usr[i] << ": ";
245 cin >> pass[i];
246 }
247
248 while(!complete){
249 int pos = 0;
250
251 cout << "Please enter your username: ";
252 cin >> inputuser;
253 for(int i=0;i<10;i++){
254 if(usr[i] == inputuser){
255 pos = i;
256 usrfnd = true;
257 }
258 }
259 if(usrfnd){
260 cout << "Please enter your password: ";
261 cin >> inputpass;
262
263 if(pass[pos] == inputpass){
264 cout << "Access Granted" << endl;
265 complete = true;
266 }
267 else{
268 cout << "Username and password do not match" << endl << endl;
269 }
270 }
271 else{
272 cout << "Username not found" << endl << endl;
273 }
274 }
275}
276
277void ch5pe8(){
278 vector<int> scores(6);
279 int swaps = 1;
280 int tot = 0;
281
282 for(int i = 0; i < 6; i++){
283 cout << "Input score: ";
284 cin >> scores[i];
285 }
286
287 while(swaps != 0){
288 swaps = 0;
289 for(int i=0; i < 5; i++){
290 if(scores[i] > scores[i+1]){
291 int a = scores[i];
292 scores[i] = scores[i+1];
293 scores[i+1] = a;
294 swaps++;
295 }
296 }
297 }
298 for(int i=1; i < 5; i++) tot+=scores[i];
299
300 cout << "Highest score : " << scores[5] << endl;
301 cout << "Lowest score : " << scores[0] << endl;
302 cout << "Olympic Average : " << float(tot)/4.0 << endl;
303
304
305}//TODO
306
569fc9e9 307int main(void){
4d582974
MC
308 cout << "Please select the program you would like to run:" << endl;
309 cout << "\t" << "1. Ch.4 PE 2" << endl;
310 cout << "\t" << "2. Ch.4 PE 4" << endl;
311 cout << "\t" << "3. Ch.4 PE 5" << endl;
312 cout << "\t" << "4. Ch.4 PE 6" << endl;
313 cout << "\t" << "5. Ch.4 PE 7" << endl;
314 cout << "\t" << "6. Ch.4 PE 8" << endl;
315 cout << "\t" << "7. Ch.5 PE 2" << endl;
316 cout << "\t" << "8. Ch.5 PE 4" << endl;
317 cout << "\t" << "9. Ch.5 PE 5" << endl;
318 cout << "\t" << "10. Ch.5 PE 6" << endl;
319 cout << "\t" << "11. Ch.5 PE 7" << endl;
320 cout << "\t" << "12. Ch.5 PE 8" << endl;
321 cout << endl << "#: ";
322
323 int choice;
324 cin >> choice;
325 switch(choice)
326 {
327 case 1:
328 ch4pe2();
329 break;
c24c0743
MC
330 case 2:
331 ch4pe4();
332 break;
245837be
MC
333 case 3:
334 ch4pe5();
335 break;
4d582974
MC
336 case 4:
337 ch4pe6();
338 break;
245837be
MC
339 case 5:
340 ch4pe7();
341 break;
4d582974
MC
342 case 6:
343 ch4pe8();
344 break;
345 case 7:
346 ch5pe2();
347 break;
cf2d4387
MC
348 case 8:
349 ch5pe4();
350 break;
245837be
MC
351 case 9:
352 ch5pe5();
353 break;
c24c0743
MC
354 case 10:
355 ch5pe6();
356 break;
245837be
MC
357 case 11:
358 ch5pe7();
359 break;
360 case 12:
361 ch5pe8();
362 break;
4d582974
MC
363 default:
364 cout << "Sorry, option unimplemented!";
365 break;
366 }
569fc9e9 367
4d582974
MC
368 char dontend; cin >> dontend;
369 return 0;
569fc9e9
MC
370}
371
372
373