943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 130241
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 20th, 2007
14

C / C++ FAQ's and Practice problems

Expand Post »
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~
Similar Threads
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 21st, 2007
2

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

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 28th, 2007
1

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

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 2nd, 2007
-3

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

Here is another problem find ten things which you can do in C but not in C++
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Mar 13th, 2007
-3

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

projecteuler.net

for the math freaks!
Reputation Points: 10
Solved Threads: 0
Light Poster
jetru is offline Offline
27 posts
since Jun 2006
Mar 27th, 2007
-1

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

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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 2nd, 2007
0

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

Few Interesting & Challenging programming problems :
http://c4swimmers.net/portal/node/9

Level: Beginners or Newbie C/C++ Programmers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
c4swimmers_net is offline Offline
2 posts
since Apr 2007
Apr 15th, 2007
0

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

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 ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 11th, 2007
1

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

Click to Expand / Collapse  Quote originally posted by ithelp ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chikkubhai is offline Offline
4 posts
since Oct 2007
Oct 11th, 2007
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how to program a program to calculate the area and the perimetre of a circle?
Next Thread in C++ Forum Timeline: Help with my code/string compare





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC