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.
The purpose of this program is to write a C program, gaining the experience of working in a non-object oriented language.
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 = 1024Notice that this case replaces the user input with the maximum upper bound implemented within the program.
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.
void setPowers(int userinteger, int ar[], int lower, int upper)
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);
baseexponent.
void print(int ar[], int userinteger, int lower, int upper)
lower and upper, the function
prints one line of the output.
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.
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.
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 our assigned 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
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.
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>
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:
The following files where handed in successfully: powers.cIf the file
powers.c is not listed,
then something has gone wrong, and you have not yet turned
in the program.