From fbaedb66302b5a7ea5b6fb6372df5f078593b65e Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Wed, 22 May 2024 07:08:12 +0200 Subject: [PATCH] Update PR3 --- fundamentos-programacion/PR3/README.md | 183 ++++++++++++++++++ .../PR3/soluciones_c/solucion1/media | Bin 0 -> 16048 bytes .../PR3/soluciones_c/solucion1/media.c | 29 +++ 3 files changed, 212 insertions(+) create mode 100755 fundamentos-programacion/PR3/soluciones_c/solucion1/media create mode 100644 fundamentos-programacion/PR3/soluciones_c/solucion1/media.c diff --git a/fundamentos-programacion/PR3/README.md b/fundamentos-programacion/PR3/README.md index 84517f4..26a901d 100644 --- a/fundamentos-programacion/PR3/README.md +++ b/fundamentos-programacion/PR3/README.md @@ -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 +#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**: + + diff --git a/fundamentos-programacion/PR3/soluciones_c/solucion1/media b/fundamentos-programacion/PR3/soluciones_c/solucion1/media new file mode 100755 index 0000000000000000000000000000000000000000..df48e48675a4eb97baf1c9a071606f06f4f5a64b GIT binary patch literal 16048 zcmeHOU2GKB6~1e1Fb%K^frOtnObApEkb-4D zUgJ0gMPKSqBWD!N`7B|A3{V8UeiX_;m7`VzYe(ZQ?yrkc{759f0{a!%PjHy~6O?>X zPUu+8`SfEobW{ZWeqHS6#|^HV5~Mz;Y`bA69;Lq~_S>mB%_u*9-ldhG^!H`>@ug*p zhl5<^X6|pi>(Ipc7v%J+WvOJ_s@i3#L{%!8&i7XJu3lBOsy3R-M3*}qQ(PJj8dICL zv??@HAdb_h%KBVc+9Tae)|36))vv7GeZ25)bMJ&XuRpWmc+dS=+sTIFCLgk)LhZ>A zA(`rvaFC6gk8hyna)g(yKXen0SE&6NV<;_f+8F!-aAVo87>B25G#vhN6olp{A%}2K zAD<|O(cWts9m%wrO763iVc>$a8#&v|+D5mTOoM^YzT0T;+HG{0$&~8JCewC@B0IWH ztJJ2(hWho!^62vD%2K_yR9jK2jE0uYM#9QkoynYSWm`6{Pi4|pi`kZ<4m-OuY3|T) zO~*8(YZAhBD#vH2bZ;VN4NgkNVSa9MGJQigFgG)qoJv=>iRE8>`+K;YWoic}Lhcvx zJ>50kRh;1af$$phl`&T@yvS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Q2xb#y#146@9+OTpZ_tv9GQr}%#R0_ku~1`xlsBPf5C}x_WG+Bn^wCh{YV4iCo;{miIe_i z9vESNzdiNkEz`MwHZQ5fe`$$vE8_1(89QFn7&my71N#h=GjK>ea{s00H;LWre~BKv bP%3 + +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; +}