Assignment 1

CS/ECE 354, Spring 2012


Send e-mail questions to Sean Bauer, sbauer@cs.wisc.edu
Please begin Subject with [CS354] for filtering purposes.


Due Friday, Feb. 17, before 5pm.


Collaboration Policy

For this assignment, you may work in pairs (2 people). All students (whether working in a pair or not) must individually turn in the assignment by individually running the handin program. Therefore, 2 copies of the assignment will be turned in for those working as a pair. The grader will choose to grade only one copy of a pair's work, and assign the same score to both students.

If working in a pair, the names and sections of BOTH students must appear at the top of the turned in assignment.


Program

The purpose of this program is to write a C program, gaining the experience of working in a non-object oriented language. Both arrays and command-line arguments are utilized.

Program Statement

Write a C program that uses command-line arguments to specify the coefficients of a polynomial, as well as a value at which to evaluate the polynomial's value. The program prints the polynomial (in a simplified format), evaluates the polynomial at the given value, and then prints this result.

We are limiting the degree of the polynomial, in order to simplify future implementations of this program (in assembly language). The coefficients are limited to integer values. The program will represent the polynomial by storing the coefficients in an array of integers.

Evaluating the polynomial for a single value occurs after all the coefficient values are in the array, and therefore, after it has been determined that all input values are good.

