parent
4e5d24464c
commit
907c3cad09
@ -1,6 +1,6 @@
|
|||||||
// In C++11 you can do the following, but most people use vector instead of arrays
|
// In C++11 you can do the following, but most people use vector instead of arrays
|
||||||
a.size(); // Returns the size of array a
|
a.size(); // Returns the size of array a
|
||||||
|
|
||||||
int a[10]; // Declare an int array with length 10.
|
int a[10]; // Declare an int array with length 10.
|
||||||
a[0] = 420; // Set the value 420 to index 0 of array a
|
a[0] = 420; // Set the value 420 to index 0 of array a
|
||||||
int b = a[0] // Set variable b to 420 by accessing index 0 of array a.
|
int b = a[0] // Set variable b to 420 by accessing index 0 of array a.
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
int main() {
|
int main() {
|
||||||
// Basic
|
// Basic
|
||||||
if(x > 0) {
|
if(x > 0) {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return -x;
|
return -x;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if else
|
// if else
|
||||||
if(x > 0) {
|
if(x > 0) {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
else if(x == 0) {
|
else if(x == 0) {
|
||||||
return 420;
|
return 420;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return -x;
|
return -x;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
// There are only 'for', 'while', and 'do while'
|
// There are only 'for', 'while', and 'do while'
|
||||||
|
|
||||||
// For loop - Outputs "0 1 2 3 4 5 6 7 8 9"
|
// For loop - Outputs "0 1 2 3 4 5 6 7 8 9"
|
||||||
for( int i=0; i<10; i++ ) {
|
for( int i=0; i<10; i++ ) {
|
||||||
std::cout << i << " ";
|
std::cout << i << " ";
|
||||||
}
|
}
|
||||||
|
|
||||||
// While loop - Outputs "0 1 2 3 4 5 6 7 8 9"
|
// While loop - Outputs "0 1 2 3 4 5 6 7 8 9"
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while( i < 10 ) {
|
while( i < 10 ) {
|
||||||
std::cout << i << " ";
|
std::cout << i << " ";
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// While loop - Outputs "0 "
|
// While loop - Outputs "0 "
|
||||||
int i = 0;
|
int i = 0;
|
||||||
do {
|
do {
|
||||||
std::cout << i << " ";
|
std::cout << i << " ";
|
||||||
}while( i<0 );
|
}while( i<0 );
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
// ## Built-in Types
|
// ## Built-in Types
|
||||||
bool
|
bool
|
||||||
|
|
||||||
char
|
char
|
||||||
|
|
||||||
string
|
string
|
||||||
|
|
||||||
// Assume signed, prepend unsigned to make it unsigned
|
// Assume signed, prepend unsigned to make it unsigned
|
||||||
short
|
short
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
||||||
long
|
long
|
||||||
|
|
||||||
// C++ 11
|
// C++ 11
|
||||||
long long
|
long long
|
||||||
|
|
||||||
// Floating Points
|
// Floating Points
|
||||||
float
|
float
|
||||||
double
|
double
|
||||||
long double
|
long double
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
// Vectors are sequence containers representing arrays that can change in size.
|
// Vectors are sequence containers representing arrays that can change in size.
|
||||||
|
|
||||||
// Library to include
|
// Library to include
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
vector<int> a; // Declare an empty vector, a.
|
vector<int> a; // Declare an empty vector, a.
|
||||||
a.push_back(1); // Appends/Adds an element whose value is 1 to vector a.
|
a.push_back(1); // Appends/Adds an element whose value is 1 to vector a.
|
||||||
std::cout << a.at(0) << std::endl; // Accessing index 0 of vector a.
|
std::cout << a.at(0) << std::endl; // Accessing index 0 of vector a.
|
||||||
a.size(); // Returns the size of the vector
|
a.size(); // Returns the size of the vector
|
||||||
a.at(0) = 420; // Changing the value at index 0 to 420.
|
a.at(0) = 420; // Changing the value at index 0 to 420.
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// ## Packages
|
// ## Packages
|
||||||
// * Package declaration at top of every source file
|
// * Package declaration at top of every source file
|
||||||
// * Executables are in package `main`
|
// * Executables are in package `main`
|
||||||
// * Convention: package name == last name of import path (import path `math/rand` => package `rand`)
|
// * Convention: package name == last name of import path (import path `math/rand` => package `rand`)
|
||||||
// * Upper case identifier: exported (visible from other packages)
|
// * Upper case identifier: exported (visible from other packages)
|
||||||
// * Lower case identifier: private (not visible from other packages)
|
// * Lower case identifier: private (not visible from other packages)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
-- to install:
|
-- to install:
|
||||||
-- (Debian/Ubuntu) apt-get install lua
|
-- (Debian/Ubuntu) apt-get install lua
|
||||||
-- (Fedora/CentOS) yum install lua
|
-- (Fedora/CentOS) yum install lua
|
||||||
-- (source code) curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz | tar xz -
|
-- (source code) curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz | tar xz -
|
||||||
-- cd lua-5.3.4
|
-- cd lua-5.3.4
|
||||||
-- make linux
|
-- make linux
|
||||||
--
|
--
|
||||||
-- to execute:
|
-- to execute:
|
||||||
-- lua hello.lua
|
-- lua hello.lua
|
||||||
|
|
||||||
io.write("Hello world, from ",_VERSION,"!\n")
|
io.write("Hello world, from ",_VERSION,"!\n")
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
# For what Recursion is, please check theory/recursion
|
# For what Recursion is, please check theory/recursion
|
||||||
|
|
||||||
# Simple Factorial Python Recursion
|
# Simple Factorial Python Recursion
|
||||||
def factorial(n) :
|
def factorial(n) :
|
||||||
if n == 0 :
|
if n == 0 :
|
||||||
return 1
|
return 1
|
||||||
else :
|
else :
|
||||||
return n * factorial(n-1)
|
return n * factorial(n-1)
|
||||||
|
|
||||||
# Simple Greatest Common Divisor Recursion
|
# Simple Greatest Common Divisor Recursion
|
||||||
def gcd(x, y) :
|
def gcd(x, y) :
|
||||||
if y == 0 :
|
if y == 0 :
|
||||||
return x
|
return x
|
||||||
else :
|
else :
|
||||||
return gcd(y, x%y)
|
return gcd(y, x%y)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
# Recursion
|
# Recursion
|
||||||
# Def: "...is a method where the solution to a problem depends on solutions to smaller instance of the same problem.." - wiki
|
# Def: "...is a method where the solution to a problem depends on solutions to smaller instance of the same problem.." - wiki
|
||||||
# TL;DR: a function that calls itself inside its body.
|
# TL;DR: a function that calls itself inside its body.
|
||||||
|
|
||||||
# Recursive programs - Pseduocode
|
# Recursive programs - Pseduocode
|
||||||
function factorial:
|
function factorial:
|
||||||
input: integer n such that n >= 0
|
input: integer n such that n >= 0
|
||||||
output: n * (n-1) * (n-2) * ... * 1 = n!
|
output: n * (n-1) * (n-2) * ... * 1 = n!
|
||||||
|
|
||||||
1. if n is 0, return 1
|
1. if n is 0, return 1
|
||||||
2. else, return ( n * factorial(n-1) )
|
2. else, return ( n * factorial(n-1) )
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
# C++
|
# C++
|
||||||
#
|
#
|
||||||
# C++ is an object-oriented programming language which provides facilities for low-level memory manipulation.
|
# C++ is an object-oriented programming language which provides facilities for low-level memory manipulation.
|
||||||
# It is widely used by big tech companies, such as, Amazon, Facebook, Google, and SpaceX
|
# It is widely used by big tech companies, such as, Amazon, Facebook, Google, and SpaceX
|
||||||
#
|
#
|
||||||
# To Compile: g++ my_script.cpp
|
# To Compile: g++ my_script.cpp
|
||||||
# To Execute: ./a.out
|
# To Execute: ./a.out
|
||||||
#
|
#
|
||||||
# See also
|
# See also
|
||||||
# C++ language cheat sheet at /c++/
|
# C++ language cheat sheet at /c++/
|
||||||
# list of pages: /c++/:list
|
# list of pages: /c++/:list
|
||||||
# learn C++: /c++/:learn
|
# learn C++: /c++/:learn
|
||||||
# C++ one-liners: /c++/:1line
|
# C++ one-liners: /c++/:1line
|
||||||
# C++ weirdness: /c++/weirdness
|
# C++ weirdness: /c++/weirdness
|
||||||
# search in pages: /c++/~keyword
|
# search in pages: /c++/~keyword
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
# ranlib
|
# ranlib
|
||||||
#
|
#
|
||||||
# generates an index to the contents of an archive and stores it in the archive.
|
# generates an index to the contents of an archive and stores it in the archive.
|
||||||
# The index lists each symbol defined by a member of an archive that is a relocatable object file.
|
# The index lists each symbol defined by a member of an archive that is a relocatable object file.
|
||||||
|
|
||||||
# This creates an index of the contents of fruits.a and stores the index in fruits.a
|
# This creates an index of the contents of fruits.a and stores the index in fruits.a
|
||||||
# This is useful for linking and in case the objects call each other.
|
# This is useful for linking and in case the objects call each other.
|
||||||
ranlib fruits.a
|
ranlib fruits.a
|
||||||
# after
|
# after
|
||||||
# ar r fruits.a apple.o orange.o pineapple.o
|
# ar r fruits.a apple.o orange.o pineapple.o
|
||||||
# is called
|
# is called
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue