CS 270, Spring 2014
Programming Assignment PA1
Introduction to C
Programming due Friday, Jan. 24 at 11:59pm, late deadline Saturday, Jan. 25 at 11:59pm.
This assignment has four objectives:
- to write a C program with console output,
- to learn how to submit your C program using the Checkin tab on the course web site,
- to understand how preliminary testing works, and
- to see if you can follow directions!
Write a C program in a file called PA1.c, using the example of the code structure
shown below. You must write five functions and the main entry point, exactly
as described in the directions below:
- Create a directory for writing programs, we suggest calling it ~/cs270.
- Create a subdirectory for the PA1 assignment, we suggest calling it ~/cs270/PA1.
- Create a file called PA1.c in the PA1 subdirectory, and copy the code shown below.
- Personalize the comment block shown below by changing the author and email.
- Write five functions, each of which accepts a parameter of type double and returns a double.
- The first function is computeCircle, which computes the area of a circle given the radius, using 3.141593 * radius * radius.
- The second function is computeTriangle, which computes the area of an equilateral triangle, using 0.433013 * side * side;
- The third function is computeSquare, which computes the area of a square, using side * side;
- The fourth function is computePentagon, which computes the area of a regular pentagon, using 1.720477 * side * side;
- The fifth function is computeHexagon, which computes the area of a regular hexagon, using 2.598076 * side * side;
- In the main entry point call the five functions in order.
- Convert argv[1] to a double, call computeCircle, and print the area of the circle.
- Convert argv[2] to a double, call computeTriangle, and print the area of the triangle.
- Convert argv[3] to a double, call computeSquare, and print the area of the square.
- Convert argv[4] to a double, call computePentagon, and print the area of the pentagon.
- Convert argv[5] to a double, call computeHexagon, and print the area of the hexagon.
- Follow the format shown in the sample output below.
- Print doubles using exactly 5 digits after the decimal point.
- Your program should exit with an error message if arguments are not supplied.
Program Structure
The following code can be used as a starting point, note that the function does not match
any of the functions asked for above.
// PA1 Assignment
// Author: Chris Wilcox
// Date: 1/15/2014
// Class: CS270
// Email: wilcox@cs.colostate.edu
// Include files
#include <stdio.h>
#include <stdlib.h>
double square(double value)
{
return (value * value);
}
int main(int argc, char *argv[])
{
// Check number of arguments
if (argc < 2)
printf("usage PA1 <double>\n");
double d = atof(argv[1]);
printf("The square of %0.5f equals %0.5f.\n", d, square(d));
return 0;
}
Sample output
Your program should print five lines. The sample output below shows how to compile, link,
and run the PA1 program on Linux using the gcc compiler. See the grading criteria below
for additional information.
$ gcc -Wall -std=c99 -c PA1.c
$ gcc -lm PA1.o -o PA1
$ ./PA1 1.0 1.0 1.0 1.0 1.0
CIRCLE, radius = 1.00000, area = 3.14159.
TRIANGLE, length = 1.00000, area = 0.43301.
SQUARE, length = 1.00000, area = 1.00000.
PENTAGON, length = 1.00000, area = 1.72048.
HEXAGON, length = 1.00000, area = 2.59808.
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly PA1.c.
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
- testCompile: checks that program compiles. (10 points)
- testComment: checks the comment block at top of program. (10 points)
- test1: checks first line of output showing the circle area. (20 points)
- test2: checks second line of output showing the triangle area. (10 points)
- test3: checks third line of output showing the area of the square. (10 points)
- Final Tests
- test4: checks fourth line of output showing the area of the pentagon. (20 points)
- test5: checks fifth line of output showing the area of the hexagon. (20 points)
- Preliminary testing only checks the first through third lines
- Final grading checks the remaining criteria, and includes the preliminary tests.
Submit your program to the Checkin tab on the course website, as you were shown in
the recitation, and read the syllabus for the late policy (if necessary).
© 2014 CS270 Colorado State University. All Rights Reserved.