CS 270
CS 270 Documentation

Programming Assignment P1: Introduction to C


Due: See progress page


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 (arrays declared outside of any function), then write four functions and the main function, which is the entry point for C programs. Do exactly as described in the directions below:
  1. Create a directory for writing programs, we suggest calling it ~/cs270.
  2. Create a subdirectory for the P1 assignment, we suggest calling it ~/cs270/P1.
  3. Create a file called P1.c in the P1 subdirectory, and copy the code shown below.
  4. Personalize the comment block shown below by changing the author and email.
  5. Declare a global array of 4 doubles called input.
  6. Declare a global array of 4 doubles called output.
  7. In the main entry point, make sure the user provides four arguments.
  8. If not, print the following message and return EXIT_FAILURE from main.
            usage: ./P1 <double> <double> <double> <double>
        
  9. Otherwise, convert arguments argv[1] to argv[4] from strings to doubles, using atof.
  10. Don't worry about argv[0], which always contains the program name.
  11. Next write four functions, each of which accepts two parameters and has no return value (void).
  12. Each function accepts an input value (double) and a pointer to an output value (double *).
    • For directions that relate to pointers you don't have to be an expert at pointers just use the provided code as a guide for now
  13. The first function is computeCircle, which computes the area of a circle given the radius, using 3.141593 * radius * radius.
  14. The second function is computeTriangle, which computes the area of an equilateral triangle, using 0.433013 * side * side;
  15. The third function is computeSquare, which computes the area of a square, using side * side;
  16. The fourth function is computePentagon, which computes the area of a regular pentagon, using 1.720477 * side * side;
  17. Each function computes the area, and dereferences the pointer to return it.
  18. Next, finish the main function as follows:
  19. Call computeCircle with input[0], passing it the address of output[0];
  20. Call computeTriangle with input[1], passing it the address of output[1];
  21. Call computeSquare with input[2], passing it the address of output[2];
  22. Call computePentagon with input[3], passing it the address of output[3];
  23. Finally print the results of all computations, as shown below.
  24. The floating-point values given above are only approximations. Live with it. Don’t improve them!
  25. Follow the format shown in the sample output, with 5 digits after the decimal point.

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: Phil Sharp
// Date:   1/15/2020
// Class:  CS270
// Email:  sharpp@colostate.edu

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

// example of a global array
int exampleGlobalArray[2];

// this function can be used as a guide for the funtions you will implement
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, and run the P1 program on Linux using the gcc compiler. See the grading criteria below for additional information.
$ gcc -Wall P1.c -o P1

$ ./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.
  • Make sure your output matches the sample output EXACTLY. There are no regrades on assignments.

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 see the progress page for due dates.