CISC105 Fall 2006 Lab02 CISC105 Fall 2006 Lab02

Programs

  1. Write a program that has all three components needed for function use that we discussed in class. (Can you name them?) The function should simply print "Hello world!". There should not be any printing done directly from main.

  2. Write a program with a function that one integer argument and one double argument (remember the max function we wrote took three integers). The function should print the sum of the two numbers.

  3. The expressions (10 < 25 > 0) looks like it might be true, and (7 < 14 < 5) 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.

  4. In a single program, use the logical operators && and || to write an expression that correctly captures each of the following the ideas, and prints the evaluation:
    1. One is less than five and five is greater than zero

    2. Two is greater than one or three is less than four

    3. Five is greater than one or less than two

    4. One is greater than two, or five is less than six and seven



  5. Write a program using the && operator in the condition for an if statement. Take a real number as input (use a double variable and scanf ) and print "Eureka" if the number is between 50 and 100, inclusive; print "34.5 is out of range" otherwise (where 34.5 is just an example number - you print the input). Test your program with the three possible cases of numeric input.

  6. Copy lab02.5.c and add only a negation operator (!) and a pair of parentheses to reverse the behavior of your program.

  7. Write a program that reads in an integer using scanf. Then use only if (and not else) so that it prints "hi-test" if the integer is greater than 100, and "decaf" if the integer is less than or equal to 100.

  8. Use a shell command to copy the lab02.7.c program, then modify it so that it uses else. Is this version better or worse than the previous version? Think about how much work the computer has to do, and how easy it is for a human C reader to understand what is going on. Write your brief thoughts in the comments.

  9. Write a program that demonstrates the importance of operator precedence. Your program will take in three integers from a user (using scanf) and place them in the variables a, b, and c. Then print the result of a C expression that you design, using those three numbers, that has no parentheses. Finally, 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 parens here> is: 15
    
    


  10. Write a program with a while loop that stops when the user enters a number that is evenly divisible by 7 (that is, there is no remainder).

  11. Write a program that sums all the odd integers between 0 and 50 inclusive and prints the total. Use a defined constant as sentinel.

  12. Write a program with a while loop that sums all numbers entered by the user (how will you get the input?) until the user enters a negative number, which should not become part of the sum. Print the sum only once, after the loop is finished (but you may also want to print the sum each time the program loops until you are finished debugging). Demonstrate your program using all the integers from 4 to 14 inclusive.

  13. Finally, show in your script file that you know how to use "ls -al" to look at your home directory. On the copy that you are handing in to your TA:
    1. circle and label the oldest and the newest file in the directory

    2. circle and label the largest and smallest files

    3. circle and label a set of file access privileges (sometimes called the "mode") of a directory



You should have a total of 12 programs named lab02.1.c to lab02.12.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form.

Submit all C files and your script on WebCT, and give the paper version of the complete script file only to your TA at the beginning of your next lab. Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.



File translated from TEX by TTH, version 3.38.
On 13 Sep 2006, 13:21.