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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
CST116
Module 6: Lab 6
11a
10.10 Learn by Doing Exercises
pp 282-283
10 pts #1
Submit: code & run
CODE:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define ARRAY_SIZE 10
void ReadData(int[ARRAY_SIZE][4], string[ARRAY_SIZE][4]);
void PrintData(int[ARRAY_SIZE][4], string[ARRAY_SIZE][4]);
int main()
{
int NumStudents_Club[ARRAY_SIZE][4]{};
string Pres_Club[ARRAY_SIZE][4]{};
ReadData(NumStudents_Club, Pres_Club);
PrintData(NumStudents_Club, Pres_Club);
}
void ReadData(int intData[ARRAY_SIZE][4], string stringData[ARRAY_SIZE][4])
{
int Club_M = 75;
int again = 1, numb_clubs = 0;
string Club_Name;
while (again && (numb_clubs < ARRAY_SIZE))
{
cout << "Enter the club name: ";
getline(cin >> ws, stringData[numb_clubs][0]);
intData[numb_clubs][0] = again;
cout << "Enter the president's name: ";
getline(cin >> ws, stringData[numb_clubs][1]);
cout << "Enter the amount of members: ";
cin >> intData[numb_clubs][1];
cout << "This club has: $" << Club_M * intData[numb_clubs][1] << endl;
numb_clubs++;
cout << "\n\tEnter 0 to exit or 1 to continue: ";
cin >> again;
}
cout << endl;
}
void PrintData(int intData[ARRAY_SIZE][4], string stringData[ARRAY_SIZE][4])
{
int numb_clubs = 0;
int Club_M = 75;
cout << "|Student Club\t|President\t|Number of Students\t|Club Money\t|" << endl;
while (numb_clubs < ARRAY_SIZE)
{
cout << "| " << stringData[numb_clubs][0] << "\t\t|" << stringData[numb_clubs][1] << "\t\t|" << intData[numb_clubs][1] << "\t\t\t\t\t| " << "$" << Club_M * intData[numb_clubs][1] << "\t\t\t| " << endl;
numb_clubs++;
}
}
RUN:
Enter the club name: Computer Systems Society
Enter the president's name: Kim Cares
Enter the amount of members: 49
This club has: $3675
Enter 0 to exit or 1 to continue: 1
Enter the club name: Society of Women Engineers
Enter the president's name: Jeanie Queen
Enter the amount of members: 51
This club has: $3825
Enter 0 to exit or 1 to continue: 1
Enter the club name: Sigma Tau Gamma
Enter the president's name: Storm Drain
Enter the amount of members: 241
This club has: $18075
Enter 0 to exit or 1 to continue: 1
Enter the club name: Trekkies
Enter the president's name: C. Kirk
Enter the amount of members: 230
This club has: $17250
Enter 0 to exit or 1 to continue: 1
Enter the club name: Home Brewers
Enter the president's name: Ross Coe
Enter the amount of members: 15
This club has: $1125
Enter 0 to exit or 1 to continue: 1
Enter the club name: High Altitude Ballooning
Enter the president's name: Justin Time
Enter the amount of members: 19
This club has: $1425
Enter 0 to exit or 1 to continue: 1
Enter the club name: Rugby
Enter the president's name: Ryan Johns
Enter the amount of members: 25
This club has: $1875
Enter 0 to exit or 1 to continue: 1
Enter the club name: IEEE
Enter the president's name: Marc Bansmere
Enter the amount of members: 36
This club has: $2700
Enter 0 to exit or 1 to continue: 1
Enter the club name: International Club
Enter the president's name: King Mbonkum
Enter the amount of members: 102
This club has: $7650
Enter 0 to exit or 1 to continue: 1
Enter the club name: Dance Club
Enter the president's name: Will Shaver
Enter the amount of members: 64
This club has: $4800
Enter 0 to exit or 1 to continue: 0
|Student Club |President |Number of Students |Club Money
| Computer Systems Society |Kim Cares |49 | $3675
| Society of Women Engineers |Jeanie Queen |51 | $3825
| Sigma Tau Gamma |Storm Drain |241 | $18075
| Trekkies |C. Kirk |230 | $17250
| Home Brewers |Ross Coe |15 | $1125
| High Altitude Ballooning |Justin Time |19 | $1425
| Rugby |Ryan Johns |25 | $1875
| IEEE |Marc Bansmere |36 | $2700
| International Club |King Mbonkum |102 | $7650
| Dance Club |Will Shaver |64 | $4800
11b
10.14 Debugging Exercises
pp 289-292
10 pts #1
Submit: code & run
* Debugging Exercise 1
CODE:
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
void GetAndDisplayWelcomeInfo ( );
void FunctionOne ( int varX[], int varY[] );
void FunctionTwo ( const int varX[], const int varY[], int varZ[] );
void PrintFunction ( const int varX[], const int varY[],
const int varZ[] );
const int SIZE = 5;
int main ( )
{
int varX[5];
int varY[SIZE];
int varZ[SIZE]; // Notice how we used the const here!
// Breakpoint 1
// Put breakpoint on the following line
GetAndDisplayWelcomeInfo ( );
FunctionOne ( varX, varY );
// Breakpoint 3
// Put breakpoint on the following line
FunctionTwo ( varX, varY, varZ );
PrintFunction ( varX, varY, varZ );
return 0;
}
void GetAndDisplayWelcomeInfo ( )
{
char name[2][20]; // First name in row 0, last name in row 1
cout << "Please enter your first name: ";
cin >> name[0];
cout << "\nPlease enter your last name: ";
cin >> name[1];
// Breakpoint 2
// Put breakpoint on the following line
cout << "\n\n\tWelcome " << name[0] << " " << name[1]
<< "!\n\t Hope all is well \n\n";
}
void FunctionOne ( int varX[], int varY[] )
{
for ( int x = 0; x < SIZE; x++ ) // NOTICE '<' NOT <=
// Breakpoint 4
// Put breakpoint on the following line
varX[x] = x;
for ( int x = 0; x < 5; x++ )
varY[x] = x + 100;
}
void FunctionTwo ( const int varX[], const int varY[], int varZ[] )
{
for ( int x = 0; x < SIZE; x++ ) // Notice the const SIZE here
varZ[x] = varX[x] + varY[x];
}
void PrintFunction ( const int varX[20], const int varY[20],
const int varZ[20] )
{
int x;
cout << " \t x \t y \t z\n\n";
for ( x = 0; x < SIZE; x++ )
cout << "\t" << setw ( 3 ) << varX[x]
<< "\t " << varY[x]
<< "\t " << varZ[x] << endl;
}
RUN:
Please enter your first name: Isabella
Please enter your last name: Mon
Welcome Isabella Mon!
Hope all is well
x y z
0 100 100
1 101 102
2 102 104
3 103 106
4 104 108
* Debugging Exercise 2
CODE:
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
void GetAndDisplayWelcomeInfo();
void FunctionOne(int varX[], int varY[]);
void FunctionTwo(const int varX[], const int varY[], int varZ[]);
void PrintFunction(const int varX[], const int varY[],
const int varZ[]);
const int SIZE = 10;
int main()
{
int varX[SIZE];
int varY[SIZE];
int varZ[SIZE]; // Notice how we used the const here!
// Breakpoint 1
// Put breakpoint on the following line
GetAndDisplayWelcomeInfo();
FunctionOne(varX, varY);
// Breakpoint 3
// Put breakpoint on the following line
FunctionTwo(varX, varY, varZ);
PrintFunction(varX, varY, varZ);
return 0;
}
void GetAndDisplayWelcomeInfo()
{
char name[2][20]; // First name in row 0, last name in row 1
cout << "Please enter your first name: ";
cin >> name[0];
cout << "\nPlease enter your last name: ";
cin >> name[1];
// Breakpoint 2
// Put breakpoint on the following line
cout << "\n\n\tWelcome " << name[0] << " " << name[1]
<< "!\n\t Hope all is well \n\n";
}
void FunctionOne(int varX[], int varY[])
{
for (int x = 0; x < SIZE; x++) // NOTICE '<' NOT <=
// Breakpoint 4
// Put breakpoint on the following line
varX[x] = x;
for (int x = 0; x < 5; x++)
varY[x] = x + 100;
}
void FunctionTwo(const int varX[], const int varY[], int varZ[])
{
for (int x = 0; x < SIZE; x++) // Notice the const SIZE here
varZ[x] = varX[x] + varY[x];
}
void PrintFunction(const int varX[20], const int varY[20],
const int varZ[20])
{
int x;
cout << " \t x \t y \t z\n\n";
for (x = 0; x < SIZE; x++)
cout << "\t" << setw(3) << varX[x]
<< "\t " << varY[x]
<< "\t " << varZ[x] << endl;
}
RUN:
Please enter your first name: Isabella
Please enter your last name: Mon
Welcome Isabella Mon!
Hope all is well
x y z
0 100 100
1 101 102
2 102 104
3 103 106
4 104 108
5 105 110
6 106 112
7 107 114
8 108 116
9 109 118
* Debugging Exercise 3
CODE:
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
void GetAndDisplayWelcomeInfo();
void FunctionOne(int varX[], int varY[]);
void FunctionTwo(int varX[], const int varY[], int varZ[]);
void PrintFunction(const int varX[], const int varY[], const int varZ[]);
const int SIZE = 10;
int main()
{
int varX[SIZE];
int varY[SIZE];
int varZ[SIZE]; // Notice how we used the const here!
// Breakpoint 1
// Put breakpoint on the following line
GetAndDisplayWelcomeInfo();
FunctionOne(varX, varY);
// Breakpoint 3
// Put breakpoint on the following line
FunctionTwo(varX, varY, varZ);
varZ[0] = -99;
PrintFunction(varX, varY, varZ);
return 0;
}
void GetAndDisplayWelcomeInfo()
{
char name[2][20]; // First name in row 0, last name in row 1
cout << "Please enter your first name: ";
cin >> name[0];
cout << "\nPlease enter your last name: ";
cin >> name[1];
// Breakpoint 2
// Put breakpoint on the following line
cout << "\n\n\tWelcome " << name[0] << " " << name[1]
<< "!\n\t Hope all is well \n\n";
}
void FunctionOne(int varX[], int varY[])
{
for (int x = 0; x < SIZE; x++) // NOTICE '<' NOT <=
// Breakpoint 4
// Put breakpoint on the following line
varX[x] = x;
for (int x = 0; x < 5; x++)
varY[x] = x + 100;
}
void FunctionTwo(int varX[], const int varY[], int varZ[])
{
varX[1] = 99;
for (int x = 0; x < SIZE; x++) // Notice the const SIZE here
varZ[x] = varX[x] + varY[x];
}
void PrintFunction(const int varX[20], const int varY[20], const int varZ[20])
{
int x;
cout << " \t x \t y \t z\n\n";
for (x = 0; x < SIZE; x++)
cout << "\t" << setw(3) << varX[x]
<< "\t " << varY[x]
<< "\t " << varZ[x] << endl;
}
RUN:
Please enter your first name: Isabella
Please enter your last name: Mon
Welcome Isabella Mon!
Hope all is well
x y z
0 100 -99
99 101 200
2 102 104
3 103 106
4 104 108
5 105 110
6 106 112
7 107 114
8 108 116
9 109 118
11c
10.15 Programming Exercises
pp 292-293
10 pts #1
Submit: code & run
Total: 30 pts
CODE:
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
bool IsPalindrome(string cString);
bool IsAlphaStr(string cString);
int CountChar(string cString, char input);
int main()
{
string userdata;
cout << "Enter the string: ";
cin >> userdata;
if (IsPalindrome(userdata))
{
cout << userdata << " is a palindrome\n" << endl;
}
else
{
cout << userdata << " is not a palindrome\n" << endl;
}
if (IsAlphaStr(userdata)) {
cout << userdata << " is alphabetic\n" << endl;
}
else
{
cout << userdata << " is not alphabetic\n" << endl;
}
char input;
cout << "\n\tWhat character do you want to count? ";
cin >> input;
cout << input << " appeared " << CountChar(userdata, input) << " times." << endl;
return 0;
}
bool IsPalindrome(string cString)
{
for (int i = 0; i < cString.length() / 2; i++)
{
if (cString[i] != cString[cString.length() - 1 - i])
return false;
}
return true;
}
bool IsAlphaStr(string cString)
{
for (int i = 0; i < cString.length(); i++)
{
if (((cString[i] >= 'A' && cString[i] <= 'Z') || (cString[i] >= 'a' && cString[i] <= 'z')))
{
return false;
}
}
return true;
}
int CountChar(string cString, char input)
{
int count = 0;
for (int i = 0; i < cString.length(); i++)
{
if (cString[i] == input)
{
count++;
}
}
return count;
}
RUN1:
Enter the string: hello
hello is not a palindrome
hello is not alphabetic
What character do you want to count? l
l appeared 2 times.
RUN2:
Enter the string: racecar
racecar is a palindrome
racecar is not alphabetic
What character do you want to count? e
e appeared 1 times.
|