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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

636 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