Lab01, CISC105 Summer 2005

Directions

Practicing UNIX

Find and review the Unix shell commands ls, cd, rm, mv, and mkdir in Andersen. Practice making one or more directories, moving into them, listing the files and directories there, and then moving back out. Use rmdir to remove any practice directories you have made.

Create a directory ``cisc105''. Go into "cisc105" and create directory ``lab01''.

Now, go into your home directory again. (You can use cd .. to go into the parent directory or cd to go into your home directory.)

Move your lab00 directory into the cisc105 directory.

Now, go into the lab01 directory and start writing programs!

Programs

  1. (10) Declare three integer variables (i, j, and result) and assign them values to calculate and display result = i / (j - j). Explain the results in comments.
  2. (5) Write a program with a single printf statement (and a return, but nothing else) in main that prints:
       *
      **
     ***
    ****
    
  3. (10) Write a simple program that reads in two integers using scanf. (Remember scanf needs a variable to put the value in, and don't forget the ampersand.) Then, print out the input using printf. Try to do this without looking at your text, notes, or website examples (to practice for an exam). If you need to look, do so, but then try it without looking again. Add a printed ``prompt'' so that when the program stops for input, the user knows what the program expects. Experiment with what you can and cannot put before, after, and between the numbers. Briefly write what you learn in the comments.
  4. (10) Write a program that demonstrates the importance of operator precedence. Your program will take in three integers from a user and place them in the variables a, b, and c. Then, print the result of a C expression, using those three numbers, that has no parentheses. Then, show a different result from the same expression with one added pair of parentheses.
    Example output (with fake numbers):
    strauss% ./a.out
    Enter a, b, and c: 12 34 5
    The result of <print out your equation here> is 51.
    The result of <print out your equation with parentheses here> is 15.
    
  5. (10) Write a program that will calculate and display the volume of a cylinder with a real number radius and height. You may assume pi=3.14159. (Hint: Use a constant. That's the last reminder about constants this semester. Use them when appropriate in the rest of this and future assignments.) The volume of a cylinder is height * the area of the circle. (What datatypes should the radius, height, and volume be?) Allow the user to enter the radius and height of the cylinder.
  6. (15) Write a program that creates a table of Olympic competition running distances in meters, kilmeters, yards, and miles. The following distances should be used: 100 m, 200 m, 400 m, and 800 m.

    Note that 1 m = .001 km = 1.094 yds = .0006215 mi

    Calculate and print the results to the screen in the following manner:

    Table of Olympic running distances
    --------------------------------------
    Meters   Kilometers   Yards    Miles
       100         ----    ----     ----
       200         ----    ----     ----
       400         ----    ----     ----
       800         ----    ----     ----
    
  7. (15) In class, Dennis said that he runs the 200m in 21 seconds and Kristen runs the 200 m in 24 seconds. (I hope I have that right! Change the speeds if I am not remembering them correctly.) Calculate how fast they are going in m/s and mi/hr.
        CISC105 Speed Demons
    -----------------------------
             meters/s    miles/hr      
    Kristen      ----        ----
    Dennis       ----        ----
    
  8. (15) The percent sign % is a math operator (specifically the mod operator) in C. Declare three integer variables (i, j, and result) and assign them values to calculate and display result = i % j. Use a series of assignment and print statements to show, in a single execution, the results of i % j, where i = 4 and j increases from 1 to 8.
  9. (10) Write a program that reads in two numbers. Then use only if statements to print "Player 1 wins" if the first number is bigger, "Player 2 wins" if the second number is bigger, and "You tied!" if the numbers are equal.
  10. (15) Copy the previous program and modify it so that it uses elses. Is this version better or worse than the previous version? Think about how much work the computer has to do (performance), and how easy it is for a human to understand what is going on (readability). Write your thoughts in the comments.
  11. (10) Write a program using the && operator in the condition for an if statement. Take a real number as input and print ``Eureka!'' if the number is between 500 and 1000, inclusive; otherwise, print ``Your number (< the number >) is out of range.''
  12. (5) Copy the previous problem and add only a negation operator (!) and a pair of parentheses to reverse the behavior of your program.
  13. (10) The expressions (2 < 4 > 0) looks like it might be true, and (2 < 4 < 1) looks like it might be false. In fact, they are both meaningless, bad code (why?). But C is happy to evaluate them anyway. Use a printf to show the integer result of each expression, and then explain in your comments how it got that result. Also, write an expression that correctly captures the idea ``Two is less than four and four is greater than zero'' and print the evaluation of that expression in the same program.
  14. (10) Write a program with a while loop that stops when the user enters a number that is evenly divisible by 6 (that is, there is no remainder). Print helpful output to the user.
  15. (10) The expression (value % 2) evaluates to an integer. Use it in a program to control a switch statement that prints ``even'' or ``odd'' depending on value (which the user inputs). Put the switch statement inside a loop so that you can keep entering numbers and determining if value is odd or even. (How many cases do you need? Do you need a default case for this problem? Test and justify your answer.)

You should have a total of 15 programs named lab01.1.c to lab01.15.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form (if it didn't compile, don't run it in the script - mark the place in the printed script file with a marker so it stands out).

Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.

Execute your program multiple times to show that you tested the program well. We require good testing, e.g., running the program with multiple, "interesting" input values. For example, would entering a negative number break your program for the player 1/2 win printing program? We will talk more about "interesting" input values in future lectures.

After you execute each file in the script, use ls, cd, and pwd to show your new directories in the script. (10 points)

Email the tar file to Gang by midnight on Wednesday (see lab00 for directions). Give the paper version to Gang at the beginning of your next lab. (15 points for each submission.)

Grading

Grading is as noted above for each program, 10 points for demonstrating your use of UNIX plus 15 points each for the electronic and paper submissions, for a total of 200 points.


Adapted from Terry Harvey and Phill Conrad.