程序代写CS代考 Java concurrency algorithm SOFT3410 Tutorial 2 Standard Library, Files and Function Pointers

SOFT3410 Tutorial 2 Standard Library, Files and Function Pointers
Question 1: Files
You are able to access files on your system through C’s standard library functions and types. The FILE type is a portable abstraction for interfacing with files and streams on your system. Once opened,youcanusefunctionslikefwrite, fgets, fread, fputs, …toreadorwrite.
int main() {
FILE* f = fopen(“numbers.txt”, “r”); char buf[512];
while(fgets(buf, 512, f)) {
printf(“%s”, buf);
}
fclose(f); }
Write an application that will generate a list of numbers and write them to a file. Use the rand function from stdlib.h to do this.
Please refer to the man pages when looking up library functions and system calls
Question 2: Limit
Construct a program that will accept three command line arguments. The first argument specifies if the program will output the lines from the beginning or end of the file. The second argument specifies the number of lines it will output. The last argument is the file name, which you will open and read from.
You can use the following as a basis
• Solve for only reading the first n lines
• Rewrite your solution to not use fgets, instead create a function to read character by character and check for a new line
• Change your logic to allow for reading from the end of the file Note: Do not use a variable length array.
1

SOFT3410 Standard Library, Files and Function Pointers
Question 3: Word Counting
You have been tasked with recreating the wc unix tool. This will need to accept a file and count the number of characters, words and lines contained within it. You can assume that each character is ASCII encoded.
1 ./wc /usr/share/dict/words
You can consider a word as any string between spaces. The escape character sequence for a line is
\n.
Your application should have a similar output format to wc.
1 ./wc words.txt
2 1068 356 100
Construct some test cases to make sure your logic is correct and counts the correct number of charac- ters, words and lines.
Question 4: Stitching
Your program will be given a set of command line arguments, each argument representing a file path. You will need to use each argument to open a file and print the contents to a column. Each column should be separated with a tab. This program is to operate like the paste command.
Example output with the following command
1 ./stitch stock.txt prices.txt
1 Lemon 3.00
2 Apple 2.50
3 Soda 4.50
4 Orange 1.75
Note: Try to avoid using dynamic allocation for this problem.
Question 5: Function pointers
Pointers are very useful creatures, on top of providing a form of indirection to data, it can be used to store the address of a function. By now you should be familiar with pointers, pointing to data. However we have access to function pointers in C. You can somewhat relate this to an interface in Java.
#include
int multiply(int x, int y) { return x * y;
}
Concurrency
Page 2 of 3

SOFT3410 Standard Library, Files and Function Pointers
int add(int x, int y) { return x + y;
}
int main() {
int (*fn)(int, int) = multiply; printf(“%d\n”, fn(2, 5));
fn = add;
printf(“%d\n”, fn(2, 5));
return 0; }
The above example shows that we are able to create a pointer and specify the type to match a functions signature. Given that the functions multiple and add have matching signatures, we are able to assign a function’s address to the variable fn and invoke it.
• When would you use a function pointers?
• Construct a function that will accept a function pointer
• Discusswithyourtutorthefunctionyouhavecreatedandwhyyouhaveusedafunctionpointer
Question 6: Sorting (Again)
Adapting your sorting solution from last week, generalise the sorting function to allow any type of data to be sorted by your algorithm. Consider how you would use a function pointer in this instance.
Refer to qsort and merge_sort from stdlib for inspiration.
Note: Typically you would call malloc to allocate a temporary amount of space for swapping items.
However, you may assume the largest object is 4KB and reserve this space.
Concurrency Page 3 of 3