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 gcc compiler.
gcc -g -Wall -c R2.c
gcc -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.
GDB (GNU DeBugger)
Although it might not be very useful for this program, now would be a good time to familiarize yourself
with the GDB debugger for C programs. GDB is a command line debugger with features similar to Eclipse's
debugger for Java. Alternatively, you can use DDD (data display debugger) for a version of GDB with a
GUI. In order to use GDB/DDD, you must compile your program with the -g flag. To open GDB or DDD, use
one of the following instructions:
gdb ./a.out
ddd ./a.out
Replace a.out with the name of your executable file. Make sure that the last line in the terminal
says "Reading symbols from ./a.out...done." If this doesn't show up, you need to recompile with
the -g flag and try again.
You can use the break command to set breakpoints now. For example:
break function_name // Sets a breakpoint at the beginning of a function
break file.c:42 // Sets a breakpoint at line 42 in file.c
After placing any desired breakpoints, use run to start your program. run can be used just like the executable in the shell, and can take parameters in the same way. For example:
$ ./a.out param1 < inputFile.txt // In the shell
$ run param1 < inputFile.txt // In GDB
Your program will now stop at any breakpoint it hits, and you can use 'next' (the same as Eclipse's step over) and 'step' (Eclipse's step into) to navigate your program. In addition, you can use the 'print' command to view the value of an expression. Here are some interesting ways to use it:
print varName // Print the value of varName
print charPtr // Print the address the pointer points to, and the string at that address
print arr[2] // Print the value at index 2 of the array
print &someVar // Print the address of someVar
print intVar + 2 // Print 2 more than the value of intVar
print *(ptrVar + 2) // The same as arr[2]
Once finished, you can use 'quit' to exit GDB. It might complain that you have a program running- just use Y to kill the program and exit GDB.
© 2018 CS270 Colorado State University. All Rights Reserved.