|CSC 161||Grinnell College||Fall, 2013|
|Imperative Problem Solving and Data Structures|
Command-line arguments are a great way to give a program input right as the program begins executing. The convention to do this is to give your main function two arguments, argc and argv. argc is an integer related to the number of arguments passed in to the program, and argv is an array of strings related to those arguments, and thus is declared as char * argv [ ]. Let's explore how to use these.
Write a program, args-example.c, in which the main function is in the form: int main (int argc, char *argv). The program should simply print out argc.
Compile and make the following calls:
What seems to be the general pattern for argc?
You may have noticed that argv is a char* array, or in other words, an array of strings. Let's now look at the first element in that array.
Modify your program from exercise 1 so it also displays argv. Then run all of the calls from exercise 1 again. What is the pattern for argv?
Now modify your program so that it displays every element in argv. You should be able to use argc to help you display all of them with a loop. Run all of the calls from exercise 1 one last time. What is the pattern for argv?
Look over the program skeleton command-robot.c. Make sure you understand what the program is doing. This is just the skeleton to a program you will complete. Now complete the program by filling in the question marks so that it supports the following commands:
in the following format: cd, where c is a command character and d is a double. Commands will be in the format l3 b0.5 s.2 r1 b.7. It should support the four motion commands and also the 's' command to modify the speed at which the robot will move.
Test your program with the following commands and watch the robot to make sure it is working as intended:
As you have surely discovered by now, most Linux utilities have "flag" options, where you give the utility a flag when you run it. One example you use whenever you compile is the -o flag, which you use with gcc when compiling a c program if you don't want your executable file to be named "a.out". Such flags are generally in the format -c, where c is the character of a particular flag. It would be useful to implement this with the robot command program.
Implement the following five flags for your command-robot program. Your implementation should have a precondition that the flag arguments come before the command arguments.
-r — This will reverse the order in which the inputted commands are executed.
-s — This will set the the forwardness of the robot to "scribbler-forward".
-f — This will set the the forwardness of the robot to "fluke-forward".
-h — This will print out help information describing what all of the flag and command options are.
-z — This will force the speed to stay at 1 throughout the entire run. The s command (not to be confused with the -s flag) will be unable to change the speed of the robot.
Test your program with various inputs and flags to make sure everything works properly.
In real utilities, the order and location of flags generally does not matter, and you can even combine flags, so that instead of -c -d, where c and d are two different flags, you can do -cd, or -dc, and the functionality will be the same.
Modify your program so that the order and location of flags does not matter. One common method of doing this is going through all of your arguments first to set the flags, and then executing the command arguments.
Modify your program so that you can combine flags.
Development of laboratory exercises is an interative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.