input
, allocated statically. This array
will eventually contain the 5 command-line arguments passed in the terminal.
output
, allocated statically. This array
will eventually contain the 4 areas computed in the program.
EXIT_FAILURE
from main (note that there is a newline character at the end of this message).
usage: ./P1 double double double double double
atof
function. The first argument is the diameter of a circle. The second argument is the side of
an equilateral triangle. The third and fourth arguments are the sides of a rectangle. The fifth argument is the
side of a regular hexagon. Store the arguments in the input
array in this same order (starting at
input[0]
).
computeCircle
, which computes the area of a circle given the diameter.
It is a void function that takes two parameters: the diameter as a double
and a pointer to an
output value as a double *
. It computes the area of the circle using the following formula:
area = 3.141593 * diameter * diameter / 4
computeTriangle
, which computes the area of an equilateral triangle given
the side. It is a void function that takes two parameters: the side as a double
and a pointer to an
output value as a double *
. It computes the area of the triangle using the following formula:
area = 0.433013 * side * side
computeRectangle
, which computes the area of a rectangle given the two sides.
It is a void function that takes three parameters: the first side as a double
, the second side as a
double
, and a pointer to an output value as a double *
. It computes the area of the
rectangle using the following formula:
area = side1 * side2
computeHexagon
, which computes the area of a regular hexagon given the side.
It is a void function that takes two parameters: the side as a double
and a pointer to an output value
as a double *
. It computes the area of the hexagon by making use of the computeTriangle
function. You must study the properties of a regular hexagon to figure out how to use that function appropriately.
It returns the area through the pointer passed as an argument.
computeCircle
with input[0]
and the address of output[0]
.
computeTriangle
with input[1]
and the address of output[1]
.
computeRectangle
with input[2]
, input[3]
, and the address of output[2]
.
computeHexagon
with input[4]
, passing it the address of output[3]
.
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; } |
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.
diff
command. Here is
an example of how to use this command:
master.txt
in the P1 folder. Make sure you
do not have trailing spaces after any line and make sure that you have exactly one blank line after the last line of output.output.txt
. You can do this quickly by
running the following command:
./P1 1.0 2.0 3.0 4.0 5.0 > output.txtThe > operator redirects the output of your program into a file instead of showing it in the terminal.
diff
to compare the output of your program against the sample output:
diff -y output.txt master.txtThe -y option instructs
diff
to show the files side by side (output.txt
on the left and
master.txt
on the right). For example, suppose I missed a comma on line 3. My diff
output will be:
CIRCLE, diameter = 1.00000, area = 0.78540. CIRCLE, diameter = 1.00000, area = 0.78540. TRIANGLE, side = 2.00000, area = 1.73205. TRIANGLE, side = 2.00000, area = 1.73205. RECTANGLE side1 = 3.00000, side2 = 4.00000, area = 12.00000. | RECTANGLE, side1 = 3.00000, side2 = 4.00000, area = 12.00000. HEXAGON, side = 5.00000, area = 64.95195. HEXAGON, side = 5.00000, area = 64.95195.Notice the | mark between the two files on line 3. That means the files are different on that line. There are other types of marks. If you do not see any marks between the files, then they are identical. If you want more information on this command, type
man diff
.