COMP2004 Programming Practice
2002 Summer School

Week 4 Monday Tutorial Exercises


  1. Do the prcs tutorial.

  2. Write a C++ program called is_even which reads a single integer from std::cin, and then returns a true exit status if the integer is even, and a false exit status if the integer is odd. Test your program with the test_is_even shell script. Don't forget to run
    chmod +x test_is_even
    

  3. The read shell command can be used to read a line from stdin into a shell variable. Download and test the enter_name shell script so that you understand how read works. The modify the enter_name shell script so that when the name entered is your own name, the message "Hi $firstname, welcome back" is output, and otherwise the message "Hi $firstname, pleased to meet you" is output.

  4. Modify the enter_age shell script so that where in the first two cases cases, x is the age difference between the age entered and your age. See if you can do it where your age occurs in the source code only once. See if you can do it using only one echo statement after the read statement.

  5. Repeat question 2, but this time make the is_even program be a shell script.

  6. Enter the shell script from last Friday's lecture for compiling code, or use your shell script from Question 3 of the Week 3 Friday tutorial. Modify the shell script so that each .cc file is only compiled if it is newer than the corresponding .o file.

  7. Further modify the shell script from the previous question so that the final program is only linked if any of the .cc files were recompiled.

  8. The wait_for_file shell script is the infinite while loop code from today's lecture, with some echo statements to show where it's up to (and some other small changes). Run the shell script, then get another xterm and in it, run the command
    echo "Hello world" > file.txt
    
    to create the file.txt file. Observe the shell script exit and delete file.txt when this happens.

  9. Rewrite the code from the previous question so that no if statement is used. (Hint: inside square brackets [ ], you can use ! as "not", just like in C++.)

  10. The example_parameters shell script is the example parameters code from today's lecture. Modify it so that only the 1st, 3rd, 5th, etc parameters are be output.

  11. Rewrite the code from the previous question so that the 2nd, 4th, 6th, etc parameters are output.

  12. Write a shell script which works like the join function from Question 6 of the Week 1 Friday tutorial. That is, it should work as follows:
    bash$ ./join fred hello how are
    hellofredhowfredare
    
    (You need to use "./join" because otherwise you will accidentally run the Unix join utility.)

  13. The code
    for i; do
    	echo "$i"
    done
    
    is another way of writing the original example_parameters shell script from Question 10, with the advantage that it doesn't destroy the parameters $1, $2, etc by shifting them. Rewrite the previous question using only this form of for loop (ie. don't use shift at all).

  14. The multiple_tests shell script is the multiple tests code from today's lecture. Modify it so that it works with your Assignment 1 submission and the sample assignment 1 input-output test files.

  15. Modify the code from the previous question so that it exits with a true exit status if all the tests are passed, and it exits with a false exit status is one or more of the tests fail.

  16. Modify the code from the previous question so that the exit status it exits with is the number of tests which were failed. In addition, before exiting it should output a summary which looks like this:
    Summary: x/y tests passed
    
    where x is the number of tests passed, and y is the total number of tests.