You are on page 1of 4

UNIVERSIDAD CARLOS III DE MADRID

PROGRAMMING. . Bachelor in Industrial Technology Engineering


FINAL EXAM. JUNE 2018.
Surname(s): ______________________________________
Name: _______________________________________

Problem 1

In this problem, you are going to design a C program to manage the information about the results of the
World Motorcycle Championship in the Moto GP category
Up to 23 pilots participate in this Championship, and the season (temporada) consists of 18 races in 18
different circuits
Prior to the race, a qualifying practice session (entrenamientos oficiales) takes place. The qualifying times in
this session determine the exit order of the pilots for the race itself. The best pilot in the qualifying session
earns the pole position (first exit position in the race).
After each race, the pilots in the 18 first positions earn points depending on their placement: the 1st pilot is
awarded 18 points, the 2nd 17, and so on. Pilots in position 19 and lower get 0 points.
The points earned in each race add up to determine the ranking (order of pilots). At the end of the season,
the first pilot in the ranking is awarded the Championship.

The program must


- Store the information regarding the pilots participating in the Championship
- Store the results of each circuit, comprising results from the qualifying session and the race itself
- Compute the score of each pilot in the Championship and determine the ranking of pilots
To develop the program, consider that the following constants have already been declared: MAX_PILOTS
(23), MAX_CIRCUITS (18), MAX_POINTS (18) AND STRINGLENGTH (maximum length of
strings used in the program)
Implement the following sections:
1. [1 point] Define and declare the data structures needed to store the information regarding pilots
and circuits
The information to store regarding a pilot is the following:
• Name: String of 30 characters containing name and surname of the pilot
• Number: Number displayed on the motorbike (dorsal)
• Country: Country of origin of the pilot
• Team: Name of the team
• Score: Number of points earned by the pilot
• Number of races won
• Number of pole positions: Number of times the pilot was first in the qualifying session
• The information to store regarding a circuit is the following:
• Name: String containing the name of the circuit
• Race results: Vector containing the number (dorsal) of the pilots in order of arrival
• Qualifying results: Vector containing the number (dorsal) of the pilots in order of arrival in
the qualifying session
2. [2 points] Write a function named signupPilots that will get the data of all the pilots
participating in the season. For each pilot, the function will query the user for a name, number
(dorsal), country and team. When reading the number (dorsal) you must check there isn’t a pilot
already using that number. If this were the case, ask for a new number, until it’s valid.
After adding a pilot, the program will ask if the user wants to add more pilots, taking into account
that the maximum number of pilots is 23.
The function must return the number of pilots, according to the following header
int signupPilots (struct typePilot pilots[]);
3. [2.5 points] Write a function named recordResultsCircuits that for each circuit will ask the
user for the position of each pilot - identified by its number (dorsal) - both in the race and in the
qualifying session. Using these data, update the information of the circuit (race results vector, and
qualifying session results vector). Update as well the data regarding the pilots (points earned, races
won and poles obtained). After reading the results for a circuit the program will ask the user if he or
she wants to add the results for more circuits. Remember the maximum number of circuits is 18.
The function must have the following header
void recordResultsCircuits (struct typeCircuit circuits[],
struct typePilot pilots[], int numPilots, int *numCircuits);

4. [3 points] Write a function named displayRanking that displays the result of each of the
pilots and the following information
• The World Champion (pilot who won the World Championship) and his score
• The pilot with the most victories (pilot who won the most races), and number of races won
• The pilot with the most pole positions and number pole positions
The function must have the following header
void displayRanking (struct typePilot pilots [],int numPilots);
This is an example of the expected display:
NUMBER NAME SCORE RACES WON POLES
99 Jorge Lorenzo 34 0 1
94 Jonas Folger 32 0 0
93 Marc Marquez 36 2 0
The world champion is:
Marc Marquez. Number: 93. Team: Honda. Country: Spain. Score: 36
The pilot who won the most races is:
Marc Marquez. Number: 93. Team: Honda. Country: Spain. Victories: 2
The pilot with the most pole positions:
Jorge Lorenzo. Number: 99. Team: Ducati. Country: Spain. Poles: 1
To solve this section you can assume you have a function which displays the name, number, team
and country of a pilot, with the following header
void displayPilot (struct typePilot pilot);

5. [1.5 puntos] Write the main function doing the following:


