Com S 228 --- Introduction to Data Structures HOMEWORK 0: GETTING STARTED (File $Date: 1995/01/20 15:37:52 $) Due: problems 1-2, in your discussion section, week of Jan 19; 3 and 5-7 beginning of class, Jan. 23. In this homework, you will get around a bit on the Com S department machines, learning how to print files, sending us vital information about your login, and learning how to run C++ programs. The section headings below give the readings related to the problems. COURSE POLICIES AND PROCEDURES (HANDOUT) section 3.6 1. (10 points) If you haven't done this already, obtain a Com S department user account. To do this, go to the ``Unix Account Activation Terminal'' in 116 Atanasoff Hall. Then follow the directions on that terminal to get your account. If you have problems with this, contact the System Support Group (ssg@cs.iastate.edu) at 294-9727 or go to their office in 108 Atanasoff Hall. If you cannot complete this by the due date, let us know you are having problems, and hand this problem in as soon as you get your account. a. Send email to your TA with the subject "HW0, my information" Put in the body of the message the following information: your family name, your given name, the last 6 digits of your University ID number (i.e. your social security number), your Com S login name, your local phone number (if you have one), and time of the discussion/lab section for which you are registered. The format should look like the following example Family Name Given Name Last 6 of ID login Phone Section Leavens, Gary 123456 leavens 294-1580 Thurs. 11 We will use this information for grading reports, class lists, and as a means to contact you. (We won't let anyone see your phone number except the course staff.) 2. (12 points) This problem is about the course directory structure and the course documents in the doc directory. You need to log in to a Com S department machine to do it. Write brief answers to the following questions on a piece of paper and hand them in. a. Change to the directory /home/cs228/public. What are the names of the directories in /home/cs228/public? b. One of the directories in /home/cs228/public is called doc. Change to that directory. What is in the file ``running-c++.txt''? c. In what file in /home/cs228/public/doc are the office hours for the course? What are the office hours of your TA? d. What is in the file ``error-messages.txt''? e. What is in the file ``getting-to-coms.txt''? f. What is in the file ``printing.txt''? The rest of this homework is found in the file /home/cs228/public/homework/hw0.txt You should work on-line from this file. You might want to print a copy if you'd rather have it in front of you. 3. (5 points) Read the file ``/home/cs228/public/doc/reading-news.txt''. Create the files that are suggested and try reading the news for our class (in the newsgroup isu.coms.228). There should be a message whose subject is: ``The message for HW0''. As your answer to this problem, write down the contents of that message. You should try to read the newsgroup isu.coms.228 fairly often. 4. (suggested practice) To make life easier on the Com S Unix machines, you may want to personalize your environment by doing the following. a. To avoid retyping the name of the course public directory, /home/cs228/public, it is nice to have a quick abbreviation for it. Put the following line in your ~/.login file, so that when you type $PUB in the shell it expands to /home/cs228/public setenv PUB "/home/cs228/public" To see this working, you'll have to log out and then log back in. Then try the command, to demonstrate that it's working. ls $PUB b. To access the programs in the course directory /home/cs228/public/bin easily, you can put the following line in your .login file setenv PATH ${PATH}:/home/cs228/public/bin Then log out and log back in for this to take effect. c. To save yourself retyping information about the class for assignments, make a file ~/.me containing // Name: your name // TA: your TA's name // Section: your section # where you fill in the information following the colons as appropriate. d. The file ~/.cshrc can be used to customize the Unix (c)shell. Create this file and put in it the following line. alias rm 'rm -i' Make sure the file ends in a newline. This says to make the rm command (for removing files) always ask for confirmation; it may save your neck sometime. This makes pwd give you shorter strings. If you are a DOS user, you can put other lines in the file to alias your favorite DOS commands to their Unix equivalents. For example, you might want to use the following. alias dir ls alias copy cp alias rename mv e. The news readers on Unix, and some other programs use a file named ~/.signature. Create a file .signature in your home directory. It should contain your street address, your e-mail address, and (if you wish) your phone number. It should be no more than 4 lines long. f. For more information about details of using the Com S department machines, such as how to forward your mail to/from project Vincent, how to get your files to/from Vincent (use ftp), how to customize your X windows or how to display X windows stuff from a Vincent workstation, use the program ``mosaic'' on a Com S department workstation (or use the URL ``http://www.cs.iastate.edu/'') and click on the topic ``How Do I Do That'' in the first list of things, and then when you're in that page, click on the topic ``CS user FAQ'' (which is under the subheading ``Questions and Answers''). (The exact URL is ``http://www.cs.iastate.edu/help/fap.html''.) RUNNING C++ See the file /home/cs228/public/doc/running-c++.txt for details about running C++. 5. (10 points) In this problem you will edit and run a simple C++ program. The questions below are for your own benefit; don't hand in answers. You will hand in a printout of the program when you are done. a. Make a directory ~/hw0, by running the following commands cd mkdir hw0 Now create a file ``roulette.C'', using emacs (or some other editor). In the file place the text between the first pair of dash lines below. (Your TA will explain what this does for you.) ----------cut here---------------------------------- // roulette.C #include #include #include void initialize_roulette_wheel() // MODIFIES: roulette_wheel state // POST: the roulette_wheel state has been initialized. { extern time_t time(time_t * tloc); extern void srand(unsigned int seed); // initialize the random number generator from the time srand((unsigned int)time(0)); } int roulette_wheel() // MODIFIES: roulette_wheel state // POST: FCTVAL is a random number between 0 and 36. { extern int rand(); return rand() % 37; } int main() // MODIFIES: cout // POST: To cout is written a random number, then a newline. { initialize_roulette_wheel(); cout << roulette_wheel() << endl; return 0; } ----------end of cut-------------------------------- Compile and execute this program. One way to do this is the following (your TA will explain what this all means). g++ -ansi -pedantic -Wall -o roulette.exe roulette.C This complies and places the text for the roulette program in the file roulette in your current working directory. If you get a compile error, check that you copied it exactly (hint, edit from the on-line copy of this homework). To play with it, type: ./roulette.exe It should print a number. If not, check that you copied the above exactly as shown. Normally for a programming assignment, you would hand in a printout of your file ``roulette.C'' and a transcript of your testing. For this problem you just have to hand in a printout of a transcript showing your testing. (For testing, just run it a few times.) Be sure to label this transcript with // Name: your name // TA: your TA's name // Section: your section # at the top. (You can do this automatically by creating a file ~/.me with this information in it, and then using typing ``cat ~/.me'' (without the quotes) at the beginning of the session that makes your transcript. The easiest way to make a transcript is to use the program ``script''. You first invoke the command script, then do your testing, then use the command ``exit'' to stop the script. For example, the following session makes a transcript on roulette-test.out % script roulette-test.out Script started, file is roulette-test.out % ./roulette.exe 7 % exit Script done, file is roulette-test.out (Alternatively, you can use the window system, or emacs to make a script.) Once you are done making the script file, print it. b. Now let's demonstrate how to work with separate compilation, along with making this program be a bit more useful. We'll separate the main procedure (what Unix invokes to start the program running) from the roulette wheel and other procedures. We'll also use some header files. Make a file roulette2-main.C and put in it the text between the first pair of dash lines below. ----------cut here---------------------------------- // roulette2-main.C #include "roulette2.h" int main() // MODIFIES: cin, cout, roulette_wheel state // POST: Prompt on cout for an input, // then write to cout "You win!" or "You lose!", then a newline. { initialize_roulette_wheel(); cout << "Bet? "; int bet; cin >> bet; int spin = roulette_wheel(); compare_with_bet(spin, bet); return 0; } ----------end of cut-------------------------------- Similarly, make a file roulette2.C and place the following in it. ----------cut here---------------------------------- // roulette2.C #include "roulette2.h" #include #include void initialize_roulette_wheel() { extern time_t time(time_t * tloc); extern void srand(unsigned int seed); // initialize the random number generator from the time srand((unsigned int)time(0)); } int roulette_wheel() { extern int rand(); return rand() % 37; } void compare_with_bet(int wheel_spin, int bet) { if (wheel_spin == bet) { cout << "You win!" << endl; } else { cout << "You lose!" << endl; } } ----------end of cut-------------------------------- Finally, make a file roulette2.h and place the following in it. ----------cut here---------------------------------- // roulette2.h #ifndef roulette2_h #define roulette2_h #include extern void initialize_roulette_wheel(); // MODIFIES: roulette_wheel state // POST: the roulette_wheel state has been initialized. extern int roulette_wheel(); // MODIFIES: roulette_wheel state // POST: FCTVAL is a random number between 0 and 36. extern void compare_with_bet(int wheel_spin, int bet); // MODIFIES: cout // POST: cout has "You won!\n" added to it, if wheel_spin == bet, // cout has "You lose!\n" added to it, otherwise. #endif ----------end of cut-------------------------------- Hand in a transcript showing both how you compiled this program, and your testing. (For testing, just run it a few times.) How to make a transcript is explained in part 1 above. Be sure the transcript is properly labeled with your information, as described in problem 1. You can now compile your program with the following command. g++ -ansi -pedantic -Wall -o roulette2.exe roulette2-main.C roulette2.C An easier way to do this is to use the program ansic++ in the course directory /home/cs228/bin. To do this you could use the following command, provided you have /home/cs228/bin in your PATH (see problem 4b). ansic++ -o roulette2.exe roulette2-main.C roulette2.C 6. (10 points) Change the program in problem 5b so that it prints out the number selected at random as well as the message. Also change the messages so that "You won!" becomes "You won. Hooray!" and "You lose!" becomes "You lost. Sorry.". Finally put your name, TA, and section information in comments in each file, after the file name. (Insert your ~/.me file into the file after the first line.) Hint: you may want to use a statement such as cout << spin << endl; Hand in a printout of the files you changed, and a transcript showing your testing. Be sure the files and the transcript are properly labeled.