CS 270 Spring Semester 2016
Programming Assignment P1: Introduction to C
Due Sunday, Jan. 24 at 11:59pm, no late submissions.
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 P1.c, using the example of the code structure
shown below. You must declare two global arrays, then write four functions and the
main function, which is the entry point for C programs. Do exactly as
described in the directions below:
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.
// P1 Assignment
// Author: Chris Wilcox
// Date: 1/8/2016
// Class: CS270
// Email: wilcox@cs.colostate.edu
// Include files
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void computeSphere(double radius, double *volume)
{
// Compute volume
double result = (4.0 / 3.0) * (3.141593 * radius * radius * radius);
// Dereference pointer to return result
*volume = result;
}
int main(int argc, char *argv[])
{
// Check number of arguments
if (argc != 2) {
printf("usage: ./P1 <double>\n");
return EXIT_FAILURE;
}
// Parse arguments
double radius = atof(argv[1]);
// Local variable
double volume;
// Call function
computeSphere(radius, &volume);
// Print volume
printf("The volume of a sphere with radius %.5f equals %.5f.\n", radius, volume);
// Return success
return EXIT_SUCCESS;
}
Sample output
Your program should print five lines. The sample output below shows how to
compile, link, and run the P1 program on Linux using the c11 compiler.
See the grading criteria below for additional information.
$ c11 -g -Wall -c P1.c
$ c11 -g P1.o -o P1 -lm
$ ./P1 1.0 2.0 3.0 4.0
CIRCLE, radius = 1.00000, area = 3.14159.
TRIANGLE, length = 2.00000, area = 1.73205.
SQUARE, length = 3.00000, area = 9.00000.
PENTAGON, length = 4.00000, area = 27.52763.
Specifications
Your program must meet the following specifications:
- Work on your own, as always.
- The name of the source code file must be exactly P1.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 machine language file, etc.
- Preliminary Tests
- testCompile: checks that program compiles. (0 points)
- testComment: checks the comment block at top of program. (10 points)
- testArguments: checks your handling of the incorrect number of parameters (10 points).
- test1: checks first line of output showing the circle area. (20 points)
- test2: checks second line of output showing the triangle area. (20 points)
- test3: checks third line of output showing the area of the square. (20 points)
- Final Tests
- test4: checks fourth line of output showing the area of the pentagon. (20 points)
- Final grading 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).
© 2016 CS270 Colorado State University. All Rights Reserved.