Update PR3

This commit is contained in:
Manuel Vergara 2024-05-22 07:08:12 +02:00
parent 036bb9d949
commit fbaedb6630
3 changed files with 212 additions and 0 deletions

View File

@ -22,6 +22,7 @@
- [16.7 Tabla de tipos estructurados](#167-tabla-de-tipos-estructurados)
- [16.7.1 Tablas de tuplas](#1671-tablas-de-tuplas)
- [16.7.2 Tabla de tablas](#1672-tabla-de-tablas)
- [16.8 Ejercicios](#168-ejercicios)
@ -1192,6 +1193,188 @@ Comentarios:
### 16.7.2 Tabla de tablas
En este caso vamos a definir otro tipo estructurado. Para facilitar el uso vamos a mantener la información de todos los cines en una única variable. Definimos una tabla donde cada elemento sea una tabla con la recaudación de las salas de cada cine.
```alg
const
MAX_THEATERS: integer = 20;
MAX_MOVIES: integer = 15;
end const
type
{declaration of a type for dates}
tDate = record
day: integer;
month: integer;
year: integer;
end record;
{declaration of type for theater information}
tTheaterDescription = record
name: string;
capacity: integer;
collect: real;
date: tDate;
end record;
{table with the information of each theater}
tTheater = record
movieTheaters: vector[MAX_THEATERS] of tTheaterDescription;
numTheaters: integer;
end record;
{declaration of a table of tTheater}
tCompany = record
movies: vector[MAX_MOVIES] of tTheater;
numMovies: integer;
end record;
end type
algorithm movieTheater
var
theMovieCompany: tCompany;
m, k: integer;
end var
{we can access the information in the folowing way}
{to access to the name of the third theater of the first movie}
theMovieCompany.movies[1].movieTheaters[3].name;
{to access to the field collect of the k th theater of the m th movie}
theMovieCompany.movies[m].movieTheaters[k].collect;
{to access to the capacity of the last theater of the last movie}
theMovieCompany.movies[theMovieCompany.numMovies].movieTheaters[theMovieCompany.movies[theMovieCompany.numMovies].numTheaters].capacity;
end algorithm
```
```c
#include <stdio.h>
#define MAX_THEATERS 20
#define MAX_MOVIES 15
typedef struct {
int day;
int month;
int year;
} tDate;
typedef struct {
tString name;
int capacity;
float collect;
tDate date;
} tTheaterDescription;
typedef struct {
tTheaterDescription movieTheaters[MAX_THEATERS];
int numTheaters;
} tTheater;
/* declaration of a table of tTheater */
typedef struct {
tTheater movies[MAX_MOVIES];
int numMovies;
} tCompany;
int main(int argc, char** argv) {
tCompany theMovieCompany;
int m, k;
/* we can access the information in the folowing way */
/* to access to the name of the third theater of the first movie */
theMovieCompany.movies[0].movieTheaters[2].name;
/* to access to the field collect of the k th theater of the m th movie */
theMovieCompany.movies[m-1].movieTheaters[k-1].collect;
/* to access to the capacity of the last theater of the last movie */
theMovieCompany.movies[theMovieCompany.numMovies-1].movieTheaters[theMovieCompany.movies[theMovieCompany.numMovies-1].numTheaters-1].capacity;
return 0;
}
```
## 16.8 Ejercicios
- **Ejercicio 1**: Dada una tabla de reales, implementa un algoritmo que calcule la media de los valores de la tabla.
- **Ejercicio 2**: Dada una tabla de reales, implementa un algoritmo que calcule el valor máximo de la tabla.
- **Ejercicio 3**: Dada una tabla de reales, implementa un algoritmo que calcule el valor mínimo de la tabla.
- **Ejercicio 4**: Dada una tabla de reales, implementa un algoritmo que calcule la suma de los valores de la tabla.
Tabla de reales:
```
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 -1.0
```
Soluciones:
- **Ejercicio 1**:
```alg
type
tReal = record
collect: vector[10] of real;
position: integer;
end record
end type
action mediaValores(in tabla: tReal)
var
suma: real;
i: integer;
end var
suma:= 0;
for i:= 1 to tabla.position do
suma:= suma + tabla.collect[i];
end for
writeReal(suma / tabla.position);
end action
algorithm simulation
var
tabla: tReal;
end var
tabla.collect[1]:= 1.0;
tabla.collect[2]:= 2.0;
tabla.collect[3]:= 3.0;
tabla.collect[4]:= 4.0;
tabla.collect[5]:= 5.0;
tabla.collect[6]:= 6.0;
tabla.collect[7]:= 7.0;
tabla.collect[8]:= 8.0;
tabla.collect[9]:= 9.0;
tabla.collect[10]:= 10.0;
tabla.position:= 10;
mediaValores(tabla);
end algorithm
```
[Solución en c](./soluciones_c/solucion1/media.c)
- **Ejercicio 2**:
- **Ejercicio 3**:
- **Ejercicio 4**:

Binary file not shown.

View File

@ -0,0 +1,29 @@
#include <stdio.h>
typedef struct
{
float collect[10];
int position;
} tReal;
void mediaValores(tReal *tabla)
{
float suma = 0;
int i;
for (i = 0; i < tabla->position; i++)
{
suma += tabla->collect[i];
}
printf("La media de los valores de la tabla es: %f\n", suma / tabla->position);
}
int main(int argc, char **argv)
{
tReal tabla = {{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}, 10};
mediaValores(&tabla);
return 0;
}