|
CS/ECE 354 Machine Organization and Programming
Fall 2008 Section 1
Instructor
Mark D. Hill
TAs
Mark Liu &
Somayeh Sardashti
|
Homework 1 // Due at Friday, September 26, before 5pm
Send e-mail questions to Mark Liu, mliu@cs.wisc.edu
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 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.
Program Statement
Write a C program that defines a range of integer values with command line arguments. There are two command line arguments that are both single digit integers that make up a range. The program determines whether or not each value in your range is coprime with each of the values in the range 2-9. These values are then printed out in the format of the examples shown below.
Two numbers are coprime (a.k.a. relatively prime) if and only if they have no common factors besides 1. Equivilently, their greatest common divisor is 1. For example, 4 and 9 are coprime but 4 and 6 are not because they share the divisor 2.
Command line arguments are used to tell the program
lower and upper bounds to specify the range of integers
to include.
Here are several examples of program execution,
where the command line and user's input is given in boldface.
These examples presume that the executable is called coprime.
Read these examples carefully, as they define what the program
must do for edge cases, as well as for error cases.
% coprime 3 6
2 3 4 5 6 7 8 9
3 Y N Y Y N Y Y N
4 N Y N Y N Y N Y
5 Y Y Y N Y Y Y Y
6 N N N Y N Y N N
The following examples illustrate what the program does
with unusual, unexpected, and bad command lines.
The italics header over each describes the difficulty within the command line.
Too few command line arguments:
% coprime
Invalid command line. Both lower and upper bounds must be
greater than 1 and less than 10. Usage:
coprime <lower> <upper>
Argument not an integer or not greater than 1:
% coprime X 5
Invalid command line. Both lower and upper bounds must be
greater than 1 and less than 10. Usage:
coprime <lower> <upper>
% coprime 6 -3
Invalid command line. Both lower and upper bounds must be
greater than 1 and less than 10. Usage:
coprime <lower> <upper>
Lower is not less than or equal to upper:
% coprime 5 4
Notice that this case simply ends the program, neither
calculating nor printing anything.
Organization and Requirements
-
Declare and use an array of 10 characters.
Please declare this array statically.
Define the preprocessor macro:
#define ARRAYSIZE 10
and use this constant value as the array's size in the declaration:
char array[ARRAYSIZE];
This (single, static) array is used (and re-used) to hold the results for each of the integers in the range. These results should be of the form Y or N.
-
Use the C function
atoi() to convert
the command line arguments into an integer representations.
Where this string is not an integer,
use the value 0 (as returned from atoi()).
This utility function is in stdlib.h.
-
Organize your code such that it uses the following functions:
void getCoprimes(char ar[], int firstindex, int lastindex, int numerator)
This function calculates whether or not each pair is coprime and places them into the array.
At ar[firstindex],
it places the result of whether or not numerator and firstindex are coprime.
At ar[firstindex+1],
it places the result of whether or not numerator and (firstindex+1) are coprime, etc.
void printArray(char ar[], int firstindex, int lastindex)
This function prints the results Y/N that are the array elements,
nicely spaced.
The parameter firstindex gives the array index of the first
array element to print,
and lastindex gives the array index of the last
array element to print.
-
Your program must follow style guidelines as given in
See Style Guidelines.
Also include a comment at the top of the source code
with your name (and your partner's name, if working in a pair).
See
Guidelines for Programs
to see an indication of point allocation for program grading.
-
Name your source code program
coprime.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.
-
Organize your code such that the only exit point(s) are in
main().
-
Your program is to operate the same as the examples given above.
Use the same formatting, strings, and messages as given in the samples.
-
Use the Unix machines assigned for this class!
A penalty will be imposed for any program that was obviously editted
on a Windows machine and transferred to the Unix machines
for turning in.
-
Yes, this program could be implemented without an array,
and without the requested functions. However, follow the
instructions, as this assignment is meant to force you to
use an array (and pointers), as well as pass parameters.
There will be a severe score deduction for a solution
that does not follow the directions.
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 anything 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
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
-
To compile a C program, use a C compiler.
cc run on our Centos Linux machines
is expected for this assignment.
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
To run this executable simply type into the command line:
./executablename arg1 arg2
(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.)
-
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 online class notes also contain an explanation.
Essentially,
argc is a count of the number
of white-space delimited strings that were used in the
command.
For this program (when correctly used) there will be 3.
Note that the first argument is the name of the program.
argv is a pointer to an array of pointers to characters (strings),
where each string is one of the white-space delimited arguments.
-
The shell (which processes Unix commands) assigns
functionality to some characters, and handles them differently.
Therefore, the command-line arguments can have unexpected values
(depending on which shell the user is using).
Your program is expected to correctly handle any alphanumeric
character within the command-line arguments.
-
A note to those few students who do not read carefully:
the machine route66 is NOT one of the instructional
machines assigned for use by this class. In fact, it is
a different platform, and the C compiler is linked differently.
Handing In the Program
We will use the lab-supported handin program.
If you wish to modify your path to make the command shorter, follow the instructions on the CSL web page
Electronic Hand In System.
*Do not turn this program in until noon on Wednesday, September 24th*
Your C source code must be in a file named
coprime.c.
Turn in your program by running the lab-supported handin program.
/s/handin/bin/handin -c cs354-1 -a a1 -d .
while your current working directory is
the one containing the coprime.c file.
This program copies the specifically named file to
a directory accessible by the instructor and TAs.
(P.S. If you add the path /s/handin/bin within
your .cshrc.local file, re-login,
then you can omit /s/handin/bin/ from the command.)
|