CISC105 Fall 2006 Lab09 CISC105 Fall 2006 Lab09

Programs

  1. Use the rand() function to generate a data file of 10,000 integers between 0 and 99, inclusive. In the data file, put ten integers on each line.

  2. How random is rand()? Count the number of occurrences of each integer in the data file from the previous problem, as follows.
    1. Create an array of ints, size 100. This will hold the count of each number, e.g. index 7 will hold the number of sevens in the file.

    2. Read the data from the file with a loop; as each number is read, go to the corresponding index of the count array and increment the value there by one.



  3. Alter the program from 2 so that it takes the name of the data file as a command line argument.

  4. Create a 2d array of char to hold the words "grey", "green", and "greasy".
    1. Show how to change the first 'e' in "green" to a 'y' (hint: do not use a function to do this).

    2. Show how to print the third word.

    3. Then show how to use printf to print only the word "easy" without making changes to the array (hint: remember that when using %s printf wants an address argument).

    4. Finally, show how to replace the word "grey" with the word "great" (hint: use a function!).



  5. Write a function that will search an array of integers of any size for a given key by traversing the array (this is called "linear search"). Your function should return the index where the key is found, as soon as it is found, or -1 if it doesn't find the key. Demonstrate your program, giving typical and boundary cases.

  6. Create an array of 300 integers, and initialize the array to the values 0 - 299. Write a loop that calls your linear search function 10,000 times to find a randomly generated key. Eliminate print statements from your program. Include the header file time.h and put a call to
    srand(time(NULL));
    
    
    at the start of your main() so that the random sequence is different each time. Time your program as follows:
    > time a.out
    1.20u 0.00s 0:01.23 98.3%
    
    
    This is a use of the Unix time function. The first number is the number we care about: the amount of time we used the CPU.
    Do this three times.

  7. Repeat the last experiment using the binary search function we wrote in class.

You should have a total of 7 programs named lab09.1.c to lab09.7.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 C 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 (all Friday labs) or in lecture Friday (Wednesday labs only). 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 31 Oct 2006, 22:42.