• Declare and initialize the variables the program needs.
• Ask the user the information of the pilots that are signed up for the championship, and display
how many pilots there are.
• Ask the user to enter the results of those pilots in as many circuits he or she wants
• Display the number of circuits for which the user has entered results
• Display the final ranking of pilots according to the results provided by the user, as well as the
World Champion, the player with the most victories and the one with the most pole positions.
Problem 2.
In this exercise we’re going to program the control system for a greenhouse. The system can water a
greenhouse sized 20 x 30 meters or less, installing a moisture sensor per square meter.
(greenhouse = invernadero; to water=regar; moisture = humedad);
1. Function main: 1 point
Write the program’s main function, which will display a menu with two options. Option 1 is the configuration
of the system, while Option 2 is starting the control mode. Each of the two options will be implemented with a
single function, being the function’s CALLS the following
configure (sensorCfg, &numRows, &numCols);
control (sensorCfg, numRows, numCols);
Where sensorCfg is a two dimension array storing the code of the sensor installed at each position (a
position is defined by a row and a column), and numRows and numCols the actual number of rows and
columns of the greenhouse.

2. Function configure: 1.5 points


Now you will develop the function configure that will configure the sensors for a specific greenhouse
using information provided by the user. The configuration consists of storing the code of the sensor that is
placed at a position in the greenhouse.
To do this, the function will first ask the user for the size of the greenhouse, that is, how may rows and
columns (of 1 meter size) it has. To prevent errors, the program must guarantee that the number of rows and
columns entered is valid. Furthermore, due to the design of the system, the number of rows has to be even.
After the size of the greenhouse is read, the function will ask the user for the code of the sensor (an integer
number) installed at the position corresponding to each row and column.

3. Function findMean: 1.5 points


For section 4, we will need a function having as input parameter a two dimensional array and a position in
the array (a point, identified by a row and column). The function will return the mean of the values
surrounding that position (above, below, to the left, to the right). The function will get a fourth parameter,
named type. If type is 1, the mean will be computed excluding the point itself, whereas if type is 2 the point
will be included. Develop this function according to the following header, and assuming the point will never
be in the outermost rows and columns of the array (valores en los bordes exteriores del array)
float findMean (float m [MAXR][MAXC] ,int r, int c, int type) ;

4. Function control: 4 points


Now you are going to develop the control function (control). This function will repeat a loop that will only
end if a physical stop button is pressed. This button is read as another sensor of the system, as described
later. The loop starts reading the moisture sensor data, then it activates the watering for each position, and
finally it checks if the stop button was pressed. Details on each of these steps follow.
To read the moisture data, use the function provided by the watering system library scanSensor (assume
it’s already implemented). This function returns the reading of a sensor using its code as input parameter.
Remember the code corresponding to a row and column is stored in the sensorCfg array .
float scanSensor(int code);
After getting all the sensor readings, you have to verify the values to correct wrong readings. When a
sensor loses contact with the surrounding soil, the reading returned is wrong, but it can be identified because
it will be abnormally low. Therefore, before watering you need to find and replace wrong readings according
to the following: if a sensor returns a reading that is 20% lower than the adjacent sensors, the moisture value
is replaced by the mean of the sensor plus the values of the adjacent sensors. To simplify the problem,
assume that the outermost sensors (first row, first column, last row, and last column) will never be wrong.
The control loop continues with the watering of the plants. To do this, call function water that takes as
parameters the moisture (the array read in the previous step) and the number of rows and columns of the
greenhouse, according to the following header. You will implement this function later, in section 5.
void water(float moisture[MAXR][MAXC],int nRows,int nCols);
The last step of the control loop is to check if the stop button has been pressed, using function
scanSensor, and passing as parameters the code of the button, which is code 21. The return value will be
1 if the button was pressed and 0 otherwise. For example, the call to this function will be
stop= scanSensor(21);
5. Function water. 1 point
Implement the function water used in the previous section, and according to the header provided. To water
a point, you must open a valve using a function from the system library (openIrrigationValve –assume it’s
already implemented),
void openIrrigationValve(int r, int c, float flow);
where flow is computed using the moisture at that point read from the sensors, according to the expression
100.0- (moisture[r][c] * FLOWCTN), and being FLOWCTN a constant defined in the watering system’s
library.
6. Function water – advanced version: 1 point
Now, let’s modify the water function to adapt the amount of water provided at a point to the real needs of the
plants. Assume there are six possible different plant types (1,2,3,4,5,6) and that for each plant type the water
flow is computed multiplying by a factor that depends on the type of plant, instead of multiplying by constant
FLOWCTN. Those factors are stored in a vector, waterNeedsPerType
float waterNeedsPerType ={2.32, 3.17, 4.53, 2.17, 12.0, 3.45};
Likewise, the array int plantType [MAXR][MAXC]contains a number (between 1 to 6) identifying the type
of plant at each position. Specify the changes needed in function water to get this behaviour. You don’t
need to rewrite the whole function, nor declare, initialize or read values for plantType and
waterNeedsPerType. Assume they are already declared and initialized.

You might also like