Computer science basics part one
Variebles and data types
int main() {
int simple_number = 5;
char simpleCharacter = 'A';
double simpleFloat = 1.337;
bool simpleBool = true;
void lol;
return 0;
}
References & Pointers
Pointers
int main () {
int some_number = 0;
int* some_number_adress = &some_number;
cout << some_number_adress << endl;
return 0;
}
References
int main () {
int some_number = 0;
int& some_value_in_memory = some_number;
int* some_number_adress = &some_number;
cout << some_number_adress << " : " << some_value_in_memory << endl;
return 0;
}
Conditions
'if else' statement
int main () {
int some_number = 0;
cin >> some_number;
if (some_number != 0) {
cout << "You changed number!" << endl;
} else {
cout << "Number is still zero!" << endl;
}
return 0;
}
'switch case' statement
int main () {
int dick_size = 0;
cin >> dick_size;
if (dick_size <= 0) {
cout << "Dick size cant be below or equal to zero" << endl;
}
else {
switch (dick_size) {
case 5: cout << "Korean Giant" << endl;
break;
case 10: cout << "Angry Russian" << endl;
break;
case 15: cout << "Nice Guy" << endl;
break;
case 25: cout << "Happy chicken lover(tolerance)" << endl;
break;
default: cout << "I cant understand that size of dick";
}
}
return 0;
}
? operator statement
int main () {
bool are_you_gay;
cin >> are_you_gay;
cout << (are_you_gay ? "Welcome to blue boat" : "Get the fuck out of here") << endl;
return 0
}
Logical operators
List of operators
- || - or
- && - and
- ! - not
int main () {
bool some_true_value = true;
bool some_false_value = false;
cout << "some_true_value || some_false_value is " << (some_true_value || some_false_value ? "true" : "false") << endl;
cout << "some_false_value || some_true_value is " << (some_false_value || some_true_value ? "true" : "false") << endl;
cout << "some_false_value && some_true_value is " << (some_false_value && some_true_value ? "true" : "false") << endl;
cout << "!some_false_value is " << (!some_false_value ? "true" : "false") << endl;
cout << "!some_true_value is " << (!some_true_value ? "true" : "false") << endl;
return 0;
}
Loops
for loop statememt
int main () {
int students_count;
cin >> students_count;
for (int i = 0; i <= students_count; i++) {
cout << i << " studnets sleep on lection" << endl;
}
return 0;
}
while loop statement
int main () {
bool should_loop_end = true;
while (should_loop_end) {
cout << "Do you want loop to continue?";
cin >> should_loop_end;
}
return 0;
}
do while loop statement
int main () {
bool should_loop_end = true;
cin >> should_loop_end;
do {
cout << "Do you want loop to continue?";
cin >> should_loop_end;
} while (should_loop_end);
return 0;
}
Arrays
int main () {
int* some_array;
int length = 0;
bool is_dynamic;
int array_element_sum = 0;
cout << "Input 1 if you want dynamic array or 0 if you want static one" << endl;
cin >> is_dynamic;
if (is_dynamic) {
cout << "Input length of your array" << endl;
cin >> length;
some_array = new int[length];
} else {
some_array = new int [10];
}
for (int i = 0; i < (length ? length : 10); i++) {
cout << "input " << i << " element of array" << endl;
cin >> some_array[i];
}
for (int i = 0; i < (length ? length : 10); i++) {
array_element_sum += some_array[i];
}
cout << "array_element_sum of all array numbers is: " << array_element_sum << endl;
delete[] some_array;
return 0;
}
Multi-dimension Arrays
int main () {
int length = 10;
int** matrix = new int* [length];
for (int i = 0; i < length; i++) {
matrix[i] = new int[length];
}
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
matrix[i][j] = sqrt(pow((i - j), 2));
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < length; i++) {
delete[] matrix[i];
}
delete[] matrix;
return 0;
}
Functions and Recursion
Functions
void output_message (char* message, int value = 0) {
cout << message << value << endl;
}
void double_number_by_ref (int& number) {
number = number * 2;
}
int double_number (int number) {
return number * 2;
}
int main () {
int some_number = 5;
int& ref_to_some_number = some_number;
int some_numbers[] = [1, 2, 3, 4, 5];
output_message("we have some int varieble with value ", some_number);
int doubled_number = double_number(some_number);
output_message("now we created new varieble, and its value: ", doubled_number);
output_message("value of old varrieble is: ", some_number);
double_number_by_ref(ref_to_some_number);
output_message("some_number now is: ", some_number);
return 0;
}
Recursive functions
int count_factorial(int number) {
for (int i = (number - 1); i > 1; i--) {
number = number * i;
}
return number;
}
int count_factorial_recursive(int number) {
if (number > 1) {
number = number * count_factorial_recursive(number -1);
}
return number;
}
int main () {
int number = 1;
cin >> number;
cout << "recursive function result: " << count_factorial_recursive(number) << endl;
cout << "interable function result: " << count_factorial(number) << endl;
return 0;
}
Computer science basics part one
By Gleb Zhidkov
Computer science basics part one
- 120