Need C++ Exercises

Recommended Posts

arithma    226
OK. So I'm giving this C++ lab but the textbook doesn't seem to provide a lot of exercises. I've been scoundering the internet for about an hour and found a little bit of material but I guess more is needed. The course is an introduction to programming (as in C in C++ of sorts), with no classes (no OOP at all)... All resources are appreciated, thanks

Share on other sites
ToohrVyk    1596
Well, what do you intend to teach? Expressions? Loops? Exceptions? Memory allocation? Pointers? References? Const correctness? Functions? SC++L?

Usually, once you know what you want to teach about, it's easy to set up small exercises to be performed.

Share on other sites
arithma    226
SC++L?
Other than that, yes these are the things I want to solve with these students.. I don't think am qualified enough to make my own set of exercises (I do make them draw a lot of stars in the console though).
I can solve with them a lot though. (3 continuous hours weekly lab, +1 hours solving session on the board for questions and the likes).
I didn't make my twenties yet and my C++Skills is about 4 years. But I am a student as well so I don't have the luxury of the time for making exercises / neither do I claim capability.

I heard about Schaum's Outlines to Programming in C++ or something like that. Anyone recommends it, any other EXERCISE textbook recommendation?

Share on other sites
Guest Anonymous Poster
I think that this may be what you are looking for. I used this to learn C++, a very good book. This link is to Amazon so you can use the "Search Inside" feature :)

Share on other sites
John_23    200

http://www.amazon.com/Beginning-C++-Game-Programming-Development/dp/1592002056

Share on other sites
ToohrVyk    1596
Tough. Though, I'm not that much older than you, and I make my own exercises (although in Caml Light). What I would suggest is to get an algorithms book from your local library (or, better yet, buy it!) such as the Cormen-Leiserson-Rivest-Stein Introduction To Algorithms, and make them implement some of the algorithms there in C++. It usually serves as a great introduction to the basics of the language, while providing them with a goal that feels greater than HelloWorld or FizzBuzz.

Reimplementing standard library features (sort, heap, stack, queue, reverse) are always a possibility. Also, some of my personal favourites are:
• Counting the number of primes smaller than 180,000.
• Determining how many iterations it takes for the syracuse sequence to reach 1, starting from an arbitrary seed.
• Given a sequence of daily stock market prices, determine when to buy and when to sell (buy before selling) to maximize profit.
• Print out all Dyck words of a given length.

SC++L : Standard C++ Library

Share on other sites
arithma    226
"Int. to Algorithms" would be too harsh on them at this point because they just got into arrays. The course is mainly C in C++ disguise. (They think the difference between C++ and C is just printf and cout at this point).

Anyway, I got this Schaum book and it has some neat exercises (some about Monte Carlo's Method which caught my attention).

I think it'll be all arithmetic exercises and a bunch of sort and search. I have to get working on it I guess.. Thanks for the replies

Share on other sites
MylesFox    122
In this book there are a lot of simple C++ examples and sample code :

http://www.amazon.com/Sams-Teach-Yourself-C++-Days/dp/0672327112

I found it to be an excellent starter book!

Share on other sites
Replicon    306
You can find easy ACM questions for them to do here. Many of them are trivial, and you can use them to think of good exercises.

Share on other sites
arithma    226
Quote:
 Original post by RepliconYou can find easy ACM questions for them to do here. Many of them are trivial, and you can use them to think of good exercises.

Thanks a big time! Exactly the kind of thing am searching for!

Share on other sites
BeanDog    1065
Here's a similar list of problems:

TopCoder problems

Share on other sites
NotAYakk    876
Remember to try them out before tossing them at the sharks, I mean innocent students.

Ideally find a shark and feed it to'm, and see if it is a tasty problem or not.

Share on other sites
CzarKirk    100
Make a program that works out if X is a prime number.

Share on other sites
arithma    226
Quote:
 Original post by CzarKirkMake a program that works out if X is a prime number.

Done that. Also made them do prime factorization of a certain number. Explained why only prime numbers would appear even though we tried dividing over the larger numbers (like 12 would become {2 2 3}).

1 1 1 11 2 2 21 2 3 31 2 3 4

Nobody was able to do it. They felt stupid when they knew the solution. I felt bad when they couldn't solve it.

Made them draw rectangles (asterisk art) of certain width, height and border width.

That was all what I gave in the last session (which was 2 hours instead of 3 because I had a test afterwards).

They are JUST going into arrays and they know nothing about pointers yet. In the board session I explained the difference between reference passing and value passing and gave them the ever known swap example.

Next steps are to sort and reverse an array.
I hoped I would give them a taste of graphics but wasn't sure how to do that yet.

Share on other sites
Rand    193
If you want something graphical, the how about introducing them to interpolation. A 2D position, (x,y) just 2 float variables. Start with a random position. Home in on a target position. The trivial way would be
if (x<targetx) x+=0.1;else if (x>targetx) x-=0.1;

But another way
x = x*0.9 + targetx*0.1;

Jazz it up and call it AI :)

Share on other sites
CzarKirk    100
Make a program to play Chess. That's hard - I should know. I tried and failed! But learnt so much about C++.

Share on other sites
Zahlman    1682
Quote:
 Original post by arithmaThey are JUST going into arrays and they know nothing about pointers yet. In the board session I explained the difference between reference passing and value passing and gave them the ever known swap example.

My strong recommendation is:

Having shown arrays, show std::vector and explain the cool extra things it can do.
Talk about how reference passing could be implemented. This gives you an opportunity to explain the concept of memory addresses, from which you can derive the concept of pointers.
Then, have them implement a rudimentary replacement for std::vector. Don't bother with templates yet (you can even give them a typedef for std::vector<int> and wave your hands over all of that, for the time being). Then describe the things that std::vector does that their versions don't (this also gives you a hook for a basic discussion of templates, if you decided not to mention them before).

Quote:
 Next steps are to sort and reverse an array.

I don't like to teach students to do simple things that are covered by the standard library... these are interesting problems, but my recommendation is not to have them write code for them - instead, discuss possible algorithms and pseudo them out, then show them std::sort (and std::list::sort) and std::reverse. It is worthwhile to discuss the meaning of an algorithm operating "in place", though.

If you were hoping to get an introduction to recursion via quicksort or mergesort, might I instead suggest giving them the Towers of Hanoi problem?

Quote:
 I hoped I would give them a taste of graphics but wasn't sure how to do that yet.

conio.h. Collaboratively make a small "game" (it could be just a scrolling map demo, to start) where you have some array of map data, and you display a console-window-sized "viewport" of it at once. Use kbhit() or getch() or whatever to pick up the IJKM keys and use them to scroll the viewport. Lots of interesting debugging lessons are possible here, BTW.