Systems Programming (XMUT)
Exercise 3: Linked Lists, File I/O and More
Due 1 December 7 pm
Resources and links
-
activity1.c
-
activity2.c
-
activity3.c
Do not rename these files.
Objective
This exercise aims to write and debug programs using linked lists, file stream input/output, and command-line arguments.
Activity 1: Linked List [30 Marks]
Copy and paste the following C program to your favourite text editor (You can extract a copy of this file from
Zip Files):
#include <stdio.h>
// (1) Complete linked list node declaration below.
// Do not change the name of the structure.
struct node {
};
int main(void)
{
struct node *head = NULL; // head points to the head of list
struct node *tmp;
// This for-loop will build a list
for(int i = 0; i < 10; i++) {
// (2) Use malloc() or calloc() to allocate memory for one node
// and let tmp point the the allocated memory.
// (3) Assign 100*(i+1) to data and let next point to NULL.
// (4) Append tmp to the list pointed by head
}
struct node *rover = head;
while(rover) {
printf("%d ", rover->data);
rover = rover->next;
}
printf("\n");
return 0;
}
Save the file as
activity1.c
. Study the source file and do the following within the file:
- Define a structure named
node
to represent a singly linked list node. The structure should have two parameters
-
data
: an integer
-
next
: pointer to next node in the list
- Use either
malloc()
or calloc()
to allocate memory for one node and let tmp
point the the allocated memory.
- Assign
100*(i+1)
to the data
member of the node and let the next
member point to NULL
.
- Append the newly created node pointed by
tmp
to the list pointed by head
. (Append means insert after the current last element in the list.)
Your program should not output anything aside from the output generated by
printf()
in lines 26 and 29.
Compile and run the program. If you are happy with the program, submit it to the Assessment System for marking.
Activity 2: Text File Processing 2 [30 Marks]
In this activity, you will perform simple text file processing using file stream I/O. Suppose you are given a text file named
raw.txt
, which contains 2 decimal numbers per line:
12 134
100 94
23 324
10 45
Copy and paste the following C program. to your favourite text editor (You can extract a copy of this file from
Zip Files):
#include <stdio.h>
int main(void)
{
int a, b;
FILE *in; // use for handling input file
FILE *out; // use for handling output file
// Open raw.txt for reading
// Open processed.txt for writing
// Go thru raw.txt file and generate processed.txt file accordingly
return 0;
}
Save the file as
activity2.c
. Use
activity2.c
as a starting point to write a program that will add the numbers in every line of
raw.txt
and output the numbers together with the sum to a file named
processed.txt
. The contents of
processed.txt
should look like this:
12 134 146
100 94 194
23 324 347
10 45 55
You can use the file
raw.txt
included in
Zip Files to test your code. If you are happy with the program, submit it to the Assessment System for marking.
Hint: If you are using
fscanf()
to scan the contents of the file, you may want to pay close attention to its return value.
Activity 3: Command-Line Arguments [40 Marks]
In this activity, you will write a C program that accepts an arbitrary number of command-line arguments. The program should treat every command-line argument (excluding the program name itself) as an integer. The program should add all the integers and print the sum to the display. For instance, suppose you compile the program into an executable file named
activity3
, then if the program is executed as
./activity3 1 2 3 4 5 6
the output should be
21
If the program is executed as
./activity3
the output should be
0
Save the program as
activity3.c
. To have an easier time with the auto-marking script, make sure that the output only consists of the sum followed by a newline character.
If you are happy with the program, submit it to the Assessment System for marking.
Hint: You may use
sscanf()
to convert a string to an integer.