Lab05, CISC105 Summer 2005

Directions

Practicing UNIX

Run quota -v to see how much quota you have allotted and how much you are using.

Run du -sh in your cisc105 directory to show which directories consume the most space.

You will demonstrate your use of UNIX at the end of this assignment as well.

Programs

  1. (40) Combine the functions from the last lab into a larger program that draws the cosine and sine functions. Start with lab04.3.c. Modify the program so that instead of simply printing out the sine and cosine, you draw a line representing the magnitude at each increment.

    Two things to keep in mind:

    Label the value at each increment. (Notice how you can use those simple functions to write a pretty cool program!)

    Add a user menu to print either the sine or cosine function.

    Example Output:

    0.000 
    0.296                               ********
    0.565                               ****************
    0.783                               ***********************
    0.932                               ***************************
    0.997                               *****************************
    0.974                               *****************************
    0.863                               *************************
    0.675                               ********************
    0.427                               ************
    0.141                               ****
    -0.158                           ****
    -0.443                  *************
    -0.688           ********************
    -0.872     **************************
    -0.978  *****************************
    -0.996  *****************************
    -0.926    ***************************
    -0.773        ***********************
    -0.551               ****************
    -0.279                       ********
    0.017                               
    0.312                               *********
    0.578                               *****************
    
  2. (30) The function rand(), with no parameters, returns a pseudo-random integer on successive calls in the same program. Write a simple program that prints five calls to rand so you can see how it works.

    You can use the mod operator to restrict the range of integers that are produced by calls to rand. (Think about how to implement that.) Then you can shift the whole range to the left or right using addition.

    Write a function that takes as parameters a high and low value and then produces a random integer in that range (inclusive). Put a call to the function in main, add scanf and a loop so the user can generate as many random numbers as he wishes, each in a different range. Add a prototype if you haven't already.

    Now make your output more random: add the statement srand( time(NULL) ) to the beginning of your main. Can you see that this is just a function call inside a function call? What do you think it does? Explain in your comments (the answer is hidden in your text).

    Example Run:

    Enter the number of random numbers to generate in a range (or -1 to exit): 10
    Enter the low and high values in the range: 1 10
    Your random numbers are
    10 7 5 1 2 2 3 8 5 7
    Enter the number of random numbers to generate in a range (or -1 to exit): -1
    Good bye!
    
  3. (15) Write a program that uses three different techniques to initialize all elements of an array of size 5 to zero.
  4. (15) Create an integer array of size 10. Take ten data points into the array using scanf and print it nicely. Then modify the contents of the array so that every location contains the sum of all numbers to the left and itself.

    Example run:

    Enter 10 numbers: 2 5 3 1 1 1 0 -1 1 1
    The running sum is 2 7 10 11 12 13 13 12 13 14
    
  5. (15) Modify factorial.c so that it declares an array of ten integers. Use repetition to initialize the array to the numbers 1-10. Then use repetition to call your factorial function on each element of the array and assign the result of the function call to the element in the array. Show the contents of the array before and after modification. (You can get rid of any extra, unused code from factorial.c.)
  6. (20) Write a program that uses a function call inside a loop to fill an array with random numbers in a range set by the user; then print asterisk histograms.

    void printHistogram( int data[], int dataSize );

    Write the function that goes with this prototype. (Recall why the size of the array is a parameter.) It should print each element of the array on a different line. Test it by creating three arrays of different sizes, and call the function from main() for each.

    Example Run (only one array):

    Enter the range: 5 10
     6: ******
     9: *********
    10: **********
     7: *******
     6: ******
    
  7. (20) Read 100 doubles into a one-dimensional array from the file provided. Print the array neatly in ten rows of ten numbers. Make sure your columns line up along the decimal point, and only print two places to the right of the point. Then traverse the array only once and display high, low, and mean of the numbers. The file is here.
  8. (20) Repeat the last program, but use a two dimensional 10x10 array. Because this array is 2-D, you'll need nested loops to fill and traverse it. Write a print function for the array. When you pass a multi-dimensional array as a parameter, all of the dimensions except the leftmost must be specified, as in: void printArray( int data[][10], int dataSize ); After that is working, add a second array that is 20x10, fill and print it.
  9. (15) Write a function that takes an array of size doubles and returns the product of the elements. Declare three arrays of different sizes, initialize, print (perhaps using a function you wrote earlier in the lab), and show the products.

You should have a total of 9 programs named lab05.1.c to lab05.9.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form (if it didn't compile, don't run it in the script - mark the place in the printed script file with a marker so it stands out).

Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.

Execute your program multiple times to show that you tested the program well.

More Practicing UNIX (10)

Print out your disc quota. Note how much space you are using and how much is available to you.

Run du -sh * in your cisc105 directory and identify which directory takes up the most space. Use | grep M if you get too much data to sort through.

Submission

Email the tar file to Gang by midnight on Wednesday (see lab00 for directions). Give the paper version to Gang at the beginning of your next lab.

Grading

Grading is as noted above for each program, plus 10 points for demonstrating your use of UNIX, for a total of 200 points.


Some programs adapted from Terry Harvey and Phill Conrad.