How to learn programming? In my experience, I have familiarized myself with the basics of programming and understand the fundamental concepts, then I start solve competitive programming problems.

To begin with, it’s recommended to focus on learning the fundamentals of C and C++ programming languages, as they provide a solid foundation.

As I mentioned earlier, problem-solving skills are paramount. Platforms like Hackerrank, Codeforces, and Codechef are excellent resources to enhance your problem-solving abilities. They offer a wide range of coding challenges and practice problems to help you sharpen your skills.

When it comes to running your code, a convenient online tool called ideone is available. It allows you to execute and test your code online, making it a practical option for beginners.

By combining theoretical knowledge, problem-solving practice, and utilizing tools like ideone, we can enhance our programming skills and embark on a successful learning journey.

Programming language?

What is a programming language? Well, first of all, what is language? Hmmmm. According to my interpretation, it is a tool used to facilitate understanding between individuals. When it comes to a programming language, who are you trying to understand? It is the computer. So, what we want the computer to understand is up to us.

Now, let’s examine a sample conversation:

  • A: Hey, what’s the sum of 10 and 5?
  • B: Are you asking if it’s 15?
  • A: Then what about 2123123 plus 12313399341?
  • B: Hmm, use a calculator.

Of course, we don’t need to use a calculator. Instead, we can use a program that we write ourselves. So, let’s attempt to write a program that calculates the sum of two numbers. Where should we start? It’s evident that the computer won’t be able to read our thoughts. Therefore, we must express our ideas clearly using a programming language to ensure the computer understands them.

Input, Output

How to write in C++? Imagination is enough to perform just three actions:

  1. Read the two numbers.
  2. Calculate their sum.
  3. Print the sum.

However, in reality, achieving this in C++ is slightly different.

Programm should start at some point. A C++ program starts with the main() section when it begins, which is

int main() {
	// things will work by line after line
}

The structure starts with working down from the {} items. However, we need to read 2 numbers, print the answer, so we need to call the library that can perform these operations. C++ has numerous libraries that provide ready-made functionalities, eliminating the need to write them from scratch.

The read and print operations are part of the iostream library, which we invoke at the beginning of our code, as shown below:

#include <iostream>

Well, we have imported a library that can handle various types of reading and printing, and now we know where our code starts.

Now, what’s the next problem? Do we need to read two numbers immediately? Not so fast. Just like when we meet a stranger, we don’t know their name right away. Our program doesn’t know that there are two numbers involved yet, so we need to make it understand. We’ll introduce them to the program by calling them a and b. However, identification is also indirect, and we need to specify the data type. In this case, let’s assume that we are trying to find the sum of two integers. So, a and b are integers. We make it clear in the C++ language that a and b are integers.

int a, b;

Typing ; indicates the end of the section. These a and b are called variables.

Now we need to read the two numbers. We can do this using the cin function.

cin >> a;
cin >> b;

The statement is written in the form of “putting the first number in the variable a and the next number in the variable b.”

We have read two numbers. Now, let’s find the sum and store it in the variables.

int c = a+b;

And the sum of a and b is our answer, which is stored in the variable c. Since adding two integers results in an integer, so the type of c is int.

Now, use cout to print the answer.

cout << c << endl;

and this means that the value of variable c will be printed to the screen. Now, let’s put these pieces together.

// "//" it's comment, it won't affect our code
#include <iostream> 	
using namespace std; 	

int main() { 
	int a = 0, b = 0;	

	cin >> a;
	cin >> b;

	int c = a+b; 

	cout << c << endl;
	return 0;
}

Float

In the example above, we were only working with integers. In the C++ language, you can work with various types of data, including fractional numbers. One example of such a data type is double, which represents fractional numbers. Another data type is float, which can store 32 bit, but we will discuss this in more detail later.

In this example, we are reading two numbers and finding their roots. How do we calculate the root of a number? Actually, we don’t have to worry about that because all the calculations are handled by the sqrt function in the math.h library.

#include <iostream>
#include <math.h> // math library

using namespace std;

int main() {
	double a, b, x, y, x1, y1; 

	cin >> a >> b;

	x = a/b;
	y = a*b;
	x1 = sqrt(a); 
	y1 = sqrt(b);

	cout << x << " " << y << " " << x1 << " " << y1 << endl;
	return 0;
}

A condition check operation or if

Until now, we can add, subtract numbers and find their roots. But how do we perform actions that work differently depending on certain conditions? For instance, let’s create a program that prints “EASY” if the given number is less than 10, and “HARD” if it’s not. First, we read the number. And then? To achieve this, our code needs to branch, checking if the number is less than 10. If it is, we print “EASY”; otherwise, we print “HARD”.

if(a < 10) {
	printf("EASY");
} else {
	printf("HARD");
}

said and

if(condition) {
	// if it's true
} else {
	// if it's false
}

is written in the form and the full code is

#include <iostream>
using namespace std;

int main() {
	int a;
	cin >> a;
	
	if( a < 10 ) {
		cout << "EASY\n";
	} else {
		cout << "HARD\n";
	}
	return 0;
}

About characters and ASCII codes

What is the ASCII code? It is a number or code by which a computer recognizes a character. In other words, the computer stores the letter ‘A’ as a number. For example, ‘A’ is recognized by the code 65, so ASCII code is 65, it represents ‘A’. Similarly, if the code is 97, it represents ‘a’. Therefore, each character has its own identification number. There are a total of 256 characters, with codes ranging from 0 to 255.(in UTF-8 extends the ASCII character)

For instance, let’s write a program that, given a letter, prints “Togmod” if it is uppercase and “Jigmed” if it is lowercase.

#include <iostream>
#include <string.h> // string library
using namespace std;

int main() {
	char s;

	cin >> s;
	if( s >= 65 && s <= 90 ) {
		cout << "Togmod\n";
	} else {
		cout << "Jigmed\n";
	}
	return 0;
}

FOR loop

While we were writing a program without encountering any issues, but someone asked us to write a program that prints all the numbers up to a certain point. Certainly, we can do that. We read the number N, and if it is greater than or equal to 1, we print 1. If it is greater than or equal to 2, we print 2. If it is greater than or equal to 3, we print 3. and so on …

	int n;
	cin >> n;
	if(n >= 1) {
		cout << "1";
	}
	if(n >= 2) {
		cout << "2";
	}
	if(n >= 3) {
		cout << "3";
	}
	...

But how long can we keep writing like this? If you’re required to write up to 247 billion lines of code, would you do it? Moreover, it’s quite time-consuming and boring. If we notice this code, we repeating the same things over and over again. We cannot continue like this unless we find an easy solution. To address this and save time, we have the for loop in C++ language.

When using a for loop, there are three parts to consider:

  1. The initial value to start with.
  2. The conditions that need to be met for the loop to repeat.
  3. How the value will change with each iteration.

These three parts are crucial, and if the conditions specified in the second part are met, the code inside the loop will execute. It is important to pay close attention to the second part to avoid potential issues or unintended consequences.

for(initial value of variable; condition to be fulfilled to loop; changing part of variable) {
        //  If the condition is true, the actions inside these {} brackets will be executed.
        //  After the operations inside these parentheses are completed, the variable is changed.
     }

The code to solve the original problem is written below

#include <iostream>
using namespace std;

int main() {
	// Problem: given an integer, all numbers from 1 to that number will be printed on one line
	int n, i; 
	cin >> n; 

	for(i = 1; i <= n; i = i+1 ) {
		cout << i << " ";
	}
	return 0;
}

WHILE loop

The next loop type is while and it’s similar to a for loop, but it only requires a condition. If the condition is true, the code inside the loop will be executed.

Let’s dive into writing a program to find the sum of the digits of a number. The trick is to find the last digit of the given number and add the value to one variable. Then add the digit in front of this digit. Then the front of that, etc. But how do you add this? we can find the last digit. It is the remainder of that number divided by 10. But how to find the digit in front of it? It is the result of dividing that number by 10 and remainder of that number by 10. And so on, if this number is greater than 0, this operation will continue. This is done using a while loop.

For example, let’s think about the number 142.

  • The last digit 2 is added to the answer and divided by 10. Now this number will be 14. The answer is 2.
  • The last digit 4 is added to the answer and divided by 10. Now this number will be 1. The answer is 6.
  • The last digit 1 is added to the answer and divided by 10. now this number will be 0. The answer is 7.
  • Since this number is equal to 0, there is no need to think further, so the loop stops. Then print the answer.
while( condition ) {// if the condition inside the while parentheses is true
    // the following actions will be performed.
}
#include <iostream>
using namespace std;

int main() {
	int n, i, s;

	cin >> n; 

	s = 0; 

	while( n > 0 ) {
		s = s + (n%10); 
		
		n /= 10; 
	}

	cout << s << endl; 
	return 0;
}

About Function

We have already used functions before, such as when finding the root of a number. These functions reside in their respective libraries and can be utilized whenever needed. Now, let’s explore how to write our own function. What are the benefits of writing custom functions? They enhance code organization and eliminate the need for repetitive code. As we write our own function, we will define the return value, retrieve values, and name them accordingly. If there is no need to return a specific value, the function is referred to as void.

// function
#include <iostream>
using namespace std;

void plp( int a, int b ) { // function to print from a -> b
	int i;
	for( i = a; i <= b; i++) {
		cout << i << " ";
	}
	cout << endl;
	return;
}

int jkl( int x, int y ) { // A function that returns the minimium of 2 numbers
	if( x > y ) return y;
	return x;
}

int main() {
	int a, b;
	cin >> a >> b;

	plp( a, b );

	cout << jkl( a, b ) << endl;
	return 0;
}

About Struct

What is a struct? A struct is a data type used to group related elements together. One advantage of a struct is that it allows you to define the type and name of each element within the group when creating the struct. For example, if we create a struct called “Person,” we can have elements such as name, age, and sex.

struct name{
  member_type member_name;
  member_type member_name;
   ....
  member_type member_name;
};
#include <iostream>
using namespace std;

struct person{
	string name; 
	string sex;
	int age; 
} shiree, lol; 

void pr( person x ) {
	cout << "NAME:" << x.name << endl; 
	cout << "SEX:" << x.sex << endl; 
	cout << "AGE:" << x.age << endl; 
	cout << endl;
	return;
}

int main() {
	shiree.age = 12; 
	shiree.sex = "Eregtei"; 
	shiree.name = "sandal"; 

	pr( shiree ); 

	person a; 
	a.age = 0;
	a.name = "Nergui";
	a.sex = "Saarmag";

	pr( a );

	cin >> a.name >> a.sex >> a.age; 

	pr(a);
	return 0;
}

Summary

In general, I write about various actions and provide examples of how they can be implemented in the C++ language. While these actions may seem simple, there are numerous possibilities and functionalities associated with them. Feel free to explore them on your own. If you have any questions, please leave a comment.