tags:

views:

1795

answers:

29

I am teaching myself C, with several books as well as online tutorials. I am in the middle of Greg Perry's book Absolute Beginner's Guide to C. I know I learn best by doing, so I need to start writing my own code. My problem is that I am a little stymied by not being able to come up with ideas for programs that will exercise my skills and not be beyond them. Any suggestions would be welcome.

Update: I have entered a Code-golf, written a small number guessing game, put a couple of great books on my wishlist, and explored some great sites that will challenge me for probably years to come. Thanks all!

Update: Finished my first Project Euler problem.

+42  A: 

http://projecteuler.net/

Good for any language. (Bring your own math library)

Min
This could keep me very busy.
Laura
+1 For Project Euler. This is always where I start when I want to learn a new language.
Joey
Make your own math library - even simply creating your own BigInteger class will give some useful experience. +1 for Project Euler.
Callum Rogers
I am going to use that site - thanks!
Laura
A: 

Implement a doubly-linked list using a single pointer.

jheddings
Haven't gotten to pointers yet - I think that's a couple chapters away, so I'll read fast and try it tonight.
Laura
I don't think you'll get it by night. It's quite difficult for the first time...
f0b0s
What's being suggested is an "XOR" linked list. It's an interesting mental exercise to impress the idea that pointers are really just numbers, and you can do math on them (including XOR). You should understand why it works, but it's something you should never actually do in real code unless you've got a *really* good reason. And probably not even then.
Casey Barker
Good points... But the question was around "coding exercises," and I think this came up in almost every C class I took.
jheddings
+10  A: 

Take Kernigan & Ritchie book. There are a lot of really good exercises.

f0b0s
+9  A: 

Silly simple example, with little to no practicle use:

Write an application that will accept a line of text input, and output the words in the reversed order.

Example:

Input - "The world is a strange place!"

Output - "place! strange a is world The"

  • Use only the memory needed to store the original string (no temp variables, etc.)
Justin Niessner
This is actually a very good problem!
Jonas
Well, the first is just string tokenization, but the bonus part does make it a little trickier.
quillbreaker
Slight variation of this, accept a line of text input and check if the input is a palindrome (the same when reversed). Kinda fun when you add the "no temp variables" restriction!
Will Boyce
+3  A: 

You can write a simple "game" using C - such as blackjack or poker - don't worry about AI just write an engine that would enable several players to play the game on the same computer.

Another good "start project" is a simple calculator - write a calculator that receives a string and returns a result. Assume you'll always get a string of number op number (.i.e. "2 + 2") and build from there.

Dror Helper
I made the "Guess the Number" game today. Will start working on pointers tomorrow.
Laura
+1  A: 

A Hello World program :)

A program that prints the factorial of a given number

A program that takes in input a list, creates a copy of a list and reverses the original list.

An implementation of a Bubblesort (Horrible algorithm, but good for practice) http://en.wikipedia.org/wiki/Bubble%5Fsort

ManicMailman
+16  A: 

Click this: code-golf

Enter every challenge that has at least 10 upvotes even if you are weeks or months late, and even if your solution isn't so great. (You don't have to actually post it unless you want to, but get each one working...)

We will be watching.


Update: This question is in the most-viewed and highest-voted lists. This answer is in the most-commented list. Laura has already entered her first challenge, so at least it is leading to practice.

Code-golf on stackoverflow may be a bit different than elsewhere

The c-g scene is a bit deeper than it sounds, especially as it relates to SO.

Yes, there are some "true Perl c-g" entries. But most entries are not Perl. I enter in Ruby usually; most people enter in the language they are currently most interested in, and the objective is not as simple as just beating Perl. (Though I do keep trying.)

C-g has zillions of benefits; the character-crunching thing is an entirely optional part of the activity.

In fact, often most of the entries are uncompressed, and they are in Java and C# and other languages that have no chance of winning. So why is that? It's because c-g is more than a golf-scoring contest. We get at least as many entries from people who are learning or showing off their favorite language than we do from people who are writing crunched Perl. And that's great.

Obviously, no educational opportunity will be perfect. One is unlikely to be building binary trees or hash tables, but it does seem like C-G on SO is worth it for many people.

DigitalRoss
I found the oldest post - Fizz buzz - and am compiling now...
Laura
Excellent. This is exactly how I learned Ruby. I have written in another question about why I think code-golf is so helpful: http://stackoverflow.com/questions/1652800/does-codegolf-make-a-better-programmer-or-is-it-even-useful/1652943#1652943
DigitalRoss
I feel this is a terrible way to learn to program. It pretty much throws all correct software engineering practices and conventions out the window.
Marcin
@Marcin: agreed. The 'challenge' here is to code in the fewest characters, not in the most elegant algorithm. The problems themselves are rarely challenging to solve; the main challenge is ripping our as many characters as possible. Fun for old heads, but not a great way to learn.
Kirk Broadhurst
Okay, I posted in the Fizzbuzz thread my answer. Yay! I like "hands-on" learning. NEXT!
Laura
Code golf is poor for someone "teaching themselves C". The ideas themselves may be useful for an assignment, but only if you remove completely the focus on writing something with the fewest lines of code.
Ash
-1: Code Golf is a great exercise for experienced developers, but not for beginners.
280Z28
Could you possibly drop that seemingly misinformed attitude about people who haven't "entered a single SO code golf challenge" - just because they haven't participated in a StackOverflow one, doesn't mean they haven't done code golf before, and don't know what it's about. You're being confronted with valid and reasonable criticism - code golf might well teach someone to solve a problem, but it won't necessarily teach them any good, general way to solve larger problems in a target language. If you'd like to contribute further to the discussion, please do so without the logical fallacies.
Rob
hehehe, code golf :D
LiraNuna
A: 

All good C books I read (and most bad ones) have exercises, usually at the end of every chapter/section. I'm not familiar with your book, but it also probably has exercises.

MAK
There are many examples, but not exercises. :( However, I never buy just one book on a topic, so I will also start C in 21 days and check to see if they have exercises.
Laura
Try the online judges line Uva, SPOJ, and ProjectEuler also. They are a bit tougher for an absolute beginner so I held back from suggesting them.
MAK
+2  A: 

Go in this sequence

  1. Given an integer, determine whether it is a prime number or not.
  2. Given an integer n , generate a Fibonacci series upto n
  3. Then do some array practice- sorting algorithms-bubble.sequential sorts etc.
Ravi
Learned about sequential sorting today, will try it out.
Laura
Did you try bubble sort?Thats the first sorting method that one should start with.
Ravi
A: 

Read in matrix data from files specified on the command line. Perform some operation (for example, multiplication, transpose, inverse). Write data out to a file.

This will give you practice with file I/O, dynamic memory allocation, looping over arrays, and optimization.

Jay Conrod
+1  A: 

I'm a visual learner so when I started out I was fascinated with animation.

Along those lines, I have 3 progressively difficult exercises:

1.) Create a bouncing ball animation that bounces off the edges of the screen.

2.) Create 100 bouncing balls that all do the same thing but in different directions. You'll have to leverage arrays to do this.

3.) Create an n-body simulation with these 100 balls where every ball is attracted to every other ball by gravity. You'll again have to use an array but with a nested loop to handle the gravity. (I have a few tutorials on the subject if you want to see my implementation.)

Steve Wortham
+4  A: 
Frank V
It's now on my amazon wish list. thx
Laura
I hope you enjoy it. :-)
Frank V
+1  A: 

A program to find prime numbers from 1 to x where the user fills in the X?

You could also give the user the option to write the results to the disk instead of the standard out.

Once the basic program is working, you could then add threading to make it work faster....

Frank V
+18  A: 

Write a text adventure.

printf("You are standing in front of a building.\n");

EDIT: Not sure why the hate - did you think I was being facetious? That line was just to get him started, not meant to be the whole thing. If you think it's a bad idea, all right, but this is what I did to get started programming.

