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

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