Skip to content

Commit 2ac5068

Browse files
committed
streams
1 parent f2db605 commit 2ac5068

5 files changed

Lines changed: 284 additions & 5 deletions

File tree

intro/05-loops/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,11 @@ In this activity, you will have to solve Fibonacci sequence. You should implemen
274274

275275
Optional Readings on [Fibonacci Sequence](https://en.wikipedia.org/wiki/Fibonacci_number);
276276

277-
Hint: Create two variables, one to store the current value and the previous value. Each iteration step calculate the sum of both and store and put into a temp variable. Copy the current into the previous and set the current with the temporary you calculated before.
277+
Hint: Create two variables, one to store the current value and the previous value. For each iteration step, calculate the sum of both and store and put into a temp variable. Copy the current into the previous and set the current with the temporary you calculated before.
278+
279+
# Outcomes
280+
281+
It is expected for you to be able to solve all questions before this one `1151` 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.
282+
283+
# Troubleshooting
284+
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.

intro/06-functions/README.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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.

intro/07-streams/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Streams and File IO
2+
3+
At this point, you already are familiar with the `iostream` header. But we never discussed what it is properly. It is a basic stream and it has two static variable we already use: `cin` for reading variables from the console input and `cout` to output things to console, [see details here](https://en.cppreference.com/w/cpp/header/iostream). It is possible to interact with all streams via the `>>` and `<<` operators.
4+
5+
But C++ have 2 other relevant streams that we need to cover: [`fstream`](https://en.cppreference.com/w/cpp/header/fstream) and [`sstream`](https://en.cppreference.com/w/cpp/header/sstream).
6+
7+
# String Stream
8+
9+
The [`sstream` header](https://en.cppreference.com/w/cpp/header/sstream) describes string stream, which is a type of memory stream and is very useful to do string manipulation. For our intent, we aro going to focus 3 types of memory streams.
10+
11+
- `ostringstream`: works just like `cout` but the content will printed to a memory region.
12+
- it is more efficient to build a complex string in this way than `cout`ing multiple times;
13+
- `istringstream`: works just like `cin` but it will read from a memory area.
14+
- it is safer to read from a closed memory area than, and you ran reset the reading pointer to re-read previous elements easier than with `cin`.
15+
16+
```c++
17+
#include <iostream>
18+
#include <sstream>
19+
using namespace std;
20+
int main() {
21+
ostringstream oss; // declare the output stream
22+
// print numbers from 0 to 100
23+
for(int i=0; i<=100; i++)
24+
oss << i << ' '; // store the data into memory
25+
cout << oss.str(); // convert the stream into a string to be printed all at once
26+
}
27+
```
28+
29+
```c++
30+
#include <iostream>
31+
#include <sstream>
32+
using namespace std;
33+
int main() {
34+
// read input
35+
string input;
36+
getline(cin, input);
37+
38+
// initialize string stream with the content from a console line
39+
istringstream ss(input); // declare the stream to read from
40+
41+
// extract input
42+
string name;
43+
string course;
44+
string grade;
45+
46+
iss >> name >> course >> grade;
47+
}
48+
```
49+
50+
# File streams
51+
52+
File streams are streams

intro/08-arrays/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Arrays
2+
3+
https://en.cppreference.com/w/cpp/language/array

intro/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
| 1 | 2023/01/16 | [Introduction](01-introduction/README.md) |
88
| 2 | 2023/01/23 | [Tooling](02-tooling/README.md) |
99
| 3 | 2023/01/30 | [Data Types, Arithmetic Operations, Type conversion](03-datatypes/README.md) |
10-
| 4 | 2023/02/06 | [Conditionals, Boolean and Bitwise Operations](04-conditionals) |
11-
| 5 | 2023/02/13 | [Loops, for, while, goto](05-loops) |
12-
| 6 | 2023/02/20 | Functions, Base Conversion, Pointers, Reference |
13-
| 7 | 2023/02/27 | File IO and review |
10+
| 4 | 2023/02/06 | [Conditionals, Boolean and Bitwise Operations](04-conditionals//README.md) |
11+
| 5 | 2023/02/13 | [Loops, for, while, goto](05-loops//README.md) |
12+
| 6 | 2023/02/20 | [Functions, Base Conversion, Pointers, Reference](06-functions/README.md) |
13+
| 7 | 2023/02/27 | [Streams, File IO] |
1414
| 8 | 2023/03/06 | Midterm |
1515
| 9 | 2023/03/13 | BREAK |
1616
| 10 | 2023/03/20 | Arrays, Vectors, String |

0 commit comments

Comments
 (0)