# Computer programming - Lab 12b: Structures

1. Rational numbers Define a structure type for rational numbers, holding numerator and denominator. Implement functions to add and multiply rationals. Return results that are in canonical form (denominator is positive and gcd with numerator is 1).

2. 2D vectors Define a structure type for vectors in 2-dimensional space. Define functions for addition, subtraction and magnitude, and to read a vector (space-separated real coordinates of endpoint, assuming zero origin). Use this function to compute the total length of the segments drawn in the path d="..." element read from a SVG file. The path keeps a current point, and the commands are of the form letter x y, with L/l for "line to" and M/m for "move", uppercase for absolute coordinates, and lowecase for relative.

3. Polynomials Define a structure type for polynomials, containing the degree and an array of real coefficients. Write a function that prints a polynomial (in X) and one that adds two polynomials, returning a dynamically allocated result.

4. Big numbers Define a structure type for big numbers, represented in binary, using an arbitrary number of bytes (stored as a structure field). Write a function that takes two big numbers and returns their sum, as a dynamically allocated structure.

5.Continued fractions A real number x can be successively approximated in a continued fraction as a0, a0 + 1/a1, a0 + 1/(a1+1/a2), a0 + 1/(a1+1/(a2+1/a3)), ... where a0 is an integer, and a1, a2, a3, ... are positive naturals. In the initial approximation, a0 is ⌊x⌋. The remaining fractional part is either 0 (we stop), or a number 0 < x1 < 1, so 1/x1 > 1 and we continue applying the same process, taking the integer part, etc. One obtains a sequence of rational numbers p0/q0 (= a0), p1/q1 (= a0 + 1/a1), p2/q2, ... that approximate x increasingly well, alternating from above and below.
Define a structure that represents a rational number and compute (recursively) the nth rational approximation of a real number x using a continued fraction.

6.Prime factors. Define a data structure to represent an unsigned number as a product of powers of primes. Write a function that computes the product of two numbers, in the same representation.

7. CSV files A .csv file contains the grade situation for a class of students. Each line contains fields separated by commas. The first field is the student name, followed by up to 10 fields with real-numbered grades or other characters; any remainder of the line is ignored.
Read the file using an appropriate data structure; you may assume there are at most 200 students. Compute averages for the students that have 10 passing grades. Sort students in decreasing order of their grade averages; break ties in alphabetical order. Print the sorted list with grade averages.

8. Sorting text Design a data structure that stores a line of text, together with its length (in characters) and number of words. Read all lines of a file named on the command line into an array of such structures. Sort and print the file with lines in increasing order of length, breaking ties first in decreasing order of words and then in alphabetical order.

9. List ZIP file contents Write a program that lists the names of the files from a .zip archive (whose name is given on the commandline) in decreasing order of their uncompressed size. Here is a zip archive of sample programs written this semester.
The .zip file format is defined here. We will handle a simplified case; additional checks should be added for a robust program.
All integer values are stored in binary, in little-endian byte order. Since the x86 architecture is little-endian, they will be placed in memory in the correct byte order when read with fread.

• First, obtain information about the file contents from the end of central directory record (sec. 4.3.16). For a .zip file with no comments, these are the last 22 bytes of the file. They contain the following relevant information:
```Offset	Byte count	Meaning
-------------------------------
10      2		number of entries in central directory (list of files)
12	4		size of central directory
16	4		offset of central directory within zipfile
```
• Next, read the central directory (after seeking to the appropriate offset). For each archived file, it contains a fixed part (of 46 bytes) and a variable part (including file name), with the following layout
```Offset	Byte count	Meaning
-------------------------------
12	2		file modification time
14	2		file modification date
24	4		uncompressed file size
28	2		file name length: fnlen
30	2		extra field length: xlen
32	2		comment length: clen
46	fnlen		file name (not null-terminated)
xlen		extra field
clen		comment
```
• Create an array of structures which contain the relevant information for each file (at the minimum, file name, file name length and uncompressed size). Sort and display the files in decreasing order of size.
• Optionally, represent time (of the last file modification). In the directory, this is represented using bits, as follows:
• time: seconds (0-29, with granularity multiple of 2 seconds), then minutes and hours
• date: day, month, year (as difference from starting year 1980)
You can use bitfields or bit operations to work with these values.

Marius Minea
Last modified: Mon Dec 11 9:45:00 EET 2017