CS 270 Programming Assignment P1

Introduction to C


Due Sunday, August 30 at 10:00pm, no late deadline.


This assignment has four objectives:
  1. to write a C program with console output,
  2. to learn how to submit your C program using the Checkin tab on the course web site,
  3. to understand how preliminary testing works, and
  4. 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 write five functions and the main entry point, 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:   8/24/2015
// Class:  CS270
// Email:  wilcox@cs.colostate.edu

// Include files
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

bool computeSphere(double radius, double *volume)
{
    // Compute volume
    double result = (4.0 / 3.0) * (3.141593 * radius * radius * radius);

    // Dereference pointer
    *volume = result;

    return true;
}

int main(int argc, char *argv[])
{
    // Check number of arguments
    if (argc != 2)
        printf("usage P1 <double>\n");

    // 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);
}

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 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:

Grading Criteria

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).
© 2015 CS270 Colorado State University. All Rights Reserved.