Skip to content

Instantly share code, notes, and snippets.

View gpetuhov's full-sized avatar
💭
Working on a project

Gennadiy Petuhov gpetuhov

💭
Working on a project
View GitHub Profile
@gpetuhov
gpetuhov / getmax.cpp
Last active March 17, 2017 08:58
C++ Get maximum of two numbers
#include <iostream>
using namespace std;
int getMax(int x, int y)
{
if (x > y)
return x;
else
return y;
}
@gpetuhov
gpetuhov / switch.cpp
Created March 17, 2017 08:50
C++ Switch example
int main() {
int x = 0;
cout << "Enter X" << endl;
cin >> x;
switch (x > 0) {
case true:
cout << "X > 0" << endl;
break;
case false:
cout << "X <= 0" << endl;
@gpetuhov
gpetuhov / forloop.cpp
Created March 17, 2017 08:55
C++ For loop example
int main() {
int NUM_ITERATIONS = 0; //number of iterations
int i;
int sum = 0;
cout << "Enter number of iterations" << endl;
cin >> NUM_ITERATIONS;
for (i = 0; i < NUM_ITERATIONS; i++) {
sum++;
}
cout << "SUM = " << sum << endl;
@gpetuhov
gpetuhov / recursion.cpp
Created March 17, 2017 08:59
C++ Recursion example
void recur(int n)
{
if (n > 0) {
cout << "1 ";
recur(n-1);
}
return;
}
int main() {
@gpetuhov
gpetuhov / getsymbol.cpp
Created March 17, 2017 09:03
C++ Get symbol from ASCII code
int code = 0;
cout << "Enter ASCII code" << endl;
cin >> code;
cout << "Symbol for this code is: " << (char) code << endl;
@gpetuhov
gpetuhov / calculations.cpp
Created March 17, 2017 09:08
C++ Calculations
int result=(0xFF & 5 >> 1 + 1);
cout << "Result = " << result << endl;
// Result is 1
int a = 5*3;
float b = 1.5f;
b += --a/2;
cout << "Result2 = " << b << endl;
// Result is 8.5
@gpetuhov
gpetuhov / class.cpp
Last active March 17, 2017 09:20
C++ Class example
#include <iostream>
using namespace std;
class Add
{ public: short S1;
int f(int x)
{ return S1 + ++x;}
int A(short a, short b);
} K1;
int Add::A(short a, short b)
@gpetuhov
gpetuhov / bubble_sort.cpp
Created March 17, 2017 09:25
C++ Bubble sort array
const int n = 10; //number of elements
int arr[n]; //array of n elements
int i,j; //array indexes
int temp; //temporary variable for sorting
srand(rand());
cout << "Unsorted array:" << endl;
for (i = 0; i < n; i++) {
arr[i] = rand();
cout << arr[i] << " ";
}
@gpetuhov
gpetuhov / struct.cpp
Last active March 17, 2017 09:45
C++ Struct example
struct my
{
int a, b;
} m1;
int func(my f)
{
return f.a + f.b++;
}
@gpetuhov
gpetuhov / pointers.cpp
Last active March 17, 2017 10:05
C++ Pointers
int x = 0;
int* ptr;
x = 10;
ptr = &x; // Save address of x into ptr
cout << "X = " << x << endl; // Result is 10
cout << "Y = " << *ptr << endl; // Result is 10
// ============
struct TwoNumbers {