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.

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.


Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now