CISC105 Summer 2007 Lab03
Notes before you begin:
- You will write one program for each of the following problems. You may start each program using a previous program as a base, but you will learn more if you code each from scratch. At this point in the semester, at least, I much prefer that you you start each from scratch. It doesn't take much longer, and it's valuable practice.
- Be sure to save each program as its own .c file. Name them lab03.n.c, where "n" is the number in the program list below. For example, the name of the file for the first program will be lab03.1.c
- Style counts. This includes header comments, comments on any tricky lines of code, good variable naming, good spacing and indentation. These are worth points to you.
- When scripting your lab, you will compile and run each one. For each .c file, you will first cat the file, then compile it, then run a.out and fully demonstrate the capability of the program. If your program does not compile, do not attempt to run it. Since it doesn't compile, no new a.out file is created--thus if you try to run it, you'll just be running the previous file which did compile.
Programs
- Write a program that reads in two integer values from the user. The program should then use a single if/else block to print out the larger value.
- Rewrite #1 (or copy it using the "cp" command), but this time use a function. The function should accept the two integers as parameters, and use if/else to print the larger number. User input should still be done in main. Make sure all the necessary components of a function (including a comment and a good name!) are present.
- In a single program, use the condition testing operators we have talked about along with the logical operators && and || to write an expression that captures each of the following ideas. The program should print the evaluation of each of these ideas. Remember: what type does a conditional test evaluate to? How do you print that type?
- Five is less than ten and ten is greater than seven
- Two is greater than one or five is greater than two
- Five is less than zero or zero is less than five
- Ten is greater than twenty, or zero is less than both one and two.
- Write a program using the && operator in an if statement. Take a real number as input (what data type is this?) and test whether the number is between ten and twenty, inclusive. Print "Yes!" if it is, "No." if it is not. Test this thoroughly in your script (you'll have to run the program more than once to test it thoroughly).
- The expression (5 < 10 > 8) looks like it might be true, and (5 < 7 < 3) looks like it ought to be false. In fact, they are both meaningless, bad code. They can still be evaluated, however. Use printf to show the evaluation of these two expressions, and explain in comments why you get these results.
- Write a program with a function that will print a line of n asterisks on a single line. The function should take a single parameter and print that many asterisks. You should only have one print statement (apart from the user input/output, of course), which prints a single asterisk.
- Write a program with a while loop that prints all the even numbers pbetween 0 and some user input. Inside this loop, also sum these numbers. Print out the sum in the end (after the loop is done).
- Modify program #6, using the cp command to make a copy of it for this problem. Add a while loop to your main, which repeats the user input/function call segment of the program until the user enters a value of -1. In comments: what word do we use to describe the -1 in cases like this?
Submission
You should have a total of 8 programs, named lab03.1.c to lab03.8.c. Make a single script file (named lab03.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab03.1.c, then do the same with lab03.2.c, etc.
Submit all program and script files on MyCourses before 11:55 Sunday night. Give the paper version of your script to your TA next Monday at the beginning of lab. Write your name and the lab number on the front of your paper submission. Also, if something doesn't work properly, please circle it in bright ink and make a note for your TA explaining the problem. You will lose less points for a problem you recognize and can explain (at least a little) than for one you don't seem to notice.