The legal tricks-Learn Your Self

Latest gadgets,softwares,hardware,reviews,programming and campuses, game cheats ext......

C / C++ FAQ's and Practice problems

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 Spam or 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 strstr
DaniWeb
Search DaniWeb
Blog Tag Related Entries
Search in forums
Search in code snippets
Search in blogs
Search in link directory
Web Search
Google Search
Wikipedia Search
function. (Intermediate)

* 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.

Regards,
Guru

---- EDIT ----

Here is my second contribution. Many of these problems occur frequently on the forums, but I thought these were worth saving and so I reproduced some of them here.

* Write a program that allows you to input students' scores and weights. The program should then calculate a weighted average and score based on the data inputted by the user. (Beginner)

* Make a program that allows the user to input either the radius, diameter, or area of the circle. The program should then calculate the other 2 based on the input. (Beginner)

* Create a program that implements a database in C++. The fields are hard-coded, and the data is saved in a binary file. Although this isn't really flexibility, you aren't relying on any external libraries or functions. (Beginner)

* Create a few classes that model playing cards. Then use this framework to create your favorite card game. (Intermediate)

* Write a program that accepts XHTML, parses and removes the tags. Then it prints out the remaining text. (Intermediate)

* Write a program which reverses the numerals in an integer, that is 326 becomes 623, etc.. (Intermediate)

* Create a sophisticated linked list class. You should be able to insert and delete nodes anywhere in the list, and the nodes should have pointers to nodes both in front and behind them. (Intermediate)

* Create a binary tree which has search and sorting functions. (Expert)

* Create a Quine, that is, a program that prints out its own source code. (Expert)

In if you're feeling really bored...

Implement your own version of the Standard Template Library!

Although that might keep you busy for a while...

Hope this helped for all ya bored programmers,

Guru

---- EDIT ----

Some for graphics ppl:

* Write a program to draw a rectangle, ellipse, square, circle, point and line based on user input. (Beginner)
* Write a program to emulate Microsoft Paint. It should be possible to switch between different tools (circle, rectangle, eraser...) using pre-defined key strocks. - Intermediate
* Write a program to plot a simple x-y graph for a harcoded function (e.g. y=cos(x)). It should be possible to zoom in on any part of the graph. - Intermediate.
* Write a program to plot a graph of given equation of form y=f(x) and a range for x as command line arguments. (e.g. my_graph_plotter -eq="y=x*x" -xmin=-10, -xmax=10) - Expert. (PS: more to do with equation solving than graphics)
* Write the classic brick-break-out game. E.g. see DX Ball. - Expert.

---- EDIT ----

Few more
1. How to call a C++ function which is compiled with C++ compiler in C code?
2. When you deliver your C++ headers and C++ library of a class (what all can you change in the class so that application using your class does not need to recompile the code)
3. How do you initialize a static member of a class with return value of some function?
4. How can one application use same apis provided my different vendors at the same time ?

0 comments: