CS 270 Fall Semester 2017

Programming Assignment P1: Introduction to C


Due Sunday, Aug. 27 at 10:00pm, 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 shown in the Program Structure section below. This program will compute the areas of some geometrical figures based on some command-line arguments. 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 given function computeSphere does not match any of the functions asked for above. It is intended as an example of how to return a value through a pointer. Your TA will explain what pointers are and how they are used in this example. If you want to do well in this class, you will need to become comfortable with pointers. 


 1

 2

 3

 4

 5

 6

 7

 8

 9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

// P1 Assignment

// Author: Chris Wilcox

// Date:   1/22/2017

// Class:  CS270

// Email:  wilcox@cs.colostate.edu


// Include files

#include <stdbool.h>

#include <stdio.h>

#include <stdlib.h>


void computeSphere(double radius, double *addressOfVolume)

{

    // Compute volume

    double result = (4.0 / 3.0) * (3.141593 * radius * radius * radius);


    // Dereference pointer to return result

    *addressOfVolume = 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 four 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 -Wall P1.o -o P1


./P1 1.0 2.0 3.0 4.0 5.0

CIRCLE, diameter = 1.00000, area = 0.78540.

TRIANGLE, side = 2.00000, area = 1.73205.

RECTANGLE, side1 = 3.00000, side2 = 4.00000, area = 12.00000.

HEXAGON, side = 5.00000, area = 64.95195.

Note that there is a newline character after the last line of output. 


A recurring issue every semester is that some students lose a large number of points for neglecting to pay attention to spelling, spaces, commas, periods, etc. To catch problems like these, you may want to consider using the diff command. Here is an example of how to use this command: 

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).


© 2017 CS270 Colorado State University. All Rights Reserved.