Exercises for teaching C/C++ wanted

Posted on 1998-09-30
Last Modified: 2012-06-21
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,


Question by:ppeck
  • 2
  • 2
  • 2
  • +5
LVL 22

Expert Comment

ID: 1174067
Most textbooks have these sort of exercises at the end of each chapter.  

Expert Comment

ID: 1174068
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).

Expert Comment

ID: 1174069
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.
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Expert Comment

ID: 1174070

Author Comment

ID: 1174071

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.



Expert Comment

ID: 1174072
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.

Expert Comment

ID: 1174073
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.


Expert Comment

ID: 1174074
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.

Expert Comment

ID: 1174075
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.

Author Comment

ID: 1174076

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,



Accepted Solution

arnond earned 50 total points
ID: 1174077
Here's my dummy answer, Patrick. Thanks...

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question