• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Exercises for teaching C/C++ wanted

Hi everybody,

I am currently teaching someone C/C++ and I think
the best way to teach it is having him program lots
of exercises under guidance.

Are there any C and/or C++ exercises on the _internet_?

I only need the questions (no solutions) but the exercises
should be of increasing difficulty and all of the key
topics should be covered (basic types, functions, pointers,
operators, use of standard library...).

For example, I am searching for exercises like:

1) "Read two numbers from stdin and write the bigger of the two to stdout"
2) "Open a text file and count all the words in it"
3) "Open a text file and count the number of occurrences of each character"
4) "Print the function values of sin(x) to stdout, given a
starting and an ending value and a step size."

Thank you,


  • 2
  • 2
  • 2
  • +5
1 Solution
Most textbooks have these sort of exercises at the end of each chapter.  
I agree, consider having him purchase a book on the subject.  However, on the internet, try going to:


for a free book to download (in RTF format I believe).
Also you can use the exercises from Stroustrup's book, or from Lippman's C++ Primer. I'm sure you can find these books in any academic library.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

ppeckAuthor Commented:

thank you for your answers & comments so far.

I know, most books on C++ offer exercises at the end of each chapter, and
I will suggest my student to buy one of these books. What I wanted to
avoid by asking this question, however, was going to a library (bookshop) and browse
10 books or so to find an exhaustive number of exercises from which I can choose
the ones I consider appropriate for my student. I simply want to use the resources
available on the internet for this.

The 2nd link in arnond's proposed answer comes close to what I am searching for.
Bruce Eckel's 2nd edition of 'Thinking in C++' also contains some exercises.
I don't mind if the exercises are part of a C/C++ course and I have to extract them
from there.

Right now, I want to keep the question unanswered. If no better comments/answers
are coming, arnond will get the points. The points will be given at the beginning of
next week.


You should "re-open" / "reject" the current answer if you want to get more people to look at it.  You see, when a question is in the "locked" section of experts-exchange, it tends to get considerably less traffic.  Then, in a week, ask whomever you think provided the best answer to re-submit it as an answer.
One of my favorite programs is 'write a program that solves the 8-queen problem'.

It's a program that needs to find out how you can place 8 queens on a chess board, so that no queen is able to catch another.

disclaimer: personal opinion...

avoid schildt (sp?).  despite the informal writing style, I've found his examples generally poor and/or confusing (especially compared to Eckel), and had occasionally found myself avoiding his examples and teaching ones i dreamt up myself.
My two cent's worth...  Specific programming excercises are all well-and-good up to a point, but to really *learn* a language, pose a series of real-world problems to be solved.  Ideally, these will require some use of basic data structures and algorithms.  Send your pupil off to solve the problem, and then review his/her solution and offer guidance as to how the problem can be solved more elegantly, using less time/memory/disk, etc.  In C++ there is wide leeway for solving problems, and you can use this approach to steer your pupil towards higher-level thinking (i.e., use STL for data structures, etc).

Some personal favorite problems of mine follow.  These are easily defined, but tend to require fairly strong algorithms and data-structures.  Some of these don't really require OO programming, but will teach the fundamentals of the language.  Good solutions will require good data-encapsulation and several class definitions.

Write a tic-tac-toe game.

Write a program to sort all of the input lines.

Write a spell-checker.  This involves finding a word-list on the Internet (easy), and writing a program that accepts the word-list, builds a searchable data-structure, and compares input text against the word-list.

Solve the transitive-closure problem.  The input consists of pairs of symbols, each pair representing an edge in a graph.  Read the pairs, build a forest of undirected graphs from the pairs, and "color" each graph a unique color (number).  Output a list of all symbols and their color.

Use OO techniques to build a simple "validated input" program.  The forms input reads a "definition" consisting of a series of prompts followed by the type of input expected.  For each prompt/type pair, the program prints the prompt and reads some input.  If the input does not match the type expected, it prints and error and prompts again.  The idea is that there are classes of data to input (name, SSN, telephone, free-form), and you want to write a "FieldValidation" base class, with subclasses that implement specific validations for their own field type.  A good excercise in when-to-use-polymorphism.

Write simple "tokenizer", which scans the input for recognized patterns.  For each recognized pattern, it creates and returns a subclass of Token, where each subclass represents a pattern.  For example: TokenInteger, TokenIdentifier, TokenFloat, TokenString, etc.  Each Token subclass has specific methods for accessing the data in the form it recognized.  For example, TokenInteger has a GetIntegerValue() method, TokenFloat has a GetFloatValue() method, etc.
ppeckAuthor Commented:

thank you for your elaborate comment. I will consider your exercises
as soon as my student reaches a level where he can possibly solve them ;-)

I will ask arnond to post a dummy answer to get the points.

Thanks everybody,


Here's my dummy answer, Patrick. Thanks...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now