Program — Introduction
This programming assignment introduces you to C: variables and computation, functions and parameters, and control structures. (You are allowed to use arrays as well if you want. However, they are not required, nor are they necessary to solve the problem)
The C compiler provided on the EECS instructional computer accounts is named gcc. It comes with a debugger gdb, which not only is a great debugger but has an extensive online help facility. It supports ANSI Standard C as described in House and the second edition of Kernighan and Ritchie. Use gcc to compile the program piglatin.c for this assignment as follows:gcc -g -Wall -o piglatin piglatin.c
(On the EECS instructional computers, the gcc command is probably set up to supply the "-g" and "-Wall" information automatically. To make sure of this, typealias
to the UNIX shell and look for an entry for gcc.)
If the program contains no syntax errors, this command will produce an executable file named piglatin. To run the program under control of the debugger, type the commandgdb piglatin
Running your program in this way is useful to simplify the task of tracking down your execution errors; for example, if your program crashes, gdb will tell you the line number where the problem occurred. You'll learn more about gdb in the next programming assignment.
House, chapters 1 through 8 except material in chapters 3, 5, and 7 on arrays.
Kernighan and Ritchie, chapters 1 through 4 except sections 1.9, 1.10, 3.8, 4.3, 4.5, 4.6, 4.7, 4.10, 4.11.2, and 4.11.3. You will not be tested on material on arrays in chapters 2 through 4.
The section "Before You Begin ..." available online in the self-paced course web site (for students who are not familiar with EECS computing facilities).
The section "C Style Examples" later in this document.
Fundamentals of C
CS 9C programming assignment 1
Pig Latin is a language parents speak to each other when they don't want their children to understand them. Each English word has a Pig Latin translation; you produce it as follows.
- If the English word begins with a vowel ("a", "e", "i", "o", "u"), add "yay"to the end of the word to produce the Pig Latin translation.
- If the English word begins with a consonant (a non-vowel), move all leading consonants to the end of the word and add "ay" to produce the Pig Latin translation. You should treat the letter `y' as a vowel when it occurs after the first character in a word. You should also treat the letter pair "qu" together as a consonant. Here are some examples.
English Pig Latin yes esyay example exampleyay bygone ygonebay test esttay quiz izquay strip ipstray trips ipstray schnitzel itzelschnay rhyme ymerhay
Write a program in C to read a word from standard input and print its Pig Latin translation on a line by itself on standard output. You may assume that an English word starts with no more than four consonants.
Guidelines for indenting, commenting, etc. are indicated in the section "C Style Examples". Follow them.
C code is rather dense. Thus your functions should be small, certainly at most a screenful of code.
Your program need not use arrays. You may assume that an English word starts with no more than four consonants. (This assumption makes it possible to avoid arrays.) You may also assume that each "q" is followed by a "u". Test your program at least on the examples given above.
The line #include <stdio.h> at the start of your program will allow you to use the predefined constant EOF and the getchar and putchar routines for input and output.
There is a program called unpig in the ~cs9c/lib directory on your class account. After reading a word, unpig prints all possible English words (and a few non-English "words") that have that word as a Pig Latin translation. You may use unpig to check your work by piping your output into it. (unpig doesn't quite work correctly for words starting with "qu", however.)
Generating a transcript of your program execution
For this and all remaining assignments, you are required to bring a transcript of your interaction with your program to the Self-Paced Center for grading. One way to create such a transcript on UNIX is to use the script program, which saves everything you do on the terminal in a file, called typescript, until you type the command exit. After you are done, you can get a paper printout of this file by using the lpr command. Here is an example of using the script and lpr commands for this assignment:% script Script started, file is typescript % cat prog1.c% gcc prog1.c % a.out yes esyay % a.out | unpig strip ripst trips strip pstri % exit % Script done, file is typescript
Everything that has appeared on the terminal between the script command and the exit command has been saved in the file called typescript. You can now type lp typescript to get a paper printout to bring in to the Self-Paced Center after you have done the rest of the assignment.
Do not type another script command without first typing exit.
Do not use an editor when you are using script. While this does no harm, it does put bizarre characters into the typescript file that cannot be displayed properly. If you do this your printout will end up being very garbled.
C gives you plenty of rope to hang yourself. To save your neck, you should spend time decomposing the problem into manageable pieces (i.e. separate functions!) before trying to code anything, and then test each piece separately. You may test your functions individually by writing a tiny "driver" main ( ) that simply gets test data and calls the function.
Some aspects of C are sure to trip up the novice C programmer, for instance, the placement of semicolons and parentheses, the need to "break" out of a case in a switch statement, and the indexing of arrays. The compiler and run-time system are sometimes of little help in finding these errors. Advice: Start with something simple, get it working, and add small pieces to it one at a time. Tutors will ask you, when you bring them bugs, "What was the last thing you did that worked?" You should have a good answer.
Arrays are not necessary for this problem. (Why? If you need a hint, consider a program that simply echoes characters from the standard input to standard output. How many variables does that program need?)
A satisfactory grade on this assignment requires the following:
- Correctly working program, tested as suggested
- Short functions
- Good style and layout
- Reasonable names for variables, functions, etc.
- Loops and blocks indented according to the given guidelines
- Informative comments accompanying every function