Assignment 1

CS/ECE 354, Fall 2009


Send e-mail questions to Tony Nowatzki, tjn@cs.wisc.edu
Due Friday, Sept. 25, 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 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 uses command line arguments to define an integer value and a range. The integer value must be greater than 0. The range is specified by 2 integers, both greater than 0, specifying the lower and upper bounds. The range given by the lower and upper bounds must be 10 or less. The program calculates all powers of the integer value specified by the range, and stores these powers in an array. The program then prints all of these powers stored in the array.

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 powers. Read these examples carefully, as they define what the program must do for edge cases, as well as for error cases.

% powers 2 3 6
Powers of 2:
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
% powers 12 1 2
Powers of 12:
12^1 = 12
12^2 = 144

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:
% powers
Invalid command line. Usage: powers <int> <lower> <upper>


% powers 80 3
Invalid command line. Usage: powers <int> <lower> <upper>
Argument not an integer:
% powers X 13 14
Invalid command line. Usage: powers <int> <lower> <upper>
Negative number entered for a command line argument:
% powers -5 20 21
Invalid command line. Usage: powers <int> <lower> <upper>
Lower bound is greater than upper bound:
% powers 3 5 2
Invalid command line. Usage: powers <int> <lower> <upper>

Range specifies more than 10 values:

% powers 2 1 15
Powers of 2:
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512
2^10 = 1024
Notice that this case replaces the user input with the maximum upper bound implemented within the program.

Organization and Requirements

  1. Declare and use an array of 10 integers. Please declare this array statically, and within main(). Define the preprocessor macro:
    
    #define ARRAYSIZE 10
    
    
    and use this constant value as the array's size in the declaration:
    
    int   powers[ARRAYSIZE];
    
    
    This (single, static) array is used to hold the powers that the program computes.

    The program only sets those array elements corresponding to the powers that it will be printing.

  2. 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, as well as pass parameters. There will be a severe score deduction for a solution that does not follow the directions.
  3. Organize your code such that it uses the following functions:

    void setPowers(int userinteger, int ar[], int lower, int upper)
    This function sets the array's values; only the array elements whose values need to be set are set. The remainder are uninitialized. powers[0] is set with the value of userinteger^lower. This function calls function power() to calculate each value. Yes, this is inefficient, and could easily be implemented without this function call, but this implementation forces the use of a function and its return value.

    int power(int base, int exponent);
    This function calculates and returns baseexponent.

    void print(int ar[], int userinteger, int lower, int upper)
    This function prints the program's output. For each array element in the range of integers defined by lower and upper, the function prints one line of the output.

  4. 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.
  5. 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 your partner's name, if working in a pair).

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

  6. Name your source code program powers.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.
  7. Your program is to operate the same as the examples given above. Use the same formatting, strings, and messages as given in the samples.
  8. 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.

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 a C compiler. Your program is expected to compile without warnings or errors using cc on our assigned 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 want 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. Karen's 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 4. 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.
  4. 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.
  5. Use the atoi() function to convert the command line arguments from strings to integers. Please utilize the value returned from atoi(), whatever it is.

    Since atoi() returns the value 0 for our program's error cases, that makes it easy for the program to detect and handle the error. An example that will cause atoi() to return the value 0 would be the second argument from the example given above:

    % powers X 13 14
    Invalid command line. Usage: powers <int> <lower> <upper>
    
  6. 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 are using the lab-supported handin program, to turn in programs online.

Your C source code must be in a file named powers.c. Turn in your program by running the lab-supported handin program. Instead of following the lab's directions, use this exact command


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

while your current working directory is the one containing the powers.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: