Gearing Up for Competitive Programming

Sarthak Rout
Dec 1, 2020 · 7 min read

What are you going to learn in this blog?

  • How to install g++ in Windows
  • How to install and setup Sublime Text 3
  • How to create your own build system
  • How to improvise your setup for Competitive Programming

When you compete for in a sport, you do need to have your sports gear with you. Likewise, for competitive programming, we need to set up our IDE to minimize the time taken for routine tasks and enable fast code typing and submission.

In this story, I will show you how to set up Sublime Text 3 for C++ for Windows. Similarly, you can set up VS Code/PyCharm/Spyder etc. for C/C++/Java/Python etc.

For Linux users, you will most probably have g++(You can skip till the installation of Sublime Text 3). You can check if you have g++ by executing:

g++ --version

First, we need to get g++ in our system. It is GNU C++ Compiler. A compiler is something that translates your code in C++ to assembly language, which is a low-level language. Low means it can be directly converted to machine language which the computer understands directly.(without any compiler)

For g++, we will need MinGW manager. It is a package manager which allows us to install GNU packages meant for Linux. This link* explains how to install it — http://www.mingw.org/wiki/Getting_Started and this video does a good job — https://www.youtube.com/watch?v=sXW2VLrQ3Bs (if you follow this through, you may skip till the installation of Sublime Text 3). *Note: If the MinGW link above is down, you may access a snapshot from the web archive — http://web.archive.org/web/20210106033223/http://www.mingw.org/wiki/Getting_Started or directly from Sourceforge — https://sourceforge.net/projects/mingw/files/ .

After installing MinGW, in the end, click Continue to start installing packages. If you missed that, start ‘MinGW Installation Manager’ from the Start Menu. The basic setup option is preselected, and you should choose ALL options (select them -> right-click -> mark for installation) leaving the ada, FORTRAN and ObjC ones. You can even include them if you like. If you want to play safe, choose all. Then, click on Installation->Apply Changes. Wait for the installation to complete.

Now, we must tell Windows that we have g++ in our system. So, search for ‘environment variables’ in the start menu and choose the option ‘Edit the system environment variables’. Follow the black-bordered buttons below —

Click on the buttons in the black border!

Add C:\MinGW\bin after clicking ‘New’ and click ‘Ok’ in each dialog box and exit.
Till this, it is covered well in the video whose link is posted above.

You can check if everything works by firing up the Command Prompt and execute :

g++ --version
If you get this output, hurray, you are on the right track, else contact a secy!

Download Sublime Text 3 from — https://www.sublimetext.com/3. This should not be much of an issue. Start it up after installation.

How your view looks in Sublime Text 3 and how will it look after breaking the view!

One of the common issues that beginners face is the input-output pipeline. This is easily resolved by dividing the screen into 3 regions — one for typing code, one for input and one for output and using freopen.

So, click on View -> Layout -> Columns: 3 and again, View -> Groups -> Max Columns: 2 . This should break your viewing area into 3 parts, as shown above. These view areas act similar to Chrome Windows where you can open as many tabs as you want and view them simultaneously as changes occur.

Now, you would be able to write a program, compile it and execute it by saving it as some file ending with .cpp and pressing Ctrl + B. Let me show that before we discuss build systems —

Create a new file and save it as ‘demo.cpp’ (demo doesn’t matter, cpp does). Also, create ‘input.txt’ and ‘output.txt’ in the same folder and open them separately in the 3 parts as shown in the figure below. Copy the code below and paste it in ‘demo.cpp’.

#include<iostream>
using namespace std;
int main(){
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
string name;
cin>>name;
cout<<"Hello "<<name<<" !";
return 0;
}

Write something (Ex: Buddy) in the input.txt file, after opening it and save it.

This is how it should look till now!

Now, go to Tools -> Build System and select C++ Single File (In general, ‘Automatic’ also does a good job). Then, press Ctrl + B. Now, your “output.txt” file should be changed to ‘Hello Buddy !’.

What is a build system? It is a set of commands that would be executed in the shell terminal to compile your code, execute it and anything else that you want in order. If you want to understand better, go to the CMD and navigate to the folder having a demo.cpp. Then, execute

g++ demo.cpp -o demo_exec.exe

This will compile the file and name it as ‘demo_exec.exe’. Then, when we execute it:

demo_exec.exe

The executable will read input from ‘input.txt’ and write output to ‘output.txt’. The build system automates this whole process.

If you remove the freopen lines, you will have to give input in the terminal, and you will receive the output there.

Compiling and executing C++ Code in the Command Prompt!

If it works well till this point, you can write any program you want now and can execute them but Don’t remove those freopen lines. They essentially connect the code to the input and output files. If it doesn’t work, check what you missed, or feel free to ping us!

Now, let us improvise this more! Whenever we submit code to the online judges for checking, they run the program directly as in the image above and our code doesn’t require ‘freopen’. Removing them every time we submit can be a pain! Hence, it is better to find a way in which ‘freopen’ is executed only locally but is ignored by the online judge.

This issue can be resolved by defining a local constant in our build system.

Also, every time a program is built, a new executable file is created. Over time, these executables eat up space in your system. Therefore, it would be prudent to maintain only one executable as we don’t require them as such. If we require, then we can simply build our code and generate our executable.

This issue is also resolved by fixing the name of the compiled executable in our build system so that only executable exists.

Without much ado, let us create our custom build system!

Go to Tools -> Build System -> New Build System … . Replace the existing code with —

{
"shell_cmd": "g++ -std=gnu++14 \"${file}\" -o \"E:\\NewCP\\outputfile.exe\" \"-DLOCALCP=1\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",

"variants":
[
{
"name": "Run",
"shell_cmd": "g++ -std=gnu++14 \"${file}\" -o \"E:\\NewCP\\outputfile.exe\" \"-DLOCALCP=1\" && \"E:\\NewCP\\outputfile.exe\" "
}
]
}

Note that you have to replace all the 3 occurrences of the path ‘E:\\NewCP\\outputfile.exe’ with ‘<DirectoryName>\\outputfile.exe’, where <DirectoryName> is the name of your directory where you will keep all your code and also, take note of “\\”, each “\” has to be replaced with “\\”. (Read https://en.wikipedia.org/wiki/Escape_character!)
Also, keep in mind that the name of the directory shouldn’t contain spaces, as it can cause problems!
Don’t forget to save the build file with some name (Ex: CP) and choose that build system in Tools -> Build System.

Now, you change your code in ‘demo.cpp’ to —

#include<iostream>
using namespace std;
int main(){
#ifdef LOCALCP
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string name;
cin>>name;
cout<<"Hello "<<name<<" !";
return 0;
}

Here, “LOCALCP” is the constant that we defined to be 1 in our build system and it is unlikely to be present in the build system of any online judge. To change the name of the local constant afterwards, use

-D<NameOfYourConstant>=1 //instead of -DLOCALCP

Also, here, “E:\\NewCP\\outputfile.exe” corresponded to a fixed compiled executable in your chosen directory.

Now, many times, you want to start writing code anywhere you like, but then you would have to copy both of the input and output files each time to that directory. Instead, you could specify an absolute path in the freopen arguments, so that the input and output file remains the same every time! Sometimes, that may not be desirable too... but you can modify them then accordingly. (Get your hands dirty!)

For using the same input.txt and output.txt every time, change the freopen lines to —

freopen("E:\\NewCP\\input.txt", "r", stdin);
freopen("E:\\NewCP\\output.txt", "w", stdout);

Note that you have to use your own directory instead of “E:\\NewCP”. And don’t forget “\\”.

The last tip: you will come across solutions used by various competitive programmers, and you will notice that they have a lot of boilerplate code. They keep a template handy to start typing code as soon as possible! Do explore online for them but don’t copy them blindly!

There are a lot of hacks to improvise your boilerplate code — you can replace all C++ headers with <bits/stdc++.h>. Header files contain the implementation of standard functions and classes that you may need such cout, cin, vector, sort, etc. Keeping track of all required headers can be a headache, so, using <bits/stdc++.h> allows us to not write the required headers explicitly! Now, our “demo.cpp” looks like —

#include<bits/stdc++.h>
using namespace std;
int main(){
#ifdef SARTHAK
freopen("E:\\NewCP\\input.txt", "r", stdin);
freopen("E:\\NewCP\\output.txt", "w", stdout);
#endif
string name;
cin>>name;
cout<<"Hello "<<name<<" !";
return 0;
}

Now you are all set for your wonderful journey in competitive programming! We also have our institute Codeforces group where we hold the Freshers Programming Contest, practice tests, recruitment tests, etc — the links will be shared with you later! Competitive programming is a skill; you must practice regularly to keep your brains sharp and improve your problem-solving skills! You can join Codeforces and/or Codechef and start practising problems and participating in contests. Solve the easiest or the most solved problems at the beginning. Gradually, choose more difficult problems that you are somewhat comfortable with!

This blog post is a part of the Programming Club, IIT Kanpur blog series.

Programming Club, IIT Kanpur

A publication to share everything technology related from hacks to research paper summaries!

Programming Club, IIT Kanpur

We are a community of students in IIT Kanpur looking forward to spreading programming and its domains to everyone!

Sarthak Rout

Written by

An evergreen learner; passionate about technology😄. Currently a Sophomore at IIT Kanpur and Secy at PClub & ECell. https://www.linkedin.com/in/routsarthak/

Programming Club, IIT Kanpur

We are a community of students in IIT Kanpur looking forward to spreading programming and its domains to everyone!