Here are several examples of program execution, where the command line (user's input) is given in boldface. These examples presume that the executable is called polyeval. Read these examples carefully, as they define what the program must do for edge cases, as well as for error cases.

polyeval  1 -6   80000  x=12
Polynomial entered:
1x^2 + -6x^1 + 80000x^0
f(12) = 80072


Leading zeros are acceptable:
polyeval  000001 -06   80000  x=12
Polynomial entered:
1x^2 + -6x^1 + 80000x^0
f(12) = 80072
Zero coefficients are acceptable:
polyeval  1 0   80000  x=12
Polynomial entered:
1x^2 + 0x^1 + 80000x^0
f(12) = 80144

The following examples illustrate what the program does with unusual or unexpected input, as well as bad command lines. The italics header over each describes the difficulty within the command line.

Not enough command line arguments:
polyeval
Not enough coefficients.  Bad command line.
Again, not enough command line arguments:
polyeval 178
Not enough coefficients.  Bad command line.
More than 5 coefficients:
polyeval  3  200 80 -1 20345678  70001 x=4
Too many coefficients.  Bad command line.
Bad integer on command line. We treat it as if it is a 0:
polyeval 178 H x=3
Polynomial entered:
178x^1 + 0x^0
f(3) = 534
Bad last argument:
polyeval -222 5678    N=3
Bad command line.
Again, bad last argument:
polyeval -222 5678    x 3
Bad command line.

Organization and Requirements

  1. The array is to hold a maximum of 5 integer coefficients. Declare this array within main(). Define the preprocessor macro:
    
    #define ARRAYSIZE 5
    
    
    and use this constant value for the array's size declaration.
  2. Organize your code such that it defines and uses the following functions. Other functions are permitted; this requirement defines a minimum set of functions. A future 354 assignment will re-implement much of this program, with this organization, in assembly language.

    int getXValue(char *argument, int *x)
    This function returns the value -1 if the last command line argument (passed as the first parameter to this function) is bad; it returns 0 otherwise. The first parameter is a pointer to a character, assumed to be a C string, and must be of the form "x=" directly followed by 1 or more characters that are taken to represent an integer value. Therefore, this function must check that the first character of the string is an 'x', and the second character is an '='. If those are valid, and there actually are further characters in the string, the function calls atoi() to turn those further characters into an integer representation. That integer is placed at the address given by the second parameter. Note that if the "x=" portion of the argument is valid, then this function uses whatever atoi() returns as the integer implied, without consideration of if the further characters could imply an integer representation.

    int storeCoef(char *cmdline[], int numcoef, int coefficients[])
    This function uses the portion of the command line arguments passed to it and extracts the integer coefficients, placing each into an array. It returns the degree of the polynomial; use a return value of -1 to indicate that this function encountered an unrecoverable error in the input, so could not complete the task of placing the coefficients into the array. It receives as its first parameter a pointer within argv, specifically the address given by argv[1] (which is a pointer to the string of the first coefficient listed within the command line arguments). The second parameter is the integer number of coefficients that the function is to parse. For each coefficient, this function turns the string representation into an integer, and places the integer into the array passed as the third parameter.

    void printPoly(int coefficients[], int degree)
    This function prints the polynomial represented by the array of coefficients and the polynomial's degree passed as parameters. Use the sample output given to guide the formatting of the polynomial.

    int evaluate(int coefficients[], int degree, int x)
    This function uses the array of polynomial coefficients, the degree of the polynomial, and the value of x (all passed as parameters) to evaluate and return the value of the polynomial at x.
  3. Do not worry about, and do not implement any code that deals with cases of integer overflow. We will test your program on integers that fit into a 32-bit, two's complement representation.
  4. Your program must follow style guidelines as given in Style Guidelines.

    Also include a comment at the top of the source code with your name and section (and your partner's name and section, if working in a pair).

    See Guidelines for Programs to see an indication of point allocation for program grading.

  5. Name your source code program polyeval.c, and place the entire program into this file. This single file is what will be turned in. It must have exactly this name to make the lab-supported handin program work.
  6. Your program is to operate the same as the examples given above. Use the same formatting, strings, and messages as given in the samples.
  7. Use the mumble Linux machines for this assignment! A penalty will be imposed for any program that was obviously edited on a Windows machine and transferred to the Unix machines for turning in. We are annoyed when we see Windows line endings (^M) at the end of every line, so we take off points. In addition, our simulator (when we write assembly language programs) complains about those Windows line endings, so working (editing) on the Linux machines will be needed for the rest of the semester.
  8. We will compile your program with
      % cc polyeval.c
    
    on a mumble Linux machine. So, your program must compile there, and without warnings or errors. According to the CSL facilities web page, these machines are located in 1350 CS.

About Copying Code and Academic Misconduct

For almost any C program that does something useful, someone has already written this program and further, has posted it for others to use. This program does not do much that is useful, and is not likely posted anywhere. Still, it is academic misconduct for you to copy or use some or all of a program that has been written by someone else.

The penalty for academic misconduct on this assignment (and all CS/ECE 354 assignments) will be a failing grade in the course. This penalty is significantly more harsh than if you simply do not do the assignment. You will gain much more by doing the assignment than by copying, possibly modifying, and turning in someone else's effort.

Notes

  1. To compile a C program, use the C compiler. Your program is expected to compile without warnings or errors using cc on the mumble instructional Linux machines. If the name of the C source code program were myprogram.c, then the Unix command
    cc myprogram.c
    will compile this program, placing the executable program in the file a.out. Note that this command (given in this manner) must be executed while your current working directory (often abbreviated cwd) is the one containing the C source code file. To place the executable in a file other than a.out, add another option and argument to the command line:
    cc myprogram.c -o executablename

    (Be forewarned: If you use another C compiler, for example on your own computer, and its implementation allows code that causes warnings or errors when compiled as expected, you will lose points.)

    And, if you know lots about compilers, please do not set any flags or other options when compiling. We need to see no warnings or errors when we compile with cc.
  2. To run a program, the name of the executable becomes the command. If your executable is named a.out, then the Unix command to execute this program will be
    a.out
    or
    ./a.out
  3. This program requires the use of command-line arguments. Look at Kernighan and Ritchie (2nd edition) section 5.10 for an excellent explanation of how they work. The On Your Own topic also contains an explanation.
  4. You will want to look up and use the atoi() function to convert the "integer" command line arguments from strings to integers. We've designed and organized this program such that the return value from atoi() is useful. From one of the instructional Linux machines, the command
    man atoi
    will bring up a manual page that describes the atoi() function. Reading and using manual pages is a skill that you will want to acquire as a programmer.
  5. The shell (which processes Unix commands) assigns functionality to some characters, and handles them differently. Therefore, these characters might not be given to the executing C program as you might expect (depending on which shell the user is using), and thus can have unexpected values. Your program is only expected to correctly handle any alphanumeric character on the command line, to define away any of the problems these characters might cause.

Handing In the Program

We are using the lab-supported handin program, to turn in programs online.

Your C source code must be in a file named polyeval.c. Turn in your program by running the lab-supported handin program. As the current version of the handin program has a bug in it, we have found a workaround. Please make sure that you include which section you are enrolled in, within your polyeval.c file in the comment that contains your name. Instead of following the lab's directions, use this exact command:


  /s/handin/bin/handin -c cs354-1 -a a1 -d . 

Note that this is to be done while your current working directory is the one containing the polyeval.c file. This program copies the specifically named file to a directory accessible by the instructor and TAs.

Some advice, as this is the first assignment of the semester: