|
| 1 | +# Base Conversion, Functions, Pointers, Parameter Passing |
| 2 | + |
| 3 | +# Base conversion |
| 4 | + |
| 5 | +Data containers use binary coding to store data where every digit can be 0 or 1, this is called base 2, but there are different types of binary encodings and representation, the most common integer representation is [Complement of two](https://en.wikipedia.org/wiki/Two%27s_complement) for representing positive and negative numbers and for floats is [IEEE754](https://en.wikipedia.org/wiki/IEEE_754). Given that, it is relevant to learn how to convert the most used common bases in computer science in order to code more efficiently. |
| 6 | + |
| 7 | +Most common bases are: |
| 8 | +- Base 2 - Binary. Digits can go from 0 to 1. `{0, 1}`; |
| 9 | +- Base 8 - Octal. Digits can go from 0 to 7. `{0, 1, 2, 3, 4, 5, 6, 7}`; |
| 10 | +- Base 10 - Decimal. Digits can go from 0 to 9. `{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}`; |
| 11 | +- Base 16 - Hexadecimal. Digits can go from 0 to 9 and then from A to F. `{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}`; |
| 12 | + |
| 13 | +## Converting from Decimal to any base |
| 14 | + |
| 15 | +There are several methods for performing base conversion, but one common method is to use the repeated division and remainder method. To convert a number from base 10 to another base `b`, you can divide the number by `b` and record the remainder. Repeat this process with the quotient obtained from the previous division until the quotient becomes zero. The remainders obtained during the process will be the digits of the result in the new base, with the last remainder being the least significant digit. |
| 16 | + |
| 17 | +For example, to convert the decimal number 75 to base 2 (binary), we can follow these steps: |
| 18 | + |
| 19 | +``` |
| 20 | +75 ÷ 2 = 37 remainder 1 |
| 21 | +37 ÷ 2 = 18 remainder 1 |
| 22 | +18 ÷ 2 = 9 remainder 0 |
| 23 | +9 ÷ 2 = 4 remainder 1 |
| 24 | +4 ÷ 2 = 2 remainder 0 |
| 25 | +2 ÷ 2 = 1 remainder 0 |
| 26 | +1 ÷ 2 = 0 remainder 1 |
| 27 | +``` |
| 28 | + |
| 29 | +The remainders obtained during the process (1, 1, 0, 1, 0, 0, 1) are the digits of the result in base 2, with the last remainder (1) being the least significant digit. Therefore, the number 75 in base 10 is equal to 1001011 in base 2. |
| 30 | + |
| 31 | +## Converting from any base to decimal |
| 32 | + |
| 33 | +The most common way to convert from any base to decimal is to follow the formula: |
| 34 | + |
| 35 | +> d<sub>n-1</sub>*b<sup>n-1</sup> + d<sub>n-2</sub>*b<sup>n-2</sup> + ... + d<sub>1</sub>*b<sup>1</sup> + d<sub>0</sub>*b<sup>0</sup> |
| 36 | +
|
| 37 | +Where d<sub>x</sub> represents the digit at the corresponding position x in the number, n is the number of digits in the number, and b is the base of the number. |
| 38 | + |
| 39 | +For example, to convert the number 1001011 (base 2) to base 10, we can use the following formula: |
| 40 | + |
| 41 | +(1 * 2^6) + (0 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 75 |
| 42 | + |
| 43 | +Therefore, the number 1001011 in base 2 is equal to 75 in base 10. |
| 44 | + |
| 45 | +# Functions |
| 46 | + |
| 47 | +A function is a block of code that performs a specific task. It is mostly used to isolate specific reusable functionality from the rest of the code. It has a name, a return type, and a list of parameters. Functions can be called from other parts of the program to execute the task. Here is an example of a simple C++ function that takes two integers as input and returns their sum. |
| 48 | + |
| 49 | +```c++ |
| 50 | +int add(int x, int y) { |
| 51 | + int sum = x + y; |
| 52 | + return sum; |
| 53 | +} |
| 54 | +``` |
| 55 | +
|
| 56 | +To call the function, you would use its name followed by the arguments in parentheses: |
| 57 | +
|
| 58 | +```c++ |
| 59 | +int a = 2, b = 3; |
| 60 | +int c = add(a, b); // c will be equal to 5 |
| 61 | +``` |
| 62 | + |
| 63 | +Functions can also be declared before they are defined, in which case they are called "prototypes." This allows you to use the function before it is defined, which can be useful if you want to define the function after it is used. For example: |
| 64 | + |
| 65 | +```c++ |
| 66 | +int add(int x, int y); |
| 67 | + |
| 68 | +int main() { |
| 69 | + int a = 2, b = 3; |
| 70 | + int c = add(a, b); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int add(int x, int y) { |
| 75 | + int sum = x + y; |
| 76 | + return sum; |
| 77 | +} |
| 78 | +``` |
| 79 | +
|
| 80 | +# Reference Declaration |
| 81 | +
|
| 82 | +NOTE: this only covers an introduction to the topic. |
| 83 | +
|
| 84 | +The `&` is used to refer memory address of the variable. When used in the declaration, it is the Lvalue reference declarator. It is an alias to an already-existing, variable, object or function. [Read more here](https://en.cppreference.com/w/cpp/language/declarations). |
| 85 | +
|
| 86 | +When used as an prefix operator before the name of a variable, it will return the memory address where the variable is allocated. |
| 87 | +
|
| 88 | +Example: |
| 89 | +```c++ |
| 90 | +string s; |
| 91 | +
|
| 92 | +// the variable r has the same memory address of s |
| 93 | +// the declaration requires initialization |
| 94 | +string& r = s; |
| 95 | +
|
| 96 | +s = "Hello"; |
| 97 | +
|
| 98 | +cout << &s << endl; // prints the variable memory address location. in my machine: "0x7ffc53631cd0" |
| 99 | +cout << &r << endl; // prints the same variable memory address location. in my machine: "0x7ffc53631cd0" |
| 100 | +
|
| 101 | +cout << s << endl; // prints "Hello" |
| 102 | +cout << r << endl; // prints "Hello" |
| 103 | +
|
| 104 | +// update the content |
| 105 | +r += " world!"; |
| 106 | +
|
| 107 | +cout << s << endl; // prints "Hello world!" |
| 108 | +cout << r << endl; // prints "Hello world!" |
| 109 | +``` |
| 110 | + |
| 111 | +# Pointer Declaration |
| 112 | + |
| 113 | +NOTE: this only covers an introduction to the topic. |
| 114 | + |
| 115 | +The `*` is used to declare a variable that holds the address of a memory position. A pointer is an integer number that points to a memory location of a container of a given type. [Read more here](https://en.cppreference.com/w/cpp/language/pointer). |
| 116 | + |
| 117 | +```c++ |
| 118 | +string* r = nullptr; // it is not required do initialize, but it is a good practice to always initialize a pointer pointing to null address (0). |
| 119 | +string s = "Hello"; |
| 120 | +r = &s; // the variable r stores the memory address of s |
| 121 | + |
| 122 | +cout << s << endl; // prints the content of the variable s. "Hello" |
| 123 | +cout << &s << endl; // prints the address of the variable s. in my machine "0x7fffdda021b0" |
| 124 | + |
| 125 | +cout << r << endl; // prints the numeric value of the address the pointer points, in this case it is "0x7fffdda021b0". |
| 126 | +cout << &r << endl; // prints the address of the variable r. it is a different address than s, in my machine "0x7fffdda021d0". |
| 127 | +cout << *r << endl; // prints the content of the container that is pointing, it prints "Hello". |
| 128 | + |
| 129 | +string other = "world"; |
| 130 | +r = &s; // r now points to another variable |
| 131 | + |
| 132 | +cout << *r << endl; // prints the content of the container that is pointing, it prints "world" |
| 133 | +``` |
| 134 | + |
| 135 | +# Passing parameter to a function by value |
| 136 | + |
| 137 | +Pass-by-value is when the parameter declaration follows the traditional variable declaration without `&`. A copy of the value is made and passed to the function. Any changes made to the parameter inside the function have don't change on the original value outside the function. |
| 138 | + |
| 139 | +```c++ |
| 140 | +#include <iostream> |
| 141 | +using namespace std; |
| 142 | +void times2(int x) { |
| 143 | + x = x * 2; |
| 144 | + // the value x here is doubled. but it dont change the value outside the scope |
| 145 | +} |
| 146 | + |
| 147 | +int main() |
| 148 | +{ |
| 149 | + int y = 2; |
| 150 | + times2(y); // this dont change the value, it passes a copy to the function |
| 151 | + cout << y << endl; // output: 2 |
| 152 | + return 0; |
| 153 | +} |
| 154 | +``` |
| 155 | +
|
| 156 | +# Passing parameter to a function by reference |
| 157 | +
|
| 158 | +Pass-by-reference occurs when the function parameter uses the `&` in the parameter declaration. It will allow the function to modify the value of the parameter directly in the other scope, rather than making a copy of the value as it does with pass-by-value. |
| 159 | +
|
| 160 | +```c++ |
| 161 | +#include <iostream> |
| 162 | +using namespace std; |
| 163 | +void times2(int &x) { // by using &, x has the same address the variable passed where the function is called |
| 164 | + x*=2; // it will change the variable in caller scope |
| 165 | +} |
| 166 | +
|
| 167 | +int main() { |
| 168 | + int y = 2; |
| 169 | + times2(y); |
| 170 | + cout << y << endl; // Outputs 4 |
| 171 | + return 0; |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +# Scopes |
| 176 | + |
| 177 | +Scope is a region of the code where a identifier is accessible. A scope usually is specified by what is inside `{` and `}`. The global scope is the one that do not is inside any `{}`. |
| 178 | + |
| 179 | +```c++ |
| 180 | +#include <iostream> |
| 181 | +#include <string> |
| 182 | +using namespace std; |
| 183 | +string h = "Hello"; // this variable is in the global scope |
| 184 | +int main() { |
| 185 | + string w = " world"; // this variable belongs to the scope of the main function |
| 186 | + cout << h << w << endl; // both variables are visible and accessible |
| 187 | + return 0; |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +Multiple identifiers with same name can not be created in the same scope. But in a nested scope it is possible to shadow the outer one when declared in the inner scope. |
| 192 | + |
| 193 | +```c++ |
| 194 | +#include <iostream> |
| 195 | +#include <string> |
| 196 | +using namespace std; |
| 197 | +string h = "Hello"; // this variable is in the global scope |
| 198 | +int main() { |
| 199 | + cout << h; // will print "Hello" |
| 200 | + string h = " world"; // this will shadow the global variable with the same name h |
| 201 | + cout << h; // will print " world" |
| 202 | + return 0; |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +# Homework |
| 207 | + |
| 208 | +Do all exercises up to this topic here https://www.w3schools.com/cpp/exercise.asp |
| 209 | + |
| 210 | +- [Hexadecimal converter](https://www.beecrowd.com.br/judge/en/problems/view/1957). In this activity, you will have to code a way to find the convert to hexadecimal. |
| 211 | + |
| 212 | +# Outcomes |
| 213 | + |
| 214 | +It is expected for you to be able to solve all questions before this one `1957` on beecrowd. Sort Beecrowd questions from the most solved to the least solved questions [here](https://www.beecrowd.com.br/judge/en/search?q=&sort=Problems.solved&direction=desc) in the link. If you don't, see [Troubleshooting](#troubleshooting). Don`t let your study pile up, this homework is just a small test, it is expected from you to do other questions on Beecrowd or any other tool such as leetcode. |
| 215 | + |
| 216 | +# Troubleshooting |
| 217 | +If you have problems here, start a [discussion](https://github.com/InfiniBrains/Introduction-to-Game-Programming-With-CPP/discussions). Nhis is publicly visible and not FERPA compliant. Use discussions in Canvas if you are enrolled in a class with me. Or visit the tutoring service. |
0 commit comments