Before you start
Make sure you already, got VSCode set up to use C++; in addition keep in mind that this is a kind of a refresher for people who have some notion of programing or a basic understanding of C++, if you feel discouraged maybe take smaller steps to make your learning a bit less overwhelming.
Try to define an algorithm in your own words.
Before reading the answer try defining it yourself first. An algorithm is a set of organized steps or instructions that we or a computer needs to execute to solve a given problem or puzzle.
For example, a recipe could be an algorithm. Think about it, you follow a series of steps to achieve your goal, which, in this case, is to not starve or to make yourself some comfort food after a stressful test.
Big O notation
In future blogs I’ll try to mathematically derive the complexity associated with a certain algorithm. For now, all you need to know is what big notation is and understand what an efficient complexity looks like.
An oversimplification is to say that Big O notation refers to the relationship between the number of operations needed to conclude or execute an algorithm and the number of elements that an array has
Some examples of algorithms that don’t grow much in terms of execution time are Quick Sort or Merge Sort with a nlog(n) complexity. This means that if there’s a need to sort 100 numbers it would take 100 log(100) = 200 operations. An algorithm like Bubble sort has a complexity of n² which means that for the same 100 elements it would take 10,000 operations. Keep in mind that these complexities are not constant, meaning that on average depending on the initial configuration a Quick Sort algorithm will perform with a complexity of nlog(n).
Pointers
Pointers sometimes may sound intimidating at first, the trick is just to tread lightly, Let’s go through the concepts one by one.
A pointer is a variable that stores the address of another variable and it’s usually denoted in code as:
<type>* <name>
for example
int* pointer1;
int *pointer2;
int * pointer3;
sometimes you will see the asterisk * In different positions, some may argue it’s because that’s how they like it or they learned it that way (they just want to be seen as different from the crowd). But they all do the same thing so don’t worry too much about the position of the asterisk as long as it’s after the type and before the variable name.
Now let’s have a closer look at how a pointer saves a memory address of another variable. In the image below we can see a variable called var set to 5 and a pointer locked on it’s address by assigning the value as:
int* pointer = &var
Now we can see that the pointer’s value is the address of the variable var. Lets write it in a .cpp file.
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]){
int var = 5;
int *pointer = &var;
cout << "The variable var is equal to: " << var <<endl;
cout << "The address of variable var is: "<< pointer <<endl;
to execute it you can either use commands or install the code runner extension of VSCode
> g++ helloworld.cpp -o hello
> ./hello.exe
> The variable var is equal to: 5
> The address of variable var is: 0x30135ffbd4
if you’d like to access the contents of a variable using pointers
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]){
int var = 5;
int *pointer = &var;
cout << "The variable var is equal to: " << var <<endl;
cout << "The address of variable var is: "<< pointer <<endl;
cout << "The content of the address is: "<< *pointer;
References
Referencias
https://www.digitalocean.com/community/tutorials/js-big-o-notation