Grumdrig
This is actually a good project!
pmg
Thanks, @pmg. I agree.
Grumdrig
+1, any smallish project is good experience. A simple game is a particularly good idea.
MAK
Writing a text adventure game was my first 'larger' C project when I was learning.
Präriewolf
You are standing in an open field west of a white house, with a boarded front door. There is a small mailbox here. http://en.wikipedia.org/wiki/Zork_I
KM
Thank you @KM! That's what I was trying to invoke.
Grumdrig
+2  A: 

Try implementing your own printf() in terms of putchar() and/or fputs(stdout). This will require you to do some simple string parsing, and will involve some nested loops, quite a lot of conditionals, and some pointer arithmetic (which you'll need to learn to be proficient in C anyway). You can start with handling %s, %u, %d and %f, and then gradually add support for more specifiers.

Pavel Minaev
+2  A: 

Make a program that can multiply arbitralily long (well ... 10 thousand digits) numbers.

I think you'll need

  • data structures
  • malloc (and friends) for memory management
  • math
  • possibly string management

Remember to make several source files, separated by subject, so that you can reuse them for other projects.

pmg
+3  A: 

So you want to drill seriously huh? click here

JPCF
Whoa. I don't think I'm ready for that. But maybe someday.
Laura
I used to think that I was unable to program at that level... Henry Ford said that attitudes are more important than aptitudes...
JPCF
+32  A: 
int a;
a = 1; // an assignment
Orion Edwards
Well played, Mr. Edwards. Well played.
Iceman
Damn, *I* wanted to write that.
Konrad Rudolph
+1 for clean answer.
Andrejs Cainikovs
tooooooing!! 8-S
OscarRyz
A: 
  1. Write a program which will calculate and list the moves necessary to solve the Tower of Hanoi problem for three discs and three rods.
  2. Modify the program to solve the problem with four discs.
  3. Modify the program to solve the problem for a number of disks specified on the command line.
Tim
+4  A: 

http://www.topcoder.com/tc?module=ProblemArchive

see me no more
Probably the best database of programming questions. You can even check your solution...
Marcin
A: 

Implement parts of the standard library yourself. strcpy is a good one for getting your head around pointers and strings. Other functions vary in their complexity and focus.

Obligatory disclaimer: Don't use your own implementations -- just test them. If you actually need to do a strcpy (or anything else that's in the standard library), use the standard library.

Steve S
A: 

Try doing "Object Oriented" design in C: If you look at the FILE pointer stuff in the standard library, you'll find indications on how to dynamically call a "method" on an "object". Try implementing your own little set of "objects" and get the feeling of this mechanism.

S.C. Madsen
+1  A: 

Once you are fairly comfortable with the basics of C, develop a basic text editor. It will give you a chance to implement pretty much all the basic features in c , along with pointers.

BM
A: 

I would check out the problems here:Bring You Own Code I regularly visit this site and learn about what NOT to do as a programmer. Recently they started adding these little challanges with responses posted in the comments. You can compare you results with others in you desired language and really learn a lot.

kramthegram
+2  A: 

Write a text version of "Don't Press the Red Button". That's how I started my son on Python...

Arkadiy
I must be missing something...is there an explanation of some sort or do I just not get it?
Laura
Did you play the game? This can start as simply an infinite loop of "Don,t press <Enter>", then go into arrays, and finally file IO. Then may be some text-based screen painting using ncurses. And finally graphics.
Arkadiy
I haven't laughed like that in years... Thanks for the link!
Liran Orevi
What game? There's a big red button with Don't Press the Red Button over it and that is all. Wait...I'm supposed to press the button, right?
Laura
A: 

Create a support schedule: given number of days to cover and number of people, create a fair schedule to cover the days. Introduce complications like no early and late support on same day, no late support more than 2 days in a row and so on.

Arkadiy
A: 

Write a tail program command for Windows and make it accept -f option.

:)

Simple enough and very useful.

You may find the source code very easily but trying to figure out how to do it your self would be valuable

OscarRyz
A: 

if you read C for dummies then you can get more assignment

A: 

Programming Praxis is another good site to practice programming. I find it great for learning another language and some of the problems are fitting for just starting out.

dbmikus