📖

your first fantastic book about coding.

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

Table of Content

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:

🌟
Types of 'container' int = Integer double / float = Decimal number long long = -(2^63) to (2^63) char = Character boolean = True / False

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:

🌟
+ Addition - Subtraction * Multiplication / Division (Only for integers) % Modulus (餘數)

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

Login name format:

wy_2xxxx

First-time login:

1. Navigate to the following URL

2. On the login page, click "Forget password" (next to the word "Password")

3. Enter your email address "s2xxxx@wahyan.edu.hk"

4. Check your email and follow the instruction to reset your password.

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

HKOI Tasks: D100 Calculate A+B

Chapter 2 - More Basic Stuff

Conditions

The usual logical conditions from mathematics.

Less thana < b
Less than or equal toa <= b
Greater thana > b
Greater than or equal toa >= b
Equal toa == b
Not Equal toa != 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’.

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.

🌟
Note:

You can run any code you want if the condition is true by typing the code after if (condition...){ and }

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.";
}

Additional Maths on C++

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=13cos(60)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/tansin / cos / tan.

🌟
Other useful Maths functions: pow : Raise to power - pow(x, 2) means x2x^2 sqrt : Compute square root - sqrt(x) means x\sqrt{x} ceil : Round up value - ceil(2.4) then the result is 3 floor : Round down value - floor(2.6) then the result is 2 round : Round to nearest - round(2.6) then the result is 3 Noted that functions including ceil, floor and round, the variable MUST be double P.S. other not-quite-useful-but-still-important functions can be found here.

cos(60 * 3.14159 / 180) noted that all sin/cos/tansin/cos/tan equations MUST be written in this format (3.14159/180)( * 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.

🌟
Noted that the xx in the code MUST be float/double. Other really-not-quite-useful functions can be found here.

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

HKOI Tasks: D101 - D108

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 EnglishIn C++
p AND qp && q
p OR qp || q
NOT p!p
p XOR qp ^ q
p NAND q!(p && q)
p NOR q!(p || q)
p XNOR qp == 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:

pqp || q
000
011
101
111
pqp && q
000
010
100
111
pqp ^ q
000
011
101
110
pqp == q
001
010
100
111

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. Follow the table above.
  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.

Homework

HKOI Tasks: D501, NP1701, NP1931

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 nn 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 nn is larger than ii and ii will +1 every time.

🌟
Don’t think that this is easy!

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 nn is larger than ii.

x[i] means input xx 個數嘅第 ii 

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:

🌟
Since the letter ‘h’ is in the 5th position, and the word ‘handsome’ got 8 characters.

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

Homework

HKOI Tasks: D301, D302, D308