CISC105 Spring 2006 Lab03 CISC105 Spring 2006 Lab03

Programs

  1. Write a program that sets a variable result to the product of doubles x (value 33.5) and y (value -141). (What type should result be?) Print result. Now change the values of x and y (how? you know two ways to do this), but do not reset result. Print result to see if it has changed. Explain this behavior in terms of memory and the assignment operator (in your comments).

  2. Write a program that asks for user input (an integer), and uses a switch statement to print "emerald" if the user enters 8 and "laura" if the user enters 14. For any other number the program should print "spam". You may only use three cases.

  3. Use a shell command to copy the previous program. Place a loop around the appropriate code so that the user is asked for data repeatedly. You may add some code to make it work properly. The program should stop when the user enters -1 (what is that number called? How should it appear in your program?).

  4. Write a program that asks for user input (a positive integer), and uses a switch statement to print "red" if the number is 0-5; "white" if the number is 6-11; and "chartreuse" if the number is 12-17. Any other number should print "mauve". You may only use four cases.

  5. Write a program that computes the average of a series of five numbers entered by the user. The program should use a loop to read one number at a time (How do we read numbers from the user? How should five be represented in your program?). If you can, do it without reading the hint at the bottom of the page1.

  6. Write a for loop that prints n asterisks in a line. You may only have a print statement that prints a single asterisk! Ask the user how long the line should be, and print it. Repeat until the user enters a length of -1.
     
    > ./a.out
    How long would you like your line? 7
    *******
    How long would you like your line? 3
    ***
    How long would you like your line? -1
    Goodbye!
    >
    
    


  7. Copy the program you made for 6. Using the same single for loop, make a square of asterisks. This time, instead of looping up to the user input number, loop until the input number squared (i.e. print enough asterisks to fill a square of size "input"). Once that is working, put a statement inside the loop that uses a conditional to print a single newline (backslash followed by the letter n). The condition should be true when the loop reaches the end of one line of a square. For example, if the user enters "6", then the program will print 36 separate asterisks with a newline after every sixth asterisk.
     
    > ./a.out
    How big would you like your square? 3
    ***
    ***
    ***
    How big would you like your square? -1
    Goodbye!
    >
    
    


  8. Here is a different way to solve the same problem. Copy the program you made for 6. Add another for loop so that the previous for loop is inside the new one. The two for loops will need different variables (why?). Both for loops will stop on the same number from the user, so that the program prints squares of asterisks instead of just one line:
     
    > ./a.out
    How big would you like your square? 3
    ***
    ***
    ***
    How big would you like your square? -1
    Goodbye!
    >
    
    


Are you familiar with (and using) the Unix shell commands ls, pwd, cd, mkdir, rm? Do you know the meaning of . and .. ? These will be on the exam.
Do you fully understand all of the integer, double, and boolean operations you have performed in previous labs? The goal of a lab is not to get the code working, but to understand 1) why you were asked to write the code; and 2) what each experiment (lab problem) showed. They will all appear on the exam.
You should have a total of 8 programs named lab03.1.c to lab03.8.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 8 program files and your script on WebCT, and give the paper version of the complete script file only on paper 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.

Footnotes:

1See H&K section 5.3 for how to compute a sum in a loop. At the end of the program, the average will be the sum divided by the number of times you went through the loop (should average be an integer?)


File translated from TEX by TTH, version 3.38.
On 1 Mar 2006, 00:49.