Write a C program in a file called R2.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 two functions and the main function, which is the entry point for C programs. Do exactly as described in the directions below:
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 |
// R2 Recitation // Author: Chris Wilcox // Date: 08/29/2018 // 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: ./R2 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; } |
Your program should print four lines. The sample output below shows how to compile, link, and run the R2 program on Linux using the c11 compiler.
c11 -g -Wall -c R2.c
c11 -g -Wall R2.o -o R2
./R2 1.0 3.0 4.0
CIRCLE, diameter = 1.00000, area = 0.78540.
RECTANGLE, side1 = 3.00000, side2 = 4.00000, area = 12.00000.
Note that there is a newline character after the last line of output.
© 2018 CS270 Colorado State University. All Rights Reserved.