15 January 2020
Fondamenti di Informatica e Laboratorio modulo A
Prof. Marco Gavanelli
15 January 2020
Exercise 1 (points 16)
An engineer has to develop a motherboard and has to decide which type of RAM memory she will use. The memories currently available on the market are listed in a di testo file memory.txt, and they are 100 at most. For each memory, the file contains the following data:
- string containing at most 20 characters, senza spazi: model
- integer: access time in ns
- integer: costin US dollars
In the previous project, the RAM memory had access time 47 ns and cost 46 $. The engineer wants to have an array listing all the memories that are better than the one currently used, i.e.
- whose access time is at most 47 ns
- whose cost is at most 46 $
Moreover, she wants to exclude those memories that have exactly the same characteristics of the current one: if she finds a memory whose access time is 47 ns and whose cost is 46 $, then she prefers to keep the memory already used also in the new version of the project.
Write a C program that shows such a list, organizing the program as follows:
- main: In the
main
function, invoke a function that reads the file memory.txt (to be implemented in step 2), one that computes the requested data (to be implemented in step 3), finally show the result through a print function (to be implemented in step 5). - read memory.txt : the read function should read the file memory.txt and copy its data into a suitable array of structures. Show on the screen the content of the array, using the print function (to be implemented in step 5).
- elaborazione: the function that short-lists those memories that are better than the old one takes as parameters
- the array read in step 2
main
an array of memories. To do so, it invokes, for each memory in the array read in step 2, a function (to be implemented in step 4) that checks if that memory is better than the one currently in use (having cost 46 $ and access time 47 ns; the field "model" can be set to any string chosen by the student) - dominates: function takes as parameters two structures of type memory and provides
- 1 if the first memory is better than the second (i.e., if the access time is lower than or equal to that of the second memory, its cost is lower than or equal to that of the second, and the inequality is strict for at least one of the fields)
- 0 otherwise
- print: the print function takes as parameters an array of memories (plus, possibly, oter parameters) and shows it on the screen.
It is necessary to organize the program into procedures and functions; it is highly recommendable to add other procedures/functions beyond those explicitly given in the text.
Exercise 2 (points 4)
For another project, the engineer needs to choose a RAM memory, and she is still undecided whether to prefer a fast but expensive motherboard or to develop a a cheap but slow one. She wants to know which memories she should take into consideration. Clearly, it is meaningless to consider a memory if there is another one which is better both as cost and as access time.
More in detail: a memory M is dominated by a memory N if
- the access time of N is lower than or equal to the access time of M
- the cost of N is lower than or equal to the cost of M
- for at least one of the two criteria, the strict inequality holds (i.e., either the access time of N is lower than that of M, or the cost of N is lower than the cost of M)
note that it is the same condition of the function developed in step 4, thus it makes sense to reuse that function.
The program should create an array of memories that are nondominated, and the main
function should invoke the print function (implemented in step 5) to print this array.
It is mandatory to organize the program in suitable functions, in particular adding a significant number of functions with respect to exercise 1.
If the student does only exercise 1, (s)he can write all the exercise in one c file.
If (s)he does also exercise 2, then (s)he should provide the following file:
- a file
SURNAME.c
(where SURNAME is to be substituted with the surname of the student) containing themain
and the functions that are use only in exercise 1 - a file
facoltativo.c
containing themain
function and those functions used only in exercise 2 - a file
funzioni.c
that contains the functions used in both exercises
plus all the header files that the student believes are necessary.
When correcting the program, the professor will create two projects: