/
Practice.cpp
604 lines (485 loc) · 14.4 KB
/
Practice.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
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
599
600
601
602
603
604
//============================================================================
// Name : Practice.cpp
// Author : HMS
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <vector>
#include <sstream>
#include <iomanip> //set for precision
#include <limits> //set for numeric limits
#include <cstring> //for strcmp
#include <stdio.h>
#include <string>
#include <utility>
using namespace std;
int summation(int);
void display_input_output();
void test_overflow();
void test_constant_variable();
void test_array_size();
void test_vector();
string split_string(string);
string abbrev_name(string);
bool consecutiveDucks (unsigned int);
void data_conversion();
void display_pyramid();
void logical_operators(int age, bool parental_consent, bool ssn, bool accidents);
void convert_cents();
void convert_cents_modulus();
void shipping_cost_calculator();
void conditional_operator();
int count_numbers(const vector<int> &vec);
int calculate_pairs(vector<int> vec);
int points(const std::array<std::string, 10>& games);
int remove_duplicate(std::array<int,5> A,int N);
std::pair<std::string, std::string> capitalize(const std::string &s);
unsigned halving_sum(unsigned n);
int main() {
// display_input_output();
// test_overflow();
// test_constant_variable();
// test_array_size();
// test_vector();
// cout << split_string("sam harris") << endl;
// cout << abbrev_name("Noah Centineo") << endl;
// cout << consecutiveDucks (64) << endl;
// data_conversion();
// display_pyramid();
// bool parental_consent{1};
// bool ssn{1};
// bool accidents{0};
// int age = 16;
// logical_operators(age,parental_consent,ssn,accidents);
// convert_cents();
// convert_cents_modulus();
// shipping_cost_calculator();
// conditional_operator();
// vector <int> v {100,101,103,-50,-99,30,70};
// cout << "Numbers of intergers presence in vector: " << count_numbers(v) << endl;
// vector<int> vec{2,4,6,8};
// cout << calculate_pairs(vec) << endl;
// const std::array<std::string, 10>& games{"1:0","2:0","3:0","4:0","2:1","3:1","4:1","3:2","4:2","4:3"};
// cout << "count of points: " << points(games) << endl;
// int N{};
// std::array<int,5> A{2,4,2,6,7};
// N = sizeof(A)/sizeof(A[0]);
// cout << "Size of current array: " << remove_duplicate(A,N) << endl;
// capitalize("abracadabra");
// cout << strcmp("Hello there" , "Good") << endl;
// cout << "Result: " << summation(8) << endl;
unsigned n = 127;
halving_sum(n);
return 0;
}
int summation(int num){
// Code Here
int value;
int result = 0;
for(int i = 0; i <num; i++)
{
value = i + 1;
result = result + value;
cout << "value = " << value << endl;
cout << "result = " << result << endl;
}
return num*(num+1)/2;
// return result;
}
void display_input_output(){
int number;
cout << "Enter your favorite number between 1 and 100: ";
cin >> number;
cout << "Amazing!! That's my favorite number too!" << endl;
cout << "No really!!, " << number << " is my favorite number!" << endl;
}
void test_overflow(){
long long num_of_ppl {7600000000};
cout << "There are about " << num_of_ppl << " people on earth." << endl;
}
void test_constant_variable(){
cout << "Estimate for carpet cleaning service" << endl;
cout << "\nNumber of small rooms: ";
int small_rooms {0};
cin >> small_rooms;
cout << "Number of large rooms: ";
int large_rooms {0};
cin >> large_rooms;
const double small_room_price {25.0};
const double large_room_price {35.0};
const double tax_rate {0.06};
const int valid_days {30}; //estimate validation
cout << "Price per small rooms: $" << small_rooms * small_room_price << endl;
cout << "Price per large rooms: $" << large_rooms * large_room_price << endl;
cout << "Cost: $" << (small_rooms * small_room_price) + (large_rooms * large_room_price) << endl;
cout << "Tax: $" << ((small_rooms * small_room_price) + (large_rooms * large_room_price))*tax_rate << endl;
cout << "===============================================" << endl;
cout << "Total estimate: $" << ((small_rooms * small_room_price) + (large_rooms * large_room_price)) +
(((small_rooms * small_room_price) + (large_rooms * large_room_price))*tax_rate) << endl;
cout << "This estimate is valid for " << valid_days << " days" <<endl;
}
void test_array_size(){
int numbers[]{1,2,3,4};
cout << "Input another number: ";
cin >> numbers[4];
cout << numbers[0] << "," << numbers[1] << "," << numbers[2] << "," << numbers[3] << "," << numbers[4];
}
void test_vector(){
vector <int> vector1;
vector <int> vector2;
vector1.push_back(10);
vector1.push_back(20);
cout << vector1.at(0) << endl;
cout << vector1.at(1) << endl;
cout << "vector1 has " << vector1.size() << " element(s).\n"<< endl;
vector2.push_back(100);
vector2.push_back(200);
cout << vector2.at(0) << endl;
cout << vector2.at(1) << endl;
cout << "vector2 has " << vector2.size() << " element(s).\n"<< endl;
vector <vector<int>> vector_2d;
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);
cout << "2D Vector...\n" << vector_2d.at(0).at(0);
cout << " " << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) ;
cout << " " << vector_2d.at(1).at(1) << endl;
vector1.at(0) = 1000;
cout << "\nAfter vector1 changed... \n" << vector_2d.at(0).at(0);
cout << " " << vector_2d.at(0).at(1) << endl;
cout << vector_2d.at(1).at(0) ;
cout << " " << vector_2d.at(1).at(1) << endl;
cout << "\nVector1...\n" << vector1.at(0) << endl;
cout << vector1.at(1) << endl;
}
string split_string(string str){
vector <string> result;
istringstream ss(str);
//split string by space
for(string s; ss >> s; ){
result.push_back(s);
}
stringstream combine_text;
//get the first character of an array
string first = result.at(0);
string second = result.at(1);
//assign upper case to first index of character
first[0] = toupper(first[0]);
second[0] = toupper(second[0]);
//concatenate string
combine_text << first.at(0) << "." << second.at(0);
string shortened = combine_text.str();
return shortened;
}
string abbrev_name(string name){
/*
* to return only the abbreviation of a name
* e.g. Michael Jordan = M.J
*
* */
string abbrev {""};
abbrev = toupper(name.at(0));
abbrev += ".";
abbrev += toupper(name.at(name.find(" ")+1));
return abbrev;
}
bool consecutiveDucks (unsigned int num){
/*
* to check if given number is a sum of two or more consecutive positive numbers
* e.g. 24 = 9+8+7
*
* */
bool check {1};
unsigned int count = 0;
for(unsigned int L = 1; L * (L+1) < 2*num; L++){
double a = (1.0* num-(L*(L+1))/2)/(L+1);
if(a - (int)a == 0.0){
count++;
}
}
if(count > 0){
return check;
}
return !check;
}
void data_conversion(){
int total{};
int num1{},num2{},num3{};
const int count{3};
cout << "Enter 3 numbers seperated by space: ";
cin >> num1 >> num2 >> num3;
total = num1+num2+num3;
double average{0.0};
average = total/count;
cout << "The sum of the numbers is " << total << endl;
cout << "The average of the numbers is " << average << endl;
double new_average{0.0};
new_average = static_cast<double>(total)/count;
cout << "The exact average of the numbers is " << new_average << endl;
}
void display_pyramid(){
int i{},j{},k{};
for(k=1;k<=5;k++){
for(j=1;j<=5-k;j++){
cout << " ";
}
for(i=1;i<=2*k-1;i++){
cout << "*";
}
cout << endl;
}
}
void logical_operators(int age, bool parental_consent, bool ssn, bool accidents) {
if ((age >=18 || (age >= 16 && parental_consent)) && ssn && !accidents)
cout << "Yes, you can work.";
return;
}
void convert_cents(){
cout << "Please enter amount in cents: ";
unsigned int cents{};
cin >> cents;
const int dollar{100},quarter{25},dime{10},
nickel{5},penny{1};
unsigned int cents_do{},cents_qu{},cents_di{},
cents_ni{},cents_pe{};
unsigned int current_cents{},balance{};
cents_do = cents/dollar;
current_cents = dollar * cents_do;
balance = cents - (dollar * cents_do);
cents_qu = balance/quarter;
current_cents = current_cents + (quarter * cents_qu);
balance -= quarter * cents_qu;
cents_di = balance/dime;
current_cents = current_cents + (dime * cents_di);
balance -= dime * cents_di;
cents_ni = balance/nickel;
current_cents = current_cents + (nickel * cents_ni);
balance -= nickel * cents_ni;
cents_pe = balance/penny;
cout << "dollars : " << cents_do << endl;
cout << "quarters : " << cents_qu << endl;
cout << "dimes : " << cents_di << endl;
cout << "nickels : " << cents_ni << endl;
cout << "pennies : " << cents_pe << endl;
}
void convert_cents_modulus(){
cout << "Please enter amount in cents: ";
int cents{};
cin >> cents;
const int dollar{100},quarter{25},dime{10},nickel{5};
int cents_do{},cents_qu{},cents_di{},
cents_ni{},cents_pe{};
int balance{};
cents_do = cents/dollar;
balance = cents % dollar;
cents_qu = balance/quarter;
balance %= quarter;
cents_di = balance/dime;
balance %= dime;
cents_ni = balance/nickel;
balance %= nickel;
cents_pe = balance;
cout << "dollars : " << cents_do << endl;
cout << "quarters : " << cents_qu << endl;
cout << "dimes : " << cents_di << endl;
cout << "nickels : " << cents_ni << endl;
cout << "pennies : " << cents_pe << endl;
}
void shipping_cost_calculator(){
int length{},width{},height{},pkg_volume{};
double total_cost{};
const double base_cost{2.50};
const double greater_100_surcharge{0.10};
const double greater_500_surcharge{0.25};
cout << "Please enter package dimension in inches" << endl;
cout << "=========================================" << endl;
cout << "\nlength : ";
cin >> length;
cout << "width : ";
cin >> width;
cout << "height : ";
cin >> height;
if(length <= 10 && width <= 10 && height <= 10){
pkg_volume = length * width * height;
if(pkg_volume >= 100 && pkg_volume <= 499){
total_cost = base_cost + (base_cost*greater_100_surcharge);
}
else if(pkg_volume >= 500){
total_cost = base_cost + (base_cost*greater_500_surcharge);
}
else{
total_cost = base_cost;
}
cout << fixed << setprecision(2);
cout << "Shipping total cost is $" << total_cost << endl;
}
else{
cout << "Dimension is more than 10 inches!" << endl;
cout << "We cannot ship it." << endl;
}
}
void conditional_operator(){
int num1{},num2{};
cout << "Enter a number: ";
cin >> num1 >> num2;
if(num1 != num2){
cout << ((num1 > num2) ? num1 : num2) << " is larger." << endl;
cout << ((num1 < num2) ? num1 : num2) << " is smaller." << endl;
}
else{
cout << "The values are the same." << endl;
}
}
int count_numbers(const vector<int> &vec){
/*
* The type_size size_t is part of the std library. It is the unsigned integer type
* result of the sizeof operator since C++11 meaning it cannot be zero or a negative
* number and <int> is a signed type which can have negative numbers. Using int will
* work, however will cause warnings with conversions when getting the length of types
* such as arrays and vectors . Also, size_t can theoretically store the maximum size
* of a possible object of any type(including arrays and vectors(array.size(); vector.size();
* sizeof(vector); sizeof(array);) size_t is commonly used for indexing and loop counting.
* When indexing containers like string , array, or vector, the typedef size_type provided
* by the containers is usually defined as a synonym for size_t.
*/
int count{0};
size_t index{};
while(index < vec.size() && vec.at(index)!=-99){
count++;
index++;
}
return count;
}
int calculate_pairs(vector<int> vec){
/*
* Given the vector vec to be {1,2,3}, the possible pairs are
* (1,2),(1,3) and (2,3). So the result would be (1*2)+(1*3)+(2*3)
* which is 11
*/
int result{};
for(size_t x{};x<vec.size();x++){
for(size_t y{x+1};y<vec.size();y++){
result += (vec.at(x)*vec.at(y));
}
}
return result;
}
int points(const std::array<std::string, 10>& games){
/*
* Our football team finished the championship. The result of each match look like "x:y".
* Results of all matches are recorded in the collection.
* For example: ["3:1", "2:2", "0:1", ...]
* Write a function that takes such collection and counts the points of our team in the
* championship. Rules for counting points for each match:
* if x>y - 3 points
* if x<y - 0 point
* if x=y - 1 point
* Notes:
* there are 10 matches in the championship
* 0 <= x <= 4
* 0 <= y <= 4
*/
int count_of_points{};
string result_of_match{};
string x{},y{};
for(auto result:games){
result_of_match = result;
x = result_of_match.at(0);
y = result_of_match.at(2);
// if(x > y){
// count_of_points += 3;
// }
// else if(x < y){
// count_of_points += 0;
// }
// else if(x == y){
// count_of_points += 1;
// }
count_of_points += (x > y)? 3 : (x < y)? 0 : 1;
}
return count_of_points;
}
int remove_duplicate(std::array<int,5> A,int N){
// int y{1};
cout << "Before [ ";
for(auto z:A){
cout << z << " ";
}
cout << "]" << endl;
// for(int x{0};x<N;x++){
// for(int y{1};y<N;y++){
// cout << "A[x] first : " << A[x] <<endl;
// cout << "A[y] first : " << A[y] <<endl;
// if(A[x] == A[y]){
// for(int b{y};b<N-1;++b){
// cout << "A[b] : " << A[b] <<endl;
// cout << "A[b+1] : " << A[b+1] <<endl;
// A[b] = A[b+1];
//
// }
// }
// }
// }
for(int x{0};x<N;x++){
for(int y{1};y<N;y++){
cout << "\nA[x] first : " << A[x] <<endl;
cout << "A[y] first : " << A[y] <<endl;
if(A[x] == A[y]){
// int a = A[y];
N -=1;
break;
// for(int b{y};b<N-1;++b){
cout << "A[y] : " << A[y] <<endl;
cout << "A[y+1] : " << A[y+1] <<endl;
// }
}
A[x] = A[x+1];
for(auto z:A){
cout << z << " ";
}
}
}
cout << "After [ ";
for(auto z:A){
cout << z << " ";
}
cout << "]" << endl;
int a{};
a = sizeof(A)/sizeof(A[0]);
return a;
}
std::pair<std::string, std::string> capitalize(const std::string &s)
{
std::string letter{};
for(size_t digit{0}; digit < s.length(); digit++){
if((digit%2) == 0){
letter += std::toupper(s[digit]);
}
else{
letter += std::tolower(s[digit]);
}
}
std::string words{};
for(size_t digit{0}; digit < s.length(); digit++){
if((digit%2) == 0){
words += std::tolower(s[digit]);
}
else{
words += std::toupper(s[digit]);
}
}
cout << "['" << letter << "', '" << words << "' ]" << endl;
return {letter, words};
}
unsigned halving_sum(unsigned n){
unsigned int division{2};
int c{};
c = n;
while(division < n){
c += (n/division);
division *= 2;
};
return c;
}