CSCI 1120 (Low-Level Computing), Spring 2011:
Homework 4

Credit:
10 points.

Reading

Be sure you have read the assigned readings for classes through 10/31.

Programming Problems

Do the following programming problems. You will end up with at least one code file per problem. Submit your program source (and any other needed files) by sending mail to bmassing@cs.trinity.edu, with each file as an attachment. Please use a subject line that mentions the course number and the assignment (e.g., ``csci 1120 homework 4''). You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's Linux machines, so you should probably make sure they work in that environment before turning them in.

  1. (5 points) Write a C program that takes one or more command-line arguments meant to represent numbers (not necessarily integers) and computes and prints their average. It should print error messages about any arguments that are not numeric but should then go ahead and compute the average of those that are. For example, called with arguments 1 and 0.2, it should print their average (0.6), and called with arguments 1, ``hello world'', and 0.2, it should print an error message about ``hello world'' but also print 0.6. It should do something sensible if none of the arguments are numeric. Library function strtod may be helpful.

  2. (5 points) Write a C program that accepts one line of text input (from standard input) and prints whether it is a palindrome. The function should include a function to get a line of input -- i.e., read characters from standard input until it either finds a newline character ('\n') or runs out of space (in which case it should print an error message). A text string is a palindrome if it ``reads the same backward and forward'' -- ignoring case and skipping non-letters. So, the following are all palindromes: You can probably write a one-line expression in Python or Scala that determines whether a text string is a palindrome according to this definition, using the language's string-processing facilities. You can probably translate that into C, but it might be just as easy, and perhaps more idiomatic, to just operate on the characters in the string directly. Library functions isalpha and tolower or toupper may be helpful.



Berna Massingill
2011-11-01