Tips and Tricks for Perplexed!

Congrats to all participants for making the Perplexed Trial Round successful and thanks for your patience and enthusiasm during the contest.

Perplexed does not aim to test your knowledge about challenging algorithms but a platform to learn about the dimensions of C language and honor the best in this regard.
A problem asked in Perplexed is simple and can be solved through various ways. You need to submit codes which suit most according to given constraints.

There are some tips which can be useful during Perplexed Main Contest:

  1. Make a solution that works first. Then try to improve it without using the constrained keywords and operators.
  2. Do not include any header files or macros. The C compiler will automatically get the header files from the standard header directory implicitly. C90 had a feature (absent of C99 and C++) called implicit function declaration: when you used a name not declared yet in a function call, the compiler behaved as if:
    
    extern int identifier();
    

    had been seen. That feature has been dropped from C99 and most compilers had option to warn about this even before C99 was promulgated. Read more here.

  3. Use small variable names to save total number of characters for the questions, where total number of characters are constrained.
  4. Declare variables without specifying any data type to save number of characters. According to section A7.3.2 Function Calls, Dennis Ritchie, “In the old style, parameter types are not specified. If no declaration is given for a parameter, its type is taken to be int”. C still supports this old style.
    1. Global variables declared without any data type. Eg:
      t;main(){}

    2. Arguments to main eg:
      main(a,b){scanf(“%d%d”,&a,&b);}

  5. Sometimes number of test cases can be ignored and we read from the input data set till the end of file has been reached.
    1. One can suppress an input by using scanf(“%*d”). Here * is used to ignore the integer data retrieved from the stdin. This way one can ignore the input which is not required, like number of test cases at beginning of file. See B.1.3 Formatted Input, Dennis Ritchie
    2. scanf() returns number of data successfully read and returns -1 when end of file has been reached. This returned value can be smartly used to obtain all input data. for eg:
      while(~scanf("%d",&a)){}

      In this, when scanf is succussful in reading, it returns some positive value. When the end of file is reached it returns -1, and ~(-1)=0, which stops the while loop.

  6. Bitwise operators are really wise. These can be used frequently as an alternative for some operations. A very good reference to this is section 7.1.3 of Art of Computer Programmiing, Vol 4 by D. E. Knuth. Some examples:
    1. To find the larger between two numbers, we have:
      
      if(b/a)
      {
        a = a^b;
        b = a^b;
        a = a^b;
      }
      

      Here, the larger value is stored in variable ‘a’. We are able to find this without using ‘’ or any temporary variable.

    2. There are alternative ways of operations using bitwise operators. eg: An operation a-b can be performed also by a+(~b+1), here we first find 2's complement of b, and then add to a.

Yes, you got it right. We want you to write a program which on compilation may generate lot of warnings but no errors!

PS: Use of whitespace characters is never restricted. So, you can always use whitespace characters and write codes which are clean and easy to read.

This entry was posted in Algorithms, Coding, Debugging, Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>