Week 09 Laboratory Exercises
Objectives
- using memory allocation
- working with structs and pointers
- introducing linked lists
Activities To Be Completed
This lab is worth a total of 1.4 marks and consists of the following activities:
| Type | Activities | Weight |
|---|---|---|
| One-dot (●◌◌) |
my_scanf mallocd_array list_print list_length |
15% |
| Two-dot (●●◌) |
list_insert_head |
15% |
| Additional |
debug_concatenate |
Not worth marks |
| Group presentation |
presentation9 |
70% |
Lab exercises are capped at 15 marks. For more details, see the course outline.
Exercise
(●◌◌)
:
My Scanf
Download my_scanf.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity my_scanf
Ben Briant has decided to learn how scanf() works, and to help with his
understanding, he has decided to write his own simple scanf functions!
They are much simpler than a regular scanf, as they only allow for scanning in
a single integer or a single double (depending on if my_scanf_int() or
my_scanf_double() is being called) at a time.
Both of these functions should:
- take in a pointer to a variable that we want to scan our value into,
- read a value from the user (using regular scanf), then
- set the value of the input to be the value read from the user.
Unfortunately, Ben got overwhelmed with all the pointer syntax (* and &),
so decided to just avoid writing any of it! Your job is to put all the * and
& in the correct spots in the code.
You will have to edit both the main() function, my_scanf_int() and
my_scanf_double(), But you can't change any part of the code aside
from adding * or &.
Examples
dcc my_scanf.c -o my_scanf ./my_scanf Enter the amount of study you need to do this week (in decimal): 7.5 Enter the number of days you have free: 3 You have on average 2.50 hour(s) each free day to do homework.
Testing
When testing your program, we are checking more than that it simply produces the correct output. We are also testing that the functions work correctly individually (by replacing your main function with one of our own).
1091 style my_scanf.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest my_scanf
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1091 lab09_my_scanf my_scanf.c
You must run give
before Monday 23 March 09:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise
(●◌◌)
:
Working with malloc'd arrays
Download mallocd_array.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity mallocd_array
Your task is to add code to these functions in mallocd_array.c:
// A function which scans in `size` integers and stores them into a
// malloc'd array.
// returns: a pointer to the malloc'd array
int *scan_array(int size) {
// TODO: 1. create a MALLOC'd array of size `size`
// TODO: 2. Loop through and scan values into the array.
// TODO 3.delete the following line and return the array.
return NULL;
}
// Given an integer array and it's size,
// returns the sum of all elements inside the array, divided by the size of the
// array.
double calculate_average(int *array, int size) {
// TODO calculate the sum of the array.
return 0;
}
Complete the C program, mallocd_array.c.
The main function has already been written for you. You must not modify it in any way.
Examples
dcc mallocd_array.c -o mallocd_array ./mallocd_array Enter size: 5 Enter 5 integers: 1 2 3 4 5 The average of all values in the array is: 3.00 ./mallocd_array Enter size: 1 Enter 1 integers: 10 The average of all values in the array is: 10.00 ./mallocd_array Enter size: 3 Enter 3 integers: 5 -4 -3 The average of all values in the array is: -0.67
Assumptions/Restrictions/Clarifications
- You do not need to handle memory leaks for this exercise. i.e. You should not
call
free - All inputs be of the correct type
1091 style mallocd_array.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest mallocd_array
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1091 lab09_mallocd_array mallocd_array.c
You must run give
before Monday 23 March 09:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise
(●◌◌)
:
Print out the elements of a Linked List
Download list_print.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity list_print
Your task is to add code to this function in list_print.c:
// print a linked list in this format:
// 17 -> 34 -> 51 -> 68 -> X
void print(struct node *head) {
// PUT YOUR CODE HERE
}
print is given one argument, head, which is the pointer to the first node
in a linked list.
Your program will be supplied up to 50 integers, which will be put into a
linked list that is passed to the print.
Add code to print so that it prints the elements in the list.
For example, if the linked list contains these 8 elements:
1, 7, 8, 9, 13, 19, 21, 42
print should print
1 -> 7 -> 8 -> 9 -> 13 -> 19 -> 21 -> 42 -> X
(including the X)
Testing
list_print.c also contains a main function which allows you to test your
print function.
This main function:
- converts the input from terminal into a linked list
- assigns a pointer to the first node in the linked list to
head - calls
list_print(head)
Do not change this function. If you want to change it, you have misread the question.
Your list_print function will be called directly in marking. The main
function is only to let you test your list_print function.
Examples
dcc list_print.c -o list_print ./list_print 1 2 4 8 16 32 64 128 256 1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64 -> 128 -> 256 -> X ./list_print 2 4 6 5 8 9 2 -> 4 -> 6 -> 5 -> 8 -> 9 -> X ./list_print 42 4 42 -> 4 -> X ./list_print 43 43 -> X ./list_print X
Assumptions/Restrictions/Clarifications
- You can assume the input will never be more than 50 integers.
printshould not change the linked list it is given. Your function should not change the next or data fields of list nodes.printshould not use arrays.printshould not call malloc.printshould not call scanf (or getchar or fgets).- Do not change the supplied
mainfunction. It will not be tested or marked.
1091 style list_print.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest list_print
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1091 lab09_list_print list_print.c
You must run give
before Monday 23 March 09:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise
(●◌◌)
:
Find the length of a Linked List
Download list_length.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity list_length
Your task is to add code to this function in list_length.c:
// Return the length of the linked list pointed by head
int length(struct node *head) {
// PUT YOUR CODE HERE (change the next line!)
return 42;
}
For this exercise, you will be given a linked list nodes containing integers, shown below.
struct node {
struct node *next;
int data;
};
Your job is to complete the length function.
length is given one argument, head, which is the pointer to the first node
in a linked list.
Add code to length so that its returns the length of the list.
For example if the linked list contains these 8 elements:
1, 7, 8, 9, 13, 19, 21, 42
length should return 8.
Testing
list_length.c also contains a main function which allows you to test your
length function.
This main function:
- scans in number of items in the list, and its values; to create a linked list,
- assigns a pointer to the first node in the linked list to
head, - calls
list_length(head), then - prints the result.
Do not change this function. If you want to change it, you have misread the question.
Your list_length function will be called directly in marking. The main
function is only to let you test your list_length function
Examples
dcc list_length.c -o list_length ./list_length How many numbers in initial list?: 9 1 2 3 6 5 4 9 9 0 Counted 9 elements in linked list. ./list_length How many numbers in initial list?: 6 1 2 3 6 5 4 Counted 6 elements in linked list. ./list_length How many numbers in initial list?: 5 1 2 3 4 5 Counted 5 elements in linked list. ./list_length How many numbers in initial list?: 2 42 4 Counted 2 elements in linked list. ./list_length How many numbers in initial list?: 0 Counted 0 elements in linked list.
Assumptions/Restrictions/Clarifications
lengthshould return a single integerlengthshould not change the linked list it is given- Your function should not change the next or data fields of list nodes
lengthshould not use arrayslengthshould not callmalloclengthshould not call scanf (orgetcharorfgets)lengthshould not print anything. It should not callprintf- Do not change the supplied
mainfunction. It will not be tested or marked
1091 style list_length.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest list_length
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1091 lab09_list_length list_length.c
You must run give
before Monday 23 March 09:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise
(●●◌)
:
Insert an element at the head of a Linked List
Download list_insert_head.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity list_insert_head
Your task is to add code to this function in list_insert_head.c:
// WRITE YOUR CODE INSIDE HERE ONLY!!!
// Insert a new node containing value at the start of the linked list.
// The head of the new list is returned.
struct node *insert_head(int value, struct node *head) {
// PUT YOUR CODE HERE (change the next line!)
return NULL;
}
insert_head is given two arguments, value and head.
valueis anintheadis the pointer to the first node in a linked list
Add code to insert_head so that it creates a new list node (using malloc)
containing value and places it at the start of the list.
insert_head should return a pointer to the new list.
For example if value is 12 and the linked list contains these 3 elements:
16, 7, 8
insert_head should return a pointer to a list with these elements:
12, 16, 7, 8
Testing
list_insert_head.c also contains a main function which allows you to test
your insert_head function.
This main function:
- converts the first set of read integers to a linked list,
- assigns a pointer to the first node in the linked list to
head - reads another single integer from standard input and assigns it to
value - calls
insert_head(value, head) - prints the result.
Do not change this function. If you want to change it, you have misread the question.
Your insert_head function will be called directly in marking. The main
function is only to let you test your insert_head function
dcc list_insert_head.c -o list_insert_head ./list_insert_head How many numbers in initial list?: 3 16 7 8 Enter number to insert to head: 12 [12, 16, 7, 8] ./list_insert_head How many numbers in initial list?: 1 16 Enter number to insert to head: 42 [42, 16] ./list_insert_head How many numbers in initial list?: 0 Enter number to insert to head: 2 [2]
Assumptions/Restrictions/Clarifications
insert_headshould not use arraysinsert_headshould not call scanf (orgetcharorfgets)insert_headshould not print anything. It should not callprintf- Do not change the supplied
mainfunction. It will not be tested or marked
1091 style list_insert_head.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest list_insert_head
When you are finished working on this exercise,
you must
submit your work by running give:
give dp1091 lab09_list_insert_head list_insert_head.c
You must run give
before Monday 23 March 09:00
to obtain the marks for this lab exercise.
Note that this is an individual exercise,
the work you submit with give must be entirely your own.
Exercise — individual:
(Not For Marks) Debugging - concatenate
Download debug_concatenate.c here
Or, copy these file(s) to your CSE account using the following command:
1091 fetch-activity debug_concatenate
Note that this exercise is not marked or worth marks!
Debugging Tips!
Some debugging tips for you:
- dcc output - as you run into issues, dcc will point you to where the errors are. Remember that dcc gives you the line number the issue is on, and will give some sort of explanation. Make sure you read everything dcc gives you. Sometimes we get “errors carried forward”, so find your first error, fix that, then recompile.
- print statements - sometimes it can be handy to see if the flow of your code puts you in the spot you expect it to be (ie. inside the right if statement, or going through a loop the correct amount of times). A quick way you can check this is by putting print statements in your code for testing purposes, like
"the value of x is %d and y is %d". This lets you check that you got against what you expected. - DPST1091 debugging guide
The Task
This exercise concatenate the strings passed as command-line arguments excluding the filename. Currently it has some issues - it is your job to figure them out and fix the code. Additionally, think of ways you could refactor the starter code to produce a simpler solution.
Examples
dcc debug_concatenate.c -o debug_concatenate ./debug_concatenate A blue whales heartbeat can be heard over 2 miles away Concatenated string: Abluewhalesheartbeatcanbeheardover2milesaway ./debug_concatenate The moon has moonquakes Concatenated string: Themoonhasmoonquakes ./debug_concatenate Pigs cannot look up into the sky Concatenated string: Pigscannotlookupintothesky ./debug_concatenate Concatenated string:
Clarifications
- The maximum length of the resulting string is 1028 characters, including the null terminator.
1091 style debug_concatenate.c
When you think your program is working,
you can use autotest
to run some simple automated tests:
1091 autotest debug_concatenate
In-Class Exercise — group:
Presentation
This week during lab class, you will be divided into small groups and assigned an activity from this week to give a short presentation on (2–5 minutes per group).
There is no give or autotest for the group presentation.
Submission
give.
You can run give multiple times.
Only your last submission will be marked.
Don't submit any exercises you haven't attempted.
If you are working at home, you may find it more convenient to upload your work via give's web interface.
Remember you have until Week 10 Monday 9:00am to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline,
using test cases different to those autotest runs for you.
(Hint: do your own testing as well as running autotest.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.
Lab Marks
When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:
1091 classrun -sturec
Voice of the Student
✨ Help Us Improve Your Learning Experience ✨
Your feedback helps us understand what’s working well and what might need improvement.
This quick, anonymous check-in has just two questions and takes less than a minute to complete.
Please answer honestly — your input makes a real difference.