-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworlds.cpp
471 lines (366 loc) · 8.81 KB
/
helloworlds.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
#include <iostream>
int main (){
// This comment
/*This
multi
line
comments
okay*/
// Hello World
// std output = string that stored
// uncomment this code
/* std::cout << "I'am Naual" << std::endl;
std::cout << "I'am love cat" << '\n';
std::cout << "I'am from Bandung"; */
// Variable and basic data type
//uncomment
/*( basic data type(storing)
integer (whole number)
ex: int age = 21;
int year = 2023;
double (number include decimal)
ex: double temprature = 24.6;
char (single character)
ex: char grade = 'A';
char initial = 'AB' (it will cause error)
boolean (boolean value/true or false)
ex: bool power = true;
string (object that represent a sequence of text)
!! string are provided from standard name space to declare it:
use >> std::string name = "Naufal";
std::cout << name;
ex: std::string name = "Naufal";
)*/
/* int x; //this first is declaration
int y = 6;
int sum = x + y;
x = 5;
*/
/*
int x = 5;
int y = 7;
int sum = x + y;
std::cout << x << '\n';
std::cout << y << '\n';
std::cout << sum <<'\n';
*/
/* This below example of string standar out
std::string name = "Naufal";
std::cout << name;
*/
// Const keyword: specifies that variable's value is constant
// tells the compiler to prevent anything from modifiying it
// (read-only)
/*
ex: (inside main)
double PI = 3.14
const double PI = 3.14 (this will not change the value in const)
double gravity = 9.83 (but maybe in worst case someone change it, so the value will also change)
gravity = 10
std::cout << PI;
*/
//Namespace =
/* Provide a solution for preventing name conflicts
in large project. Each entity needs a unique name.
A namespace allows for identically name entities
as long as the namespace are diffrent.
*/
//Typedef and type aliases
/*
// Typedefs and type aliases
// typedef = reserved keyword used to create an additional name
// (alias) for another data type.
// New identifier for an existing type
// Helps with readability and reduces typos
// Use when there is a clear benefit
// Replaced with 'using' (work better w/ templates)
ex :
#include <iostream>
//typedef std::string text_t;
//typedef int number_t;
using text_t = std::string;
using number_t = int;
int main(){
text_t firstName = "Bro";
number_t age = 21;
std::cout << firstName << '\n';
std::cout << age << '\n';
return 0;
}
//Arithmetic operator
#include <iostream>
int main()
{
int students = 20;
// addition
// ------------------
//students = students + 1;
//students+=1;
//students++;
// subtraction
// ------------------
//students = students - 1;
//students-=1;
//students--;
// multiplication
// ------------------
//students = students * 2;
//students*=2;
// division
// ------------------
//students = students / 2;
//students/=2;
// modulus
// ------------------ (Good for find odd or even num)
//int remainder = students % 3;
//std::cout << remainder;
std::cout << students;
return 0;
}
//Type conversion
// type conversion = conversion a value of one data type to another
// Implicit = automatic
// Explicit = precede value with new data type (int)
ex:
#include<iostream>
int main() {
int correct = 8;
int questions = 10;
double score = correct/(double)questions * 100;
std::cout << score << "%";
return 0;
}
//User input
// cout << (insertion operator)
// cin >> (extraction operator)
ex:
#include <iostream>
int main()
{
std::string name;
int age;
std::cout << "What's your full name?: ";
std::getline(std::cin >> std::ws, name);
//Above the std::ws, because it were in position
above the question so in age there's no line instead
it's a new line.
std::cout << "What's your age?: ";
std::cin >> age;
std::cout << "Hello " << name << '\n';
std::cout << "You are " << age << " years old";
return 0;
//Useful math related function
ex:
#include <iostream>
#include <cmath>
int main()
{
double x = 3.99;
double y = 4;
double z;
//z = std::max(x, y);
//z = std::min(x, y);
//z = pow(2, 4);
//z = sqrt(9);
//z = abs(-3);
//z = round(x);
//z = ceil(x);
//z = floor(x);
std::cout << z;
return 0;
*/
//If statement
// if statement = do something if a condition is true.
// if not, then don't do it.
/* ex:
#include <iostream>
int main()
{
int age;
std::cout << "Enter your age: ";
std::cin >> age;
if(age >= 18){
std::cout << "Welcome to the site!";
}
else if(age < 0){
std::cout << "You haven't been born yet!";
}
else{
std::cout << "You are not old enough to enter!";
}
return 0;
}
//Switch
// switch = alternitive to using many "else if"
// statements, compare one value againdt matching cases.
ex:
#include <iostream>
int main()
{
char grade;
std::cout << "What letter grade?: ";
std::cin >> grade;
switch(grade){
case 'A':
std::cout << "You did great!";
break;
case 'B':
std::cout << "You did good";
break;
case 'C':
std::cout << "You did okay";
break;
case 'D':
std::cout << "You did not do good";
break;
case 'F':
std::cout << "YOU FAILED!";
break;
default:
std::cout << "Please only enter in a letter grade (A-F)";
}
return 0;
//Ternary Operator ?:
replacement to an if/else statement
condition ? expression1 : expression2;
ex:
#include <iostream>
int main()
{
//int grade = 50;
//grade >= 60 ? std::cout << "You pass!" : std::cout << "You fail!";
//int number = 9;
//number % 2 ? std::cout << "ODD" : std::cout << "EVEN";
//bool hungry = true;
//hungry ? std::cout << "You are hungry" : std::cout << "You are full";
return 0;
}
*/
//Logical operator
/* // && "and" = check if two conditions are true
|| "OR" = check if at least one conditions is true
! "Not" = reverses the logical state of operator
ex:
#include <iostream>
int main()
{
int temp;
bool sunny = false;
std::cout << "Enter the temperature: ";
std::cin >> temp;
if(temp > 0 && temp < 30){
std::cout << "The temperature is good!\n";
}
else{
std::cout << "The temperature is bad!\n";
}
if(temp <= 0 || temp >= 30){
std::cout << "The temperature is bad!\n";
}
else{
std::cout << "The temperature is good!\n";
}
if(!sunny){
std::cout << "It is cloudy outside!";
}
else{
std::cout << "It is sunny outside!";
}
return 0;
}
*/
//String methode
/*
length methode : it will give you how much character in some string
ex :
empty methode : check is string empty or not
return Boolean value
ex :
clear() : if you give an input it won't show the input you give
ex :
append : adding/appending string to another end of string
ex :
at(index) : give char at given position
ex :
insert(index, char) : insert char in string
find(char) : finding char in string, give position of char
erase(begin index, end index) : erasing char in string
You can read at string class in c++.com web
*/
// While loop
/*
ex :
#include <iostream>
int main()
{
std::string name;
while(name.empty()){
std::cout << "Enter your name: ";
std::getline(std::cin, name);
}
std::cout << "Hello " << name;
return 0;
}
*/
// Do while loop
/*
Do some block of code first,
THEN repeat again if condition is true
ex :
#include <iostream>
int main()
{
int number;
do{
std::cout << "Enter a positive #: ";
std::cin >> number;
}while(number < 0);
std::cout << "The # is: " << number;
return 0;
}
*/
// For loop
/*
ex:
#include <iostream>
int main()
{
for(int i = 10; i <20 ; i++){
//count to 20
std::cout << i << '\n';
}
std::cout << "HAPPY NEW YEAR!\n";
return 0;
*/
// Break & continue
/*
Break = break out of a loop
Continue = skiping current iteration
ex:
#include <iostream>
int main()
{
// break = break out of a loop
// continue = skip current iteration
for(int i = 1; i <= 20; i++){
if(i == 13){
//break;
//continue;
}
std::cout << i << '\n';
}
return 0;
*/
//Nested loop
/*
Loop inside the loop
Loop the Loop
*/
// Random
/*
pseudo-random = NOT truly random(but close)
srand(seed())
*/
//Function
return 0;
}