CISC105 Spring 2006 Lab02 CISC105 Spring 2006 Lab02

Programs

  1. Write a simple program that asks for input and reads in a single integer using scanf (what does scanf need? don't forget the ______) and prints it back out using printf. Try to do this without looking at your text, notes, or website examples (as you would for an exam). If you need to look, do so, but then try it without looking again. Test it.
    Now modify it so that the single scanf statement now reads three integers into three variables and then prints the values. It is good programming practice to start with a very simple program, test it, and then add the more complicated features that you want, one at a time. Only submit the three variable program.

  2. Use the unix copy command to copy the file lab02.grade.c to your lab02 directory. This file is in the same directory as the lab, but since it is on the composers you can copy it just as you would within your own directories by using its full path name:
    /www/htdocs/CIS/105/tharvey/06S/labs
    Note that the pathname has a slash at the very beginning.
    After you copy the file, examine it to see if you can find the logic error in the program. Being able to read code and predict its behavior is very useful when programming and on exams. Once you find thee problem, compile it and test the program to see if you are correct, then fix the program.

  3. Write a program that reads in an integer (how?). Then use only if (and not else) so that it prints "green" if the integer is greater than 100, and "aroma" if the integer is less than or equal to 100.

  4. Use a shell command to copy the lab02.3.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 to understand what is going on. Write your brief thoughts in the comments.

  5. 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 tht 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
    
    


  6. The expressions (1 < 4 > 0) looks like it might be true, and (1 < 4 < 3) 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 "One is less than five and five is greater than zero" and print the evaluation of that later in the same program output. This may well be the most important lesson in this lab.

  7. Write a program using the && operator (pronounced "and") in the condition for an if statement. Take a real number as input (use a double variable) 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).

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


  9. 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).

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

  11. Using combinations of if, else, and only the boolean operator >, write a program that takes three integers as input from a user and prints out only the largest number. Now for the hard part: no matter what three numbers are put in, and no matter what order, to get full credit no more than two numeric comparisons may take place before the answer is known. There is more than one correct way to do this, but it takes some thinking.

  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 14 to 21 inclusive.

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.
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


Submit all twelve 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 21 Feb 2006, 20:48.