CISC105 Summer 2007 Lab02

Notes before you begin:

Programs

  1. Declare an integer variable i and assign it the value of 10. Print out the value of this variable, along with a nice message.
  2. For this problem, declare an integer variable, but instead of naming it i, give it an invalid variable name. Do you remember the rules for variable naming? Try to compile your program and look closely at the error message. Then, comment out the variable declaration and add a printf statement, which prints to the screen the reason (in your own words) that the variable name is invalid. Your program should now compile, and when you run it a single sentence should print to the screen. Once it is working, repeat this entire process again in the same file, but make the new variable invalid for a different reason. In the end, your program should print two sentences to the screen.
  3. Write a new program. Declare three variables, i, j, and result. Assign the values 5 and 10 to i and j, respectively. Then calculate result according to the following formula:
    result = i2 + 3j - 11
    Note that the above formula is not C code. You'll need to translate this formula into something C understands. Then, print out the value of result, along with a nice message.
  4. Copy your previous program into a new file for this program. Use the Unix command "cp." Look up the command in your Unix book if you're not sure how to use it. What should the new file be named? Using this copied program as a base, change the value of i to 10 and the value of j to 15. Compile it and run it so that you're sure everything works correctly. Follow these instructions very carefully: After the statement which prints the value of result, add another assignment statement that changes the value of i to something else. Then, after that, add one more statement that prints the value of result once again. Has this value changed? Add a comment to your program explaining why the value of result has or has not changed.
  5. Declare an integer variable named i. Assign it the value 50.5 (which is not an integer), and then print out the variable with a nice message. Explain in a comment why you get the results that you do.
  6. Now declare a double variable f and assign it the value 50.5. Print out the value of f (with a nice message, of course). What type of placeholder do you need for a double variable? This should print correctly, so make sure it prints what you expect.
  7. Declare an integer variable named i and give it the value 9. Print the result of dividing i by 2. Explain in a comment what's happening with your results. Also explain in comments a way to make this division work correctly. Hint: there are 3 simple ways to do this. Now, implement the change you described in your comments. Make sure your program is printing the correct result of the division in the end.
  8. Write a program that has all three components needed for function use that we discussed in class. Add a comment describing these three components. Your function should simply print "Hello, World!". There should be no printing done directly from main.
  9. Write a program with a function. This function should take two parameters, an integer and a double parameter, and print the sum of the two numbers. You may ask the user for these numbers with a scanf, or you may just assign them yourself. Make sure, however, that the variables have values before they are sent to the function. That is, whether you're using scanf or assignment, you should set those values in main and then pass the variables to the function.

Submission

You should have a total of 9 programs, named lab02.1.c to lab02.9.c. Make a single script file (named lab02.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab02.1.c, then do the same with lab02.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.