Hello to all programmers out there. Considering the growing request for practice problems by the beginners, we ( Me, Joey, Niek, Aaron..) have decided to start a sticky which will host some common practice problems which would help the beginners in understanding the programming concepts in a better way. (Did I mention 'Practice makes a man perfect' ? ;) )
The practice problems enlisted will have their own difficulty level or the skill level required(beginner to expert). However feel free to jump to any of them if you think you are up for it.
Please don't post "Thank you" posts in this thread since this is meant to be used as a guide for all beginners. Also if you have already developed a solution and want to get it verfied, don't post it here -- create a new thread. Any "Thank you", "Help Me", "Spam" posts will be prompty deleted. I hope you understand this.
Some general guidelines while attempting the problems for beginners or those who have just started programming:
- The use of all non standard headers and functions is discouraged. Implement the problem with only standard C / C++ functions. A list of standard C / C++ functions and headers can be found here.
- Don't use system("pause") to pause your program if possible. Use getchar( ) if you are using C and cin.get( ) if you are using C++.
- The prototype of the entry point of the program i.e. the main function is int main( void ) . Anything else is non standard and incorrect. See here for explanation.
- If possible stay away from using scanf( ) for accepting input from the user. The fgets( ) and sscanf( ) combination works the best here. It also gives you more control over the way you interpret and analyse the user input. For a detailed tutorial look here.
- Try giving meaningful names to the variables you use. Though it may seem like a lot of work using names like 'my_string' when 'a' seems to be doing the job pretty well, it would save you from a lot of head banging in the future specially when you can tell what a varaible is meant to do in a program just by looking at its name.
- If possible do initialize variables when you declare them -- especially pointers. It would save you from a lot of trouble when you variables start returning junk values and you have no idea where they came from.
- After you are done with pointers in your program, don't forget to ground them (set them to NULL) to prevent their inadvertent use after they have lost their meaning or outside their context. This will definately save you from pulling your hair out when the program becomes large and accessing stray pointers would only result in hard to find bugs.
- Don't forget to check for return values of functions. Even though it may look to you that the function would never fail, if they do, there sure would be some obscure, hard to find bugs. One classic eg. is checking for return value of the malloc function which returns NULL if allocation fails. Ditto for fopen( ) .
Now, some practice problems from my side:
- Write a program which finds the factorial of a number entered by the user. (check for all conditions) (Beginner).
- Create a program which generates fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8 (beginner)
- Write a program to simulate a simple calculator. It should accept two number from the user along with the required operation to be performed. Addition, Subtraction, Division and Multiplication are the basic operations that should be implemented. Feel free to implement the other operations (Beginner)
- Create a simple Palindrome checker program. The program should allow the user to enter a string and check whether the given string is a palindrome or not. Only digits and alphabets should be considered while checking for palindromes -- any other characters are to be ignored. (Intermediate)
- Implement your own
- Write a program which will print all the pairs of prime numbers whose sum equals the number entered by the user. ( suggested by Aniseed ) (Intermediate)
- Write a program which will perform the job of moving the file from one location to another. The source and destination path will be entered by the user. Perform the required error checking and handle the exceptions accordingly. (Intermediate)
- Write a program which performs addition, subtraction, multiplication of matrices. The dimensions of both the matrices would be specified by the user (dynamic memory allocation required). Use of structure or a class to define the matrix would be a good idea. (Expert)
Hope this guide helped you.