Uploaded by Kartavya Gohil

C Programming Lab: Operating System Management

advertisement
ID: _________________ Name:_______________________
Tutorial Day:_________ Time: ________Room:_________
Tutor’s Name:_______________________Date: __________
ICT60001 Operating System Management - Week 8
Goals:



Review lecture 8
Practice adapting and writing C code
String validation, loops, selection, writing functions, random numbers, arrays,
dynamic memory (heap), sorting algorithms.
Resources:




Lecture notes (week 8)
Notebook
PC running Quincy, Visual Studio or gcc
Sample C programs
Lab:
Download and unzip the Lab file (ICT60001Lab8.zip) onto your H: drive, laptop or
portable drive.
Lab Task:
Today you are going to write C programs.
For each program that you write, you must add block comments which describe the
functions you create. Add the following sections:
o Description
o @param – describe each formal parameter
o @return – describe what the function returns
o @pre-conditions – describe any operations or declarations which are
assumed to have been done before the function is called. (optional)
o @post-conditions – describe any changes to the program state or
variables after the function has returned. (optional)
The Lab8ExampleX.c programs contain an example programs showing examples of
code and comments that you can use as the basis of your programs.
These programs are to include a main() which calls the function, passes
appropriate test data and displays the results.
Task 1: Void function, no parameters.
Write debug and run a program which contains a function prototype, a main function
and a void function called greeting.
This is the function prototype:
void greeting();
The greeting function should contain an initialised char array called name,
and a print statement which prompts for a name,
followed by a scanf statement which inputs text into name,
followed by a printf statement which displays the greeting:
ICT60001
J Hamlyn-Harris, Swinburne
Page 1
ID: _________________ Name:_______________________
Tutorial Day:_________ Time: ________Room:_________
Tutor’s Name:_______________________Date: __________
Welcome <yourname>.
where the user's input is used in place of <yourname>
The main function must have a function call to run the greeting function and then shut
down (return 0).
Task 2: return function, no parameters.
Rewrite Task 1 (and save as task2) so that the greeting function returns the number of
non-null characters in the name variable (after user input). You will need to include
the string header file and use the strlen function. Don't forget to update the function
prototype and definition.
Modify the code in the main function to store the return value in an integer and
display it on the screen.
Task 3: return function, 1 parameter (pass by value).
Save task2 as task3 and add a loop (for) to the greeting function - after the scanf
statement.
The loop must run n times, where n is an integer passed into the function as a
parameter. Put (move) the printf statement inside the loop.
Modify the function call in the main function to pass in a hard-coded constant
(e.g.10). Don't forget to update the function prototype and function definition.
Task 4. Writing and calling functions
1. Modify the program called task4.c by adding a function for calculating the
factorial of any positive integer between 1 and 13.
int factorial(int number);
The code for calculating a factorial is:
int product = 1;
/*check input is in range*/
if (n <= 0 || n > 13)
{
printf("invalid data\n");
}
for (i = n ; i > 1 ; i--)
{
product *= i;
}
theFactorial = product;
Your final version of task4.c must print a sentinel value (-1) if the input
parameter for the function is out of range.
Use the existing code to test your function.
ICT60001
J Hamlyn-Harris, Swinburne
Page 2
ID: _________________ Name:_______________________
Tutorial Day:_________ Time: ________Room:_________
Tutor’s Name:_______________________Date: __________
5. Write the code for the main( ) in task5.c. The program contains a function
which calculates cube roots of positive and negative numbers.
Your code must call the function from inside a loop, sending the actual
parameters -27.0, -24.0, …0.0, … 27.0, (steps of 3.0) and must print the
results on the screen showing 4 decimal places in neat columns:
6. Using task6.c, static.c and random.c as a starting point, write a program
which
a. seeds the random number generator
b. runs a loop which calls a function which returns a random integer
between 1 and 100. The loop runs 1000 times.
i. checks the returned value. If the value is between 1 and 49, it
calls a function called n-counter, which increments its value
ICT60001
J Hamlyn-Harris, Swinburne
Page 3
ID: _________________ Name:_______________________
Tutorial Day:_________ Time: ________Room:_________
Tutor’s Name:_______________________Date: __________
and returns it. If the random number is between 50 and 69, the
code calls p_counter, which increments it's count and returns
it. Do the same for values in the range 60-69, 70-79 and 80100.
c. call each counter function to get the final count and print a table of
number frequencies.
Task 7. return function, 2 parameters (pass by value and pass by reference).
Look at the code in Lab8Example0. It used functions to validate input: ints and
strings. It uses pass by reference to send strings (char arrays) to functions. Try it out.
Observe how the string functions do not return the filtered string, they just change the
string after being passed the location (in the name of the string).
Save as task3 as task7. Add a new function called filterString. This will be the
prototype:
void filterString(char string[], int length);
The code inside filterString will be based on the code in Lab7Example0.
Write something like this:
char forbidden[14] = ".,<>(\");/-\\&*?";
int i, j;
for (i = 0 ; i < length ; i++)
{
for (j = 0 ; j < 14 ; j++)
{
if (string[i] == forbidden[j])
{
string[i] = '~';
}
}
}
return;
After you have the new function written call it from within greeting (between the
scanf and the for loop) to remove any strange characters from the name. You can use
strlen(name) to get the actual parameter value of length.
Don't forget to add a block comment to the new function.
Task 8. Save as Task8 and refer to slide 89 of lecture 8 (Deleting an element from an
array).
Try to add code that will remove an illegal character instead of replacing it.
Task 9. (Windows version)
ICT60001
J Hamlyn-Harris, Swinburne
Page 4
ID: _________________ Name:_______________________
Tutorial Day:_________ Time: ________Room:_________
Tutor’s Name:_______________________Date: __________
Examine the file: sorttest0.c. This fills an array with ARRAYSIZE integers. It prints
the array, calls the bubblesort and quicksort functions and then prints the (now sorted)
arrays.
Add code which times the sort.
1. Include time.h (provides the code for clock()),
2. Add variables for storing the start time in milkliseconds, stop time in
milliseconds and the duration in milliseconds
3. Just before calling bubblesort(), call clock() and store and print the time.
4. Call bubblesort()
5. Call clock() again, subtract the stored time; print the difference.
Repeat these steps for the quicksort function call.
Task 10. Examine sorttest1.c. Compare the code you found to that you wrote for
Task 9.
Add code for running a selection sort on a freshly randomised array (freshly filled
with random numbers).
The file contains hints. You will need to rename some variables.
Use the printArray() to verify that all of the sorts work on small arrays (~10)
Comment printArray() out and increases the ARRAYSIZE to 10000-100000 and
compare the times for the different algorithms.
Task 11. Examine sorttest2.c. Compare the code you found to that you wrote for
Task 10.
Add code which declares a second array on the heap (using malloc). Slide 92 of the
lecture will help. Include a guard which will terminate your program if the heap
allocation fails.
Copy the stack-based function call code (including filling the array with random
numbers) for each sort algorithm function call to near the end of the main() and
change it to use the heap-based array. Update printf messages to indicate this code
times the sorting of arrays on the heap.
Remember to free() the heap before your program ends.
Is the heap faster or slower than using the stack?
Task 12. Research the internet for other sorting algorithms. Select one which
performs better on the heap than on the stack. Try adapting this code and putting it
into your solution to Task 11. This will be one of your assignments for this unit.
ICT60001
J Hamlyn-Harris, Swinburne
Page 5
Download