Some general tips for programming.

    by nikoo28

    A Programming Contest is a delightful playground for the exploration of intelligence of programmers. To start solving problems in contests, first of all, you have to fix your aim. Some contestants want to increase the number of problems solved by them and the other contestants want to solve less problems but with more efficiency. Choose any of the two categories and then start.

    If you are a beginner, first try to find the easier problems.Try to solve them within short time. At first, you may need more and more time to solve even simple problems. But do not be pessimistic. It is for your lack of practice. A good programmer spends 10% time for coding and 45% time for thinking and the rest of the time for debugging. So to decrease the time for coding you should practice to solve easier problems first as they increase your programming ability.

    1. Programming languages and dirty debugging

    Most of the time a beginner faces this problem of deciding which programming language to be used to solve the problems. So, sometimes he uses such a programming language which he doesn’t know deeply. That is why; he debugs for finding the faults for hour after hour and at last can understand that his problem is not in the algorithm, rather it is in the code written in that particular language. To avoid this, try to learn only one programming language very deeply and then to explore other flexible programming languages. The most commonly used languages are C, C++, PASCAL and JAVA. Java is the least used programming language among the other languages. Avoid dirty debugging.

    2. Avoid Compilation Errors

    • When you use a function check the help and see whether it is available in standard Form of the language. For example, do not use strrev() function of <string.h> header file of C and C++ as it is not ANSI C, C++ standard. You should make the function manually if you need it. Code manually or avoid those functions.
    • Don′t use input and output file for your program. Take all the inputs for standard input and write all the outputs on standard output (normally on the console window).
    • Do not use <conio.h> header file in C or C++ as it is not available in standard C and C++. Usually don′t use any functions available in header file. It is the great cause of compilation error for the programmers that use Turbo C++ type compiler. Using Turbo C++ type compiler should be avoided every time.
    • Built-in functions and packages are not allowed for using in online judge.

    3. Forget Efficiency and start solving easier problems

    Sometimes, you may notice that many programmers solved many problems but they made very few submissions (they are geniuses!). At first, you may think that I should try to solve the problems as less try as possible. So, after solving a problem, you will not want to try it again with other algorithm (may be far far better than the previous algorithm you used to solve that problem) to update your rank in the rank lists. But my opinion is that if you think so you are in a wrong track. You should try other ways as in that and only that way you can know that which of the algorithms is better.
    Again in that way you will be able to know about various errors than can occur. If you don′t submit, you can′t know it. Perhaps a problem that you solved may be solved with less time in other way. So, my opinion is to try all the ways you know. In a word, if you are a beginner forget about efficiency.

    4. Study Algorithms

    An algorithm is a definite way to solve a particular problem. If you are now skilled in coding and solving easier problems, read the books of algorithms next. Of course, you should have a very good mathematical skill to understand various algorithms. Otherwise, there is no other way but just to skip the topics of the books. If you have skill in math, read the algorithms one by one, try to understand. After understanding the algorithms, try to write it in the programming language you have learnt. If you can write it without any errors, try to find the problems related to the algorithm, try to solve them.

    Remember – Practice makes a man perfect.

    Some links to online problem solving portals are:-
    http://www.spoj.com
    https://leetcode.com/
    http://www.codechef.com
    http://www.projecteuler.net
    http://www.topcoder.com

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array of n integers that has been rotated an unknown number of times, give a O(log (n)) solution that finds an element in the array? Input: arr [] = {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}. Find 5 . Output: 8 (the index of 5 in the array) Let us assume that the given array is arr[]. The basic approach for this problem will be:- Find the pivot element. That means after which all elements are in ascending order. This basically …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a wild pointer?

    by nikoo28
    2 minutes read

    Question: What is a wild pointer in a program? Uninitialized pointers are known as wild pointers. These are called so because they point to some arbitrary memory location and may cause a program to crash or behave badly. This can be understood from the below examples. Please note that if a pointer p points to a known variable then it’s not a wild pointer. In the below program, p is a wild pointer till this points to a. If we want pointer to a value (or set of values) without …

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a memory leak?

    by nikoo28
    2 minutes read

    Question:What is a memory leak in a program? We have been writing many programs where we have used the “free()” keyword. One such example can be found in the “Delete the complete list” function found in the post – Important traversals in a Linked List. The code snippet is:- This is done due to a reason. When we allocate a memory in a program, usually that memory is unallocated once the execution of the program finishes. However, there may be some cases where the program runs forever and these scenarios, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • StringsTheory

    Swap strings in C

    by nikoo28
    3 minutes read

    Question:How will you swap two strings in C? Let us consider the below program. Output of the program is str1 is study, str2 is algorithms. So the above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. Let us see the correct ways for swapping strings: Method 1(Swap Pointers):- If you are using character pointer for strings (not arrays) then change str1 and str2 to point each other’s data. i.e., swap pointers. In a function, if we want …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) What we discussed was the most naive approach by using two for loops. However, this method is not optimized and will take a lot of time if the data is very large. We need to improve the complexity of the program. One such method is …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with both positive and negative numbers, find the two elements such that their sum is closest to zero. ? Input: arr [] = {1, 60, -10, 70, -80, 85} Output: -80, 85 (Their sum is -5 that is closest to 0(zero)) We can solve this problem by a brute force approach. That involves two loops, in the first loop we pick an element from the array and in the second loop we add this element individually with each of the other elements in the array. In …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More