CS 332: Operating Systems

Getting started in C

Due: 11:59PM Monday, April 5.

Some Getting-Started Programs

Once you have learned a couple programming languages, getting started in a new language is mostly a matter of finding good reference materials, getting a source of sample programs, and writing a bunch of small programs to get the syntax and core libraries under control.

When I want to learn a new programming language, I start by writing a few small programs to make sure I can handle the basics. Here's a collection of programs that make a nice introduction to most programming languages.

  1. (Output.) Write the "hello, world" program to print a simple text message to standard output.
  2. (Input.) Write a program that asks the user for his/her name, age in years, and hourly wage as a decimal number, and prints the information back.
  3. (A function.) Write a recursive factorial function as an example of a function that takes an integer parameter and returns an integer. Write a short main program to test the function.
  4. (Arithmetic and conditionals.) Write a change-making program. Here's a description of such a program as assigned to a recent CS 111 class.
  5. (Input/output parameters.) Write a function that takes two parameters and swaps them. Whether this is possible depends on whether your programming language supports pass-by-reference in some form.
  6. (File input, loops, and command-line arguments.) Count the lines in a text file whose name is specified as a command-line argument.
  7. (Lists/arrays.) Given a text file consisting of one name on each line, read in the list of names, sort them into alphabetical order, and print the sorted list. Let the user specify the text file as a command-line argument.
  8. (String manipulation and searching.) Count the number of times each word in a text file appears. Print the results sorted in decreasing order by word count. Let the user specify the text file as a command-line argument. (If your language has a built-in hash table, like Python's dictionaries or Perl's hashes, use that. Otherwise, a linear search ought to do the job, though more slowly.)
  9. (Classes.) Create a class called Circle with instance variables to keep track of the center and radius of a circle. The class should have a suitable constructor (or constructors), plus methods getArea, getCircumference, and a collection of appropriate accessors. Your program should read a list of circles from a text file whose lines consist of three numbers separated by spaces (e.g. "3.2 4 2.7" represents the circle of radius 2.7 centered at (3.2, 4)), instantiating Circle objects for each one. Once you have a list of Circle objects, run through the list reporting the center, radius, area, and circumference of each circle. Let the user specify the file of circle data as a command-line argument.
  10. (Pointers, references, and memory allocation.) Read a list of integers from a file into a linked list of your own construction. Sort the linked list (insertion sort and merge sort both work well with linked lists) and print the sorted list. Let the user specify the file as a command-line argument.

After getting these basics under control, I start exploring the language's standard libraries. I will always want to know more about string manipulation, the file system (create/delete/move files, traverse a directory tree, etc.), simple GUIs and line graphics, invoking other programs, networking, etc. But this is a longer-term project. If I have a good pet project to work on, most of these libraries come up naturally.

Your assignment

Try writing the programs above using either C or C++. Hand in three: the factorial function, the word-counter, and the linked list sorter.

Don't forget that good style and organization are valuable in any program. Many of these programs can be written entirely in main(), but even so, they would probably benefit from a well-chosen function or two.

Reference materials

Sample programs

We'll develop a few examples in class, but the Internet is full of samples as well. Searching for "C examples", "C++ samples", "C example programs", etc. leads you to lots of stuff. Mostly, these pages are intertwined with a particular development system (e.g. Microsoft's Visual C++) or are filled with pretty bad code. Still, you can learn a lot about the language by trying some of the samples you find on-line.

Here's a small collection of C++ samples that I found this morning. It's on the pretty-good side of the pages I find when searching for samples. It's not great C++, but it illustrates a useful variety of techniques. As always with stuff you find on-line, you have to evaluate quality and reliability yourself. Fortunately with sample programs, your compiler can help you with that.

If there are particular samples you would like me to write, let me know.