C / C++ FAQ's and Practice problems

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

C / C++ FAQ's and Practice problems

 
4
  #1
Jan 20th, 2007
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 [search]strstr[/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,
Sanjay aka ~s.o.s~
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C / C++ FAQ's and Practice problems

 
-1
  #2
Jan 21st, 2007
Here is my 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)
Last edited by John A; Jul 22nd, 2009 at 2:26 am.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C / C++ FAQ's and Practice problems

 
0
  #3
Jan 28th, 2007
Personally, the semantics behind a specific programming language do not interest me in the slightest. The link provided is good for extending one's problem solving skills in any programming language.

http://www.topcoder.com/

Sign up and go to > software competitions > problem and set analysis and pit your wits against some of the best in the world.

There was a massive archive of problems from Olympiad Informatics but I can't seem to find it. Hmm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: C / C++ FAQ's and Practice problems

 
-2
  #4
Mar 2nd, 2007
Here is another problem find ten things which you can do in C but not in C++
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 27
Reputation: jetru is an unknown quantity at this point 
Solved Threads: 0
jetru jetru is offline Offline
Light Poster

Re: C / C++ FAQ's and Practice problems

 
0
  #5
Mar 13th, 2007
projecteuler.net

for the math freaks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: C / C++ FAQ's and Practice problems

 
0
  #6
Mar 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: c4swimmers_net is an unknown quantity at this point 
Solved Threads: 0
c4swimmers_net c4swimmers_net is offline Offline
Newbie Poster

Re: C / C++ FAQ's and Practice problems

 
0
  #7
Apr 2nd, 2007
Few Interesting & Challenging programming problems :
http://c4swimmers.net/portal/node/9

Level: Beginners or Newbie C/C++ Programmers
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: C / C++ FAQ's and Practice problems

 
0
  #8
Apr 15th, 2007
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 ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 4
Reputation: chikkubhai is an unknown quantity at this point 
Solved Threads: 0
chikkubhai chikkubhai is offline Offline
Newbie Poster

Re: C / C++ FAQ's and Practice problems

 
0
  #9
Oct 11th, 2007
Originally Posted by ithelp View Post
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 ?
What are the answers for you questions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: C / C++ FAQ's and Practice problems

 
0
  #10
Oct 11th, 2007
>What are the answers for you questions?
This thread isn't about answers, it's about providing questions for you to answer on your own. If you're having trouble, start your own thread to get help.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

Tags
c++, faq, practice, problem, sticky

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC