Due Friday, Feb. 17, before 5pm.
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.
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.
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) = 80072Zero 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) = 534Bad last argument:
polyeval -222 5678 N=3 Bad command line.Again, bad last argument:
polyeval -222 5678 x 3 Bad command line.
main().
Define the preprocessor macro:
#define ARRAYSIZE 5
and use this constant value for the array's size declaration.
int getXValue(char *argument, int *x)
"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[])
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)
int evaluate(int coefficients[], int degree, int x)
x.
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.
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.
% cc polyeval.con 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.
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.
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
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
cc.
a.out, then
the Unix command to execute this program will be
a.out
./a.out
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
atoi() function.
Reading and using manual pages is a skill that you will want
to acquire as a programmer.
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:
The following files where handed in successfully: polyeval.cIf the file
polyeval.c is not listed,
then something has gone wrong, and you have not yet turned
in the program.
ls -l /p/course/cs354-common/public/spring12.handin/LOGIN/a1
Substitute the the portion of the path LOGIN
with your own CS login.
What gets output will include the files in that directory,
along with the date/time that you last modified them.