del_acc's blog

By del_acc, history, 6 years ago, In English

cerr

You can use cerr instead of cout to debug. cerr writes to the standard error stream which is not seen by the online judge.

Example:

instead of cout << "x is " << x << '\n';

you can use cerr << "x is " << x << '\n';

One drawback of this is that cerr increase the runtime of a program. The runtime of the below program would decrease if the cerr statements were removed.

for(int i = 0; i<n; i++)
{
    for(int j = 0; j<n; j++)
    {
        cout << i*j << "\n";
        cerr << "i is " << i << "\n"
        cerr << "j is " << j << "\n"
    }
}

We are facing a trade-off between coding time and running time.

When our solution seems good, we should remove some debugging statements which consume some time.

To remove the cerr statements quickly you can use #define cerr if(false)cerr. Thanks to sharepoLOVEDDD for pointing in out in the comments

To redirect cerr to a file we have three options(source: https://askubuntu.com/questions/625224/how-to-redirect-stderr-to-a-file):

  1. Redirect stdout to one file and stderr to another file: command > out 2>error

  2. Redirect stderr to stdout (&1), and then redirect stdout to a file: command >out 2>&1

  3. Redirect both to a file: command &> out

Watch Macro

This macro is an easy way to debug variables. It also prints the name of the variable:

#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl

You can use it like this:

#include<bits/stdc++.h>
using namespace std;

#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl;

int main()
{
    int n;
    n = 2*2;
    watch(n); //output : n is 4
    int exponent = 2;
    watch(pow(n,exponent)); //output: pow(n,exponent) is 16
}

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it

By del_acc, history, 6 years ago, In English

GetCode

This is an Sublime Text 3 plugin which:

  1. Takes the URL of a problem such as https://www.codechef.com/problems/INVLIS.
  2. Parses the URL and get the problem code(In this case it is INVLIS).
  3. Creates a new .cpp or .py file with a pre-determined template(cpp.snippets which is customized via settings).

Keyboard shortcuts

For all operating Systems, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

How to install

  1. Install Package Control from here
  2. In Sublime, Open Package Control

Open Package Control from the Preferences Menu

  1. Select Package Control: Install Packages

enter image description here

  1. Now you should be able to search for and install the GetCode Package.

How to use it

After installing, it is necessary to configure the settings(setting the default directory and snippets).

After configuring the settings, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

Supported websites(for now)

  1. Codechef
  2. Codeforces
  3. SPOJ
  4. Virtual Judge

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it

By del_acc, history, 6 years ago, In English

In c++, how do I pass multi-dimensional arrays in a function with variable lengths?

Thanks and regards

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it