📖

written by our dear FR family members. all the bullshit are from - Issac Ko, Aidan Wong, Horace Wong and Brian Peng.

## Chapter 1 - Basic Stuff

If you know that, our computer basicallly separated into 3 steps in order to complete a task. There are input, process and output.

### Output

And basically your first code in all programming language, is to know how to output “Hello World”.

So here’s the code in C++:

#include <iostream>
using namespace std;

int main() {
cout << "Hello World" << endl;
}

Explain:

#include iostream is a part of the C++ standard library that allows us to read and write text from / to the console.

using namespace std; means we use the namespace named std.

int main() tells the compiler that we’re going to define a function called ‘main’.

cout << “Hello World” cout, which stands for ‘output’ and the << operator allows us to send stuff to the console to be output.

“Hello World”the ‘‘ ’’ lets the computer know hello world is a text.

endl means output a new line to the result.

### Input without space

The second part of programming is input something, without space.

So here’s the code in C++:

#include <iostream>
using namespace std;

int main() {
string n;
cin >> n;
cout << "Hi, " << n << endl;
}

Explain:

string is a container. Container can be any type of stuff. Examples below:

Result:

However, if we input something with space:

It will still output “Issac” only but not “Ko”.

Therefore, there’s a special function called getline() can help us to do this.

### Input with space

Here’s the code:

#include <iostream>
using namespace std;

int main() {
string n;
getline(cin, n);
cout << "Hi, " << n << endl;
}

Result:

So now, you know how to input and output, time to learn how to do the process!

### Maths in any programming language

Usually after you have completed your first code which is output “Hello World”, the next assignment is to make a calculator.

Here’s the Maths symbols used basically in all programming language:

Example code for a very simple calculator:

#include <iostream>
using namespace std;

int main() {
int a, b;
cin >> a >> b;
cout << a + b;
}

And here’s the example of modulus %

#include <iostream>
using namespace std;

int main() {
int a;
a = 10;
cout << a % 5 << endl;
}

Result:

It is 0 because 10 can be divided by 5.

### Homework

wy_2xxxx

1. Navigate to the following URL

School Hosted: W101, W102, W103, W104 (Those are like very easy...)

## Chapter 2 - More Basic Stuff

### Conditions

The usual logical conditions from mathematics.

 Less than a < b Less than or equal to a <= b Greater than a > b Greater than or equal to a >= b Equal to a == b Not Equal to a != b

### If Clause

In most of the tasks that you will do in the future, it is a must that you need to use ‘If clause’.

• Use if to specify a block of code to be executed, if a specified condition is true
• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false

If Statement

#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;
if (x > 1) {
cout << x << " is greater than 1.";
}
}

Explain:

if is the function that specifies a block of code to be executed, if a specified condition is true.

2 > 1 is what you need to tell the program when will the following code run, or simply ‘condition’.

cout << "2 is greater than 1" is the output function.

Else Statement

#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;
if (x > 2) {
cout << x << "is greater than 2.";
} else {
cout << x << "isn't greater than 2.";
}

Else If Statement

#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;
if (x > 2) {
cout << x << "is greater than 2.";
} else if (x = 1) {
cout << x << "is equal to 1.";
}

So, please look at this very simple Maths question, and try to grab a pen and solve it.

And I am pretty sure that you know the solution, right?

$x = 13 * cos(60)$﻿

However, in the world of C++, we cannot directly type this equation in our code.

But here’s the solution to this problem.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
int side, angle, x;
side = 13;
angle = 60;

x = 13 * cos(60 * 3.14159 / 180);
cout << x << endl;

return 0;
}

Explain:

#include <cmath> allows us to use Maths function such as $sin / cos / tan$﻿.

cos(60 * 3.14159 / 180) noted that all $sin/cos/tan$﻿ equations MUST be written in this format $( * 3.14159 / 180)$﻿, since it must be expressed as radians.

### Set Precision

Here’s the code of how to do this:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
double x;
x = 3.1415926;

cout << fixed << setprecision(3);
cout << x << endl;

return 0;
}

Result:

Explain:

#include <iomanip> allows us to use parametric manipulators such as set precision.

cout << fixed << setprecision(3) means we have to fixed the value, and set precision to 3 decimal numbers.

### Operators

This is quite important on most of the tasks, especially when we are using If clause.

Here’s some of the examples of some operators:

 || OR && AND != Not Equal
#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;

if (x % 10 = 0 || x % 2 = 0) {
cout << "True" << endl;
} else if (x % 3 = 0 && x % 4 = 0) {
cout << "False" << endl;
} else if (x != 0) {
cout << "Not equal to 0" << endl;
}

return 0;
}

Explain:

The first statement is || means OR, so the input value just needs to satisfy 1 of the statements (e.g. 10).

The second statement is && means AND, so the input value needs to satisfy both of the statements (e.g. 12).

### Homework

School Hosted: W111, W112, W113, W114

## Chapter 3 - Logic

Warning - This part is EXTREMELY difficult, especially for someone like me who got no interests in Maths.

You can feel free to skip this part if you have no interests of going to the HKOI competition.

But if you choose to stay, then be careful that your brain will explode.

### Logic Symbols

Those all the very basic logic symbols that you need to remember for the HKOI competition:

 In English In C++ p AND q p && q p OR q p || q NOT p !p p XOR q p ^ q p NAND q !(p && q) p NOR q !(p || q) p XNOR q p == q

Don’t understand at all? Pretty normal lol.

Explain:

Logic symbols is used to know the operator’s function and you should know all of the followings:

 p q p || q 0 0 0 0 1 1 1 0 1 1 1 1
 p q p && q 0 0 0 0 1 0 1 0 0 1 1 1
 p q p ^ q 0 0 0 0 1 1 1 0 1 1 1 0
 p q p == q 0 0 1 0 1 0 1 0 0 1 1 1

Examples:

If the question ask you to output the result of 8 XOR 13:

1. You need to write down their numbers in binary.
1. So the answer is 5.

But if you still do not understand, really recommend you to re-read the above section.

### Homework

Actually no School Hosted tasks requires you to understand about the logic symbols, but it is still important to at least know about it.

However, there is still 1 HKOI tasks that you can do which is D112.

## Chapter 4 - Files

Actually to be honest, not many tasks requires you to learn how to input and output from files, but this is quite easy so maybe you should learn this before For Loops.

Here’s an example:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream infile;
infile.open("score.in");

int a;
infile >> a;
infile.close();

ofstream outfile;
outfile.open("score.out");
outfile << 0.5 * a << endl;
outfile.close();
}

Explain:

#include <fstream> allows you to use the file functions.

ifstream infile means you create a file for input.

infile.open(”score.in”) means you open the input file named as score.in.

infile << a replace the cin to infile.

ofstream outfile means you create a file for output.

outfile.open("score.out") means you open the output file named as score.out.

outfile << 0.5 * a << endl replace the cout to outfile.

## Chapter 5 - Loops

Ok, here’s come the most useful and annoying part in your coding history.

Loop is also 1 of the concepts that I am still suffering with...

### While Loops

This is the easy version of loops. But you won’t use it because there’s an advanced one called For Loops which will be covered in the next section.

#include <iostream>
using namespace std;

int main () {
int n;
cin >> n;

while (n <= 10) {
cout << "You are stupid!" << endl;
n++;
}
}

Result:

Explain:

while (n <= 10) means the condition of the loop.

n++ means $n$﻿ will +1 every time.

### For Loops

This is the advanced version of loops and also the most used loops in the future.

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;

for (int i = 1; i <= n ; i++) {
cout << "Hi" << endl;
}
}

Result:

Explain:

for (int i = 1; i <= n ; i++) means loop until $n$﻿ is larger than $i$﻿ and $i$﻿ will +1 every time.

### Input using For Loops

Many tasks requires you to use this skill!

#include <iostream>
using namespace std;

int main() {
int x;
cin >> x;

for (int i = 1; i <= x; i++) {
int x[i];
cin >> x[i];
}
}

Explain:

for (int i = 1; i <= x; i++) means that input until $n$﻿ is larger than $i$﻿.

x[i] means input $x$﻿ 個數嘅第 $i$﻿﻿

### Homework

School Hosted: W201 - W206

HKOI Tasks: D201 - D210, D307

## Chapter 6 - Enumeration

THE MOST DIFFICULT PART OF FOR LOOPS!!!!!

Examples:

Explain:

1. Input the number (e.g. 5)
1. Output 5 numbers, then endl

Here’s the code (need to use Nested Loops):

#include <iostream>
using namespace std;

int main () {
int n;
cin >> n;

for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++) {
cout << (i - 1) * n + j;
if (j != n) cout << " ";
}
cout << endl;
}
}

Explain:

for (int i = 1; i <= n; i++) the first loop is for the vertical part.

for (int j = 1; j <= n; j++) the second loop is for the horizontal part.

### Homework

HKOI Tasks: J100, J140, J150, S140, S150

## Chapter 8 - String

Strings are sequences of characters (letters, numbers, symbols, etc.)

Example:

Input: I am handsome.

#include <iostream>
using namespace std;

int main(){
string n;
cin >> n;
cout << n;
}

Output: I

### Getline

The above code will only read the first word because there are spaces between words.

To read the whole line, we should use getline.

#include <iostream>
using namespace std;

int main(){
string n;
getline (cin,n);
cout << n;
}

Output: I am handsome.

### Array

n[i-1] the ith character of the string.

E.g. I am handsome.

n[0] = I

n[1] = (space)

n[2] = a

n[3] = m

...

### Length

To know the length of the string, we should use length.

#include <iostream>
using namespace std;

int main(){
string n;
getline (cin,n);
cout << n.length();  //Don't forget there should always be brackets () after n.length.
}

Output: 14

### Substring

A sub-string is a string inside a string (lmao).

#include <iostream>
using namespace std;

int main(){
string n;
getline (cin,n);
cout << n.substr(5,8);
}

Output:

//I am handsome
0123456789...

handsome

Explain:

Format: n.substr( the character to begin, how many characters afterward)