-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_Daily_Trainings.cpp
1610 lines (1329 loc) · 32.8 KB
/
My_Daily_Trainings.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<iostream>
using namespace std;
int main(){
//"endl" is used to create a new line
cout<< "The boy is a good bo, no doubt!" <<std::endl;
cout<< "he likes Chike !" <<std::endl;
return 0;
}
//One can do without the (namespace)
#include<iostream>
int main(){
//"\n" is used to create a new line
std::cout<< "The boy is a good bo, no doubt! \n";
std::cout<< "he likes Chike !!";
return 0;
}
#include<iostream>
using namespace std;
int main(){
std::cout<< "The boy is a good bo, no doubt! \n";
std::cout<< "he likes Chike !!";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int myNum;
myNum = 15;
cout << myNum;
return 0;
}
// it will overwrite the previous value:
#include<iostream>
using namespace std;
int main()
{
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
return 0;
}
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
int myNum = 5; // Integer (whole number without decimals)
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
#include<iostream>
using namespace std;
int main()
{
double x = 5.3;
int y = 6;
double sum = x + y;
cout << sum;
return 0;
}
//Declare Many Variables
#include<iostream>
using namespace std;
int main()
{
int x = 5, y = 6, z = 50;
cout << x + y + z;
return 0;
}
//Declare Many Variables
#include<iostream>
using namespace std;
int main()
{
int a = 65, b = 66, c = 67;
cout << a << "\n";
cout << b << "\n";
cout << c;
return 0;
}
//One Value to Multiple Variables
#include<iostream>
using namespace std;
int main()
{
int x, y, z;
x = y = z = 50;
cout << x + y + z;
return 0;
}
//Constant/unchangeable and read-only
#include<iostream>
using namespace std;
int main()
{
const int myNum = 15; // myNum will always be 15
const int minutesPerHour = 60;
const float PI = 3.14;
double s = PI * minutesPerHour;
cout << s;
return 0;
}
//Creating a Simple Calculator
#include<iostream>
using namespace std;
int main()
{
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
// scientific number with an "e" indicate the power of 10:
#include<iostream>
using namespace std;
int main()
{
float f1 = 35e3;
double d1 = 12E4;
cout << f1;
cout << d1;
return 0;
}
// scientific number with an "e" indicate the power of 10:
#include<iostream>
using namespace std;
int main()
{
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun <<"\n"; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int sum1 = 100 + 50; // 150 (100 + 50)
double sum2 = sum1 + 250.50; // 400.5 (150 + 250)
double sum3 = sum2 + sum2; // 800 (400 + 400)
cout << sum1 <<"\n";
cout << sum2 <<"\n";
cout << sum3;
return 0;
}
//Arithmetic Operators*
// + Addition Adds together two values x + y
// - Subtraction Subtracts one value from another x - y
// * Multiplication Multiplies two values x * y
// / Division Divides one value by another x / y
// % Modulus Returns the division remainder x % y
// ++ Increment Increases the value of a variable by 1 ++x
// -- Decrement Decreases the value of a variable by 1 --x
#include<iostream>
using namespace std;
int main()
{
cout<< 100 + 50;
return 0;
}
//Assignment Operators*
// = x = 5 same as x = 5
// += x += 3 same as x = x + 3
// -= x -= 3 same as x = x - 3
// *= x *= 3 same as x = x * 3
// /= x /= 3 same as x = x / 3
// %= x %= 3 same as x = x % 3
// |= x |= 3 same as x = x | 3
// &= x &= 3 same as x = x & 3
// |= x |= 3 same as x = x | 3
// ^= x ^= 3 same as x = x ^ 3
// >>= x >>= 3 same as x = x >> 3
// <<= x <<= 3 same as x = x << 3
#include<iostream>
using namespace std;
int main()
{
int x;
x = 5;
cout<< (x += 3) << endl;
cout<< (x -= 3) << endl;
cout<< (x *= 3) << endl;
cout<< (x /= 3) << endl;
cout<< (x %= 3) << endl;
cout<< (x &= 3) << endl;
cout<< (x |= 3) << endl;
cout<< (x ^= 3) << endl;
cout<< (x >>= 3) << endl;
cout<< (x <<= 3) << endl;
return 0;
}
//Comparison Operators*
// == Equal to x == y
// != Not equal x != y
// > Greater than x > y
// < Less than x < y
// >= Greater than or equal to x >= y
// <= Less than or equal to x <= y
#include<iostream>
using namespace std;
int main()
{
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is greater than 3
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x = 5;
int y = 3;
// returns 1 (true) because 5 is greater than 3
// returns 0 (false) when it is not true
cout << (x == y)<<"\n";
cout << (x != y)<<"\n";
cout << (x < y)<<"\n";
cout << (x > y)<<"\n";
cout << (x <= y)<<"\n";
cout << (x >= y)<<"\n";
return 0;
}
// String Concatenation
#include<iostream>
using namespace std;
int main()
{
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
return 0;
}
// String Concatenation
#include<iostream>
using namespace std;
int main()
{
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
return 0;
}
// You can Concatenation with Append
#include<iostream>
using namespace std;
int main()
{
string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
return 0;
}
// String Concatenation
#include<iostream>
using namespace std;
int main()
{
string firstName = "John ";
string lastName = "Doe";
cout << firstName<< "\n";
cout << lastName;
return 0;
}
// Adding Numbers
#include<iostream>
using namespace std;
int main()
{
//number
int x, y, z;
x = 10;
y = 20;
z = x + y; // z will be 30 (an integer)
cout<< z;
return 0;
}
// Adding Strings
#include<iostream>
using namespace std;
int main()
{
// string concatenation
string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)
cout << z;
return 0;
}
//C++ String Length --length() or size())
#include<iostream>
using namespace std;
int main()
{
string txt, txt2;
txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
txt2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length() <<endl;
cout << "The length of the txt2 string is: " << txt2.length();
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string txt, txt2;
txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
txt2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size() <<endl;
cout << "The length of the txt2 string is: " << txt2.size();
return 0;
}
// C++ Access Strings
#include<iostream>
using namespace std;
int main()
{
string myString = "Hello";
cout << myString[0] << endl;
cout << myString[1] << endl;
cout << myString[2] << endl;
cout << myString[3] << endl;
cout << myString[4];
// Outputs H e l l o
return 0;
}
// C++ Change String Characters
#include<iostream>
using namespace std;
int main()
{
string myString;
myString = "Hello";
myString[1] = 'a';
myString[4] = 'a';
cout << myString;
// Outputs Halla instead of Hello
return 0;
}
// C++ Strings - Special Characters
#include<iostream>
using namespace std;
int main()
{
string txt, txt1, txt2;
txt = "We are the so-called \"Vikings\" from the north.";
txt1 = "The character \\ is called backslash.";
txt2 = "It\'s alright.";
cout << txt << endl << txt1 << endl << txt2;
return 0;
}
// C++ User Input Strings
#include<iostream>
using namespace std;
int main()
{
string firstName;
cout << "Type your first name: ";
cin >> firstName; // get user input from the keyboard
cout << "Your name is: " << firstName;
// Type your first name: John
// Your name is: John
return 0;
}
// C++ User Input Strings using getline()
#include<iostream>
using namespace std;
int main()
{
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
// Type your full name: John Doe
// Your name is: John Doe
return 0;
}
// C++ Strings Omitting Namespace
/*
You might see some C++ programs that runs without the standard namespace library.
The using namespace std line can be omitted and replaced with the std keyword,
followed by the :: operator for string (and cout) objects:
*/
#include<iostream>
#include<string>
int main()
{
std::string greeting = "Hello";
std::cout << greeting;
return 0;
}
// C++ Math-- max $ min
#include<iostream>
using namespace std;
int main()
{
cout << max(5, 10)<< endl;
cout << min(5, 10);
return 0;
}
// C++ CMath-- Header
#include<iostream>
#include<cmath>
int main()
{
std::cout << sqrt(64)<< "\n";
std::cout << round(2.6) << "\n";
std::cout << fabs(2.6) << "\n";
std::cout << log(2) << "\n";
std::cout << ceil(2.678);
return 0;
}
// C++ CMath-- Header(alternatively)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout << sqrt(64)<< "\n";
cout << round(2.6) << "\n";
cout << fabs(2.6) << "\n";
cout << log(2) << "\n";
cout << ceil(2.678);
return 0;
}
// C++ Boolean Expressions
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int y = 9;
cout << (x > y) << endl; // returns 1 (true), because 10 is higher than 9
cout << (x == 10) << endl; // returns 1 (true), because the value of x is equal to 10
cout << (10 == 15); // returns 0 (false), because 10 is not equal to 15
return 0;
}
/*
C++ Boolean Expressions
Real Life Example
*/
#include<iostream>
using namespace std;
int main()
{
int myAge = 25;
int votingAge = 18;
cout << (myAge >= votingAge); // returns 1 (true), meaning 25 year olds are allowed to vote!
return 0;
}
//If statement
#include<iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
//If.. Else statement
#include<iostream>
using namespace std;
int main() {
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
cout << "Old enough to vote!";
}
else
cout << "Not old enough to vote.";
return 0;
}
//If.. Else statement
#include<iostream>
using namespace std;
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
// Else If.. statement
#include<iostream>
using namespace std;
int main(){
int time = 22;
if (time < 10) {
cout << "Good morning.";
}
else if (time < 20) {
cout << "Good day.";
}
else {
cout << "Good evening.";
}
return 0;
}
// Short Hand If...Else
#include<iostream>
using namespace std;
int main(){
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}
// C++ Switch Statements
#include<iostream>
using namespace std;
int main()
{
int day = 4;
switch (day){
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Ssaturday";
break;
case 7:
cout << "Sunday";
break;
}
return 0;
}
// C++ Switch Statements- Default keyword
#include<iostream>
using namespace std;
int main()
{
int day = 5;
switch (day){
case 6:
cout<<"Today is saturday";
break;
case 7:
cout<<"Today is sunday";
break;
default:
cout<< "Looking forward to weekend!";
}
return 0;
}
// C++ while loop
#include<iostream>
using namespace std;
int main(){
int i;
i = 0;
while (i < 5) {
cout<< i << "\n";
++i;
}
}
// C++ do/while loop
#include<iostream>
using namespace std;
int main(){
int i;
i = 0;
do {
cout<< i << "\n";
++i;
}
while (i < 5);
}
// C++ For Loop
/*
When you know exactly how many times you want to loop through a block of code,
use the for loop instead of a while loop:
*/
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
return 0;
}
// Another example
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i <= 10; i = i + 2) {
cout << i << "\n";
}
return 0;
}
// C++ Nested Loops
#include<iostream>
using namespace std;
int main()
{
// Outer loop
for (int i = 1; i <= 2; ++i) {
cout << "Outer: " << i << "\n"; // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; ++j) {
cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
}
}
return 0;
}
// Foreach Loop
#include<iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
return 0;
}
// C++ Break and Continue
// C++ Break
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++) {
if (i == 6) {
break;
}
cout << i << "\n";
}
return 0;
}
// C++ Break and Continue
// C++ Continue
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
cout << i << "\n";
}
return 0;
}
// C++ Break and Continue in while loop
// C++ Break
#include<iostream>
using namespace std;
int main()
{
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break;
}
}
return 0;
}
// C++ Break and Continue in while loop
// C++ Continue
#include<iostream>
using namespace std;
int main()
{
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
return 0;
}
//C++ Arrays [square brackets]
/*
string cars[4];
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
int myNum[3] = {10, 20, 30};
*/
#include<iostream>
using namespace std;
int main()
{
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[2];
return 0;
}
// Change an Array Element
#include<iostream>
using namespace std;
int main()
{
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[2] = "Benz";
cout << cars[2];
return 0;
}
// C++ Arrays and Loops
// C++ Loop Through an Array
#include<iostream>
using namespace std;
int main()
{
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << i << "=" << cars[i] << "\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
return 0;
}
// The foreach Loop
/*
for (type variableName : arrayName) {
// code block to be executed
}
*/
#include<iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
return 0;
}
// C++ Omit Array Size
/*
string cars[] = {"Volvo", "BMW", "Ford"}; // Three arrays
same as
string cars[3] = {"Volvo", "BMW", "Ford"}; // Also three arrays
*/
// Omit Elements on Declaration
#include<iostream>
using namespace std;
int main()
{
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
cout << cars[5];
return 0;
}
// C++ Array Size
#include<iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
cout << sizeof(myNumbers);
/*
You learned from the Data Types chapter that an int type is usually 4 bytes,
so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
*/
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(int);
cout << getArrayLength;
return 0;
}
// Loop Through an Array with sizeof()
#include<iostream>
using namespace std;
int main()
{
//Instead of writing
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//better to write
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) {
cout << myNumbers[i] << "\n";
}
return 0;
}
// you can also use the "for-each" loop:
#include<iostream>
using namespace std;
int main()
{
//better to write
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
return 0;
}
// C++ Multi-Dimensional Arrays
/*
To declare a multi-dimensional array, define the variable type,
specify the name of the array followed by square brackets which specify how many elements the main array has,
followed by another set of square brackets which indicates how many elements the sub-arrays have:
string letters[2][4];
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
string letters[2][2][2] = {
{
{ "A", "B" },
{ "C", "D" }
},
{
{ "E", "F" },
{ "G", "H" }
}
};
*/
// Access the Elements of a Multi-Dimensional Array
/*This statement accesses the value of the element in the