Anybody knows of good C/C++ exercises for beginners on the Web?

I found out that my learning process goes much smoother when I can get hands on the things. Pure theory puts me to sleep.

I did a little research myself but couldn't come up with anything. There are plenty of tutorials, no exercises though. I appreciate your response!


  • visit

    there are few good links

    : I found out that my learning process goes much smoother when I can get hands on the things. Pure theory puts me to sleep.

    : I did a little research myself but couldn't come up with anything. There are plenty of tutorials, no exercises though. I appreciate your response!

  • When I taught college C++, I found the following projects to work well. To the complete beginner, some of these are very challenging. Even to the expert, some of the possible enhancements (especially for the piglatin one) hold a few surprises.

    If you need a hint on any of these, e-mail me and I'll be happy to help. These are fun projects that are challenging (as challenging as you want them to be) without taking too long to write. Great hobby stuff.

    1. Pick a number

    In it's simplest form, the computer generates a random number (look up srand() and rand()) and prompts the user to guess it. The computer prompts the player with "higher" and "lower" until the user gets it right. A slightly more complex version asks the user for a number and then tries to guess it, letting the user indicate "higher" or "lower". The final version uses two modules--best as C++ objects, one that generates a random number and one that tries to guess it.

    This project teaches basic I/O, storage in variables, looping, and branching.

    2. Blackjack

    This project is more complicated (and more rewarding) than the pick-a-number game, but many of the underlying principles are the same. goal is to write a computer blackjack game with simplified rules. The simplified rules are as follows:

    a. Aces are always 11 points. (told you it was simplified)

    b. Face cards (card values 11-14) are worth 10 points

    c. All other cards are worth face value

    d. User is given a single card, then asked to "hit" or "stay" until he/she exceeds a total of 21 or stays.

    e. The computer goes through the same process, randomly drawing cards until it exceeds 16.

    f. The player with the highest value under 21 wins.

    I recommend using random numbers between 1 and 14 to represent the cards. This project teaches program logic, functions (try to put this all in main() and you'll be sorry!), and more complex looping and branching.

    Once the basic version is down, start adding a few enhancements:

    a. Display "Jack" instead of 11, "Queen" instead of 12, and so forth.

    b. If the player gets 21, he wins automatically--the computer doesn't get a turn.

    c. Aces can be either 1 or 11, at the user's choice.

    d. Aces can be either 1 or 11, and can change on-the-fly as necessary (requires arrays).

    3. Piglatin

    This is one of my favorites, and is wonderful for learning complicated string handling. Piglatin is a children's pseudo-language done by taking the first consonant sound of a word and putting it at the end, followed by "ay." From a programming standpoint, the difficult part is figuring out what constitutes a consonant sound. Remember that some constanant sounds have multiple letters--School has three! I suggest the following basic algorithm:

    a. Locate the first vowel in the word.

    b. Copy all letters from the vowel on to a temporary string.

    c. Copy all letters before the vowel to the end of the temporary string.

    d. Add "ay."

    Once the basic version works well, there are a number of things you can do to "tweak" it:

    a. Try translating a whole sentance, word-by-word. Watch for punctuation marks! strtok() will make life easier here.

    b. Finding an algorithm that correctly handles "Yes" and "System" (not to mention "Fly")is tricky. What do you do with those pesky Y's, that can be both vowel and consonent? E-mail me if you need a hint.

    c. QU should be handled seperately. For example "square" should be printed as "aresquay".

    d. Capitalization requires special attentiuon. "Hello" should be "Ellohay," not "elloHay."

    e. Some special cases, such as "America" that both start and end with a vowel need an extra y at the end--"Americayay."

    4. Hangman

    The game of hangman is a fun but challenging project. One of the hardest points here is user interface. This is a good project for anyone wanting to learn Windows programming beyond simple dialog work. I won't go into much detail here--everyone knows the rules and there aren't too many surprises here.

    5. Universal Numbers

    This is an excellent excersize for Object Oriented Programming, but is a little on the advanced side. The task is to write a fully-encapsulated "Number" class that can hold a floating point number, an integer, or a fraction. The fraction is the hard part. Software that uses it should be able to use it for mixed math; that is, multiple a fraction by a float (which always yields a float), etc.

    If this is too complex, start with just a fraction class and work from there.

    For Windows programmers, I also recommend trying to sublcass or superclass the edit control to produce a control that can handle fractions. For example, "1/2" should be legal to enter, but not "1/2.3" or "/3" or something.

    For DOS programmers, a member function of your number class should be able to input or output fractions as well as floating point numbers/integers.

    Some may be tempted to add complex number support as well. Most C++ books have a sample along those lines. Me, I never saw a use for imaginary numbers.

    6. Programming Games

    I'm going to insert a plug for my own free-ware program here. I hope nobody calls it "spamming."

    There are a number of games available where players battle eachother through programming. One of my favorites is "AI Wars: The Insect Mind." The offical page is here, but follow the link to the forum as soon as you find it. Tell them Mantis sent you. The registered version is much better than the shareware version. The language is BASIC-like.

    My own is at this site, and involves whole fleets of programmed starships battling eachother. The langauge here is somewhat C-like (although not, by any means, C), and you write the code for fleets of starships that battle fleets powered by the code of other programmers.

    There are other games as well. These are a good outlet for programmers trying to learn the ropes. In order to succeed at these games, you must be clever and efficient. Slow code translates to unresponsive AI. Unresponsive AI get defeated every time.

    Good luck!

  • Did anybody straighten you out?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!