“Where there is code, there will be errors”
If you have ever been into programming/coding, you must have definitely come across some errors. It is very important for every programmer to be aware of such errors that occur while coding. In this post, we have curated the most common types of programming errors and how you can avoid them.
1. Syntax errors:
These are the type of errors that occur when code violates the rules of the programming language such as missing semicolons, brackets, or wrong indentation of the code,
Example:
Write a function int fib(int n) that returns Fn. For example, if n = 0, then fib() should return 0. If n = 1, then it should return 1. For n > 1, it should return Fn-1 + Fn-2.
For n = 9
Output: 34
Wrong Implementation of Code for the above Problem Statement:
C++
// Fibonacci Series using Recursion #include <bits/stdc++.h> using namespace std; int fib( int n) { if (n <= 1) return n return fib(n - 1) + fib(n - 2); int main() { int n = 9; cout << fib(n); getchar (); return 0; } |
If we review the above code, we can see that a semicolon (;) is missing after the return statement in the ,fib() function and the closing bracket is also missing for the fib() function.
Below is the screenshot of the error.
2. Logical errors:
These are the type of errors that occurs when incorrect logic is implemented in the code and the code produces unexpected output.
Example:
Find GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers that is the largest number that divides both of them.
For finding the GCD of two numbers we will first find the minimum of the two numbers and then find the highest common factor of that minimum which is also the factor of the other number.
Wrong Implementation of Code for the above Problem Statement:
C++
// C++ program to find GCD of two numbers #include <iostream> using namespace std; // Function to return gcd of a and b int gcd( int a, int b) { int result = min(a, b); // Find Minimum of a and b while (result > 0) { if (a % result != 0 && b % result != 0) { break ; } result--; } return result; // return gcd of a and b } // Driver program to test above function int main() { int a = 98, b = 56; cout << "GCD of " << a << " and " << b << " is " << gcd(a, b); return 0; } |
GCD of 98 and 56 is 55
Expected Output: GCD of 98 and 56 is 14
In the above code, the Output produced by the code and the expected output are different. Hence, we can say that there is a logical error in the above code. If we review the above code, we can see the condition, if (a % result != 0 && b % result != 0) is not correct. It should be if (a % result == 0 && b % result == 0).
3. Runtime errors:
These are the errors caused by unexpected condition encountered while executing the code that prevents the code to compile. These can be null pointer references, array out-of-bound errors, etc.
Example:
C++
// C++ program to illustrate // runtime error #include <iostream> using namespace std; // Driver Code int main() { int a = 5; // Division by Zero cout << a / 0; return 0; } |
Below is the error produced by the above code:
4. Time Limit exceeded error:
Time Limit Exceeded error is caused when a code takes too long to execute and execution time exceeds the given time in any coding contest. TLE comes because the online judge has some restrictions that the code for the given problem must be executed within the given time limit.
How to Overcome Time Limit Exceed(TLE)?
Example:
Given two arrays, arr1 and arr2 of equal length N, the task is to find if the given arrays are equal or not. Two arrays are said to be equal if: both of them contain the same set of elements, arrangements (or permutations) of elements might/might not be the same. If there are repetitions, then counts of repeated elements must also be the same for two arrays to be equal.
Expected Time Complexity: O(N)
One possible approach can be to Sort both arrays, then linearly compare the elements of both arrays. If all are equal then return true, else return false. The time complexity for this approach will be O(N*log(N)). But the expected time complexity is O(N), so this code will give Time Limit Exceeded error. In online judges, we need to write code that executes within a given time limit. Hence, we need to optimize the approach.
Another possible approach can be to store the count of all elements of arr1[] in a hash table. Then traverse arr2[] and check if the count of every element in arr2[] matches with the count of elements of arr1[]. The time complexity for this approach will be O(N). Hence code for this approach will not give a time limit error and will get submitted successfully.
Below is the code for the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Returns true if arr1[0..N-1] and arr2[0..M-1] // contain same elements. bool areEqual( int arr1[], int arr2[], int N, int M) { // If lengths of arrays are not equal if (N != M) return false ; // Store arr1[] elements and their counts in // hash map unordered_map< int , int > mp; for ( int i = 0; i < N; i++) mp[arr1[i]]++; // Traverse arr2[] elements and check if all // elements of arr2[] are present same number // of times or not. for ( int i = 0; i < N; i++) { // If there is an element in arr2[], but // not in arr1[] if (mp.find(arr2[i]) == mp.end()) return false ; // If an element of arr2[] appears more // times than it appears in arr1[] if (mp[arr2[i]] == 0) return false ; // decrease the count of arr2 elements in the // unordered map mp[arr2[i]]--; } return true ; } // Driver's Code int main() { int arr1[] = { 3, 5, 2, 5, 2 }; int arr2[] = { 2, 3, 5, 5, 2 }; int N = sizeof (arr1) / sizeof ( int ); int M = sizeof (arr2) / sizeof ( int ); // Function call if (areEqual(arr1, arr2, N, M)) cout << "Yes" ; else cout << "No" ; return 0; } |
Yes
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!