Curso-lenguaje-C/fundamentos-programacion/PR3/README.md

1026 lines
28 KiB
Markdown
Raw Normal View History

2024-05-12 11:49:58 +02:00
# PR 3
[Volver a la página principal de "Fundamentos de la Programación"](../README.md)
**Índice**
- [PR 3](#pr-3)
- [16. Tipos de datos estructurados: tabla](#16-tipos-de-datos-estructurados-tabla)
- [16.1 Sintaxis para la declaración (definición) de tablas](#161-sintaxis-para-la-declaración-definición-de-tablas)
- [16.1.1 Ejemplo](#1611-ejemplo)
- [16.1.2 Acceso a los elementos de una tabla](#1612-acceso-a-los-elementos-de-una-tabla)
- [16.1.3 Ejemplo](#1613-ejemplo)
- [16.2 Inicialización de una tabla](#162-inicialización-de-una-tabla)
- [16.3 Carga de datos a partir de una secuencia](#163-carga-de-datos-a-partir-de-una-secuencia)
- [16.4 Recorrido de una tabla](#164-recorrido-de-una-tabla)
- [16.4.1 Ejemplo: lectura y recurrido](#1641-ejemplo-lectura-y-recurrido)
- [16.5 Búsqueda de un elemento en una tabla](#165-búsqueda-de-un-elemento-en-una-tabla)
- [16.6 Ordenación de las tablas](#166-ordenación-de-las-tablas)
- [16.6.1 Inserción ordenada](#1661-inserción-ordenada)
- [16.6.2 Algoritmo de ordenación](#1662-algoritmo-de-ordenación)
- [16.6.3 Importancia del orden en las tablas](#1663-importancia-del-orden-en-las-tablas)
- [16.6.3.1 Ejemplo](#16631-ejemplo)
- [16.6.3.2 Ejemplo](#16632-ejemplo)
- [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. Tipos de datos estructurados: tabla
2024-05-19 16:57:11 +02:00
Una tabla es un tipo de dato estructurado que permite almacenar un conjunto de elementos del mismo tipo. Cada elemento de la tabla se identifica mediante un índice que indica su posición en la tabla. El índice de un elemento de una tabla es un número entero que va desde 1 hasta el número de elementos de la tabla. En la mayoría de los lenguajes de programación, el índice de un elemento de una tabla se inicia en 1. En algunos lenguajes de programación, como C, el índice de un elemento de una tabla se inicia en 0.
2024-05-12 11:49:58 +02:00
## 16.1 Sintaxis para la declaración (definición) de tablas
2024-05-19 16:57:11 +02:00
Los elementos que se necesitan para declarar una tabla son:
- Un vector que define la longitud de la tabla y el tipo de elemento que se guardará. Por ejem:
- Predefinidos en el lenguaje algotítmico (integer, real, char, boolean)
- Tipos estructurados definidos por el usuario
- Una variable de tipo entero que se utiliza para indicar el número de elementos que hay en cada momento en la tabla (La ocupación de la tabla).
La declaración que hacemos es la de un tipo estructurado (tupla) que después podemos utilizar para definir variables dentro del algoritmo.
```alg
{constant definition - where length is a constant that defines the length of the vector}
const
MAX_ELEMENTS: integer = length;
end const
{type definition}
type
tExampleTable = record
elements: vector[MAX_ELEMENTS] of integer;
numElements: integer;
end record
end type
{var definition using the type}
var
aTable: tExampleTable;
end var
```
```c
2024-05-19 20:22:09 +02:00
#include <stdio.h>
2024-05-19 16:57:11 +02:00
#define MAX_ELEMENTS length
typedef struct {
int elements[MAX_ELEMENTS];
int numElements;
} tExampleTable;
tExampleTable aTable;
```
2024-05-12 11:49:58 +02:00
### 16.1.1 Ejemplo
2024-05-19 16:57:11 +02:00
Guardar un cuadro de control para las puertas de un edificio de un complejo de edificios. Cada elemento de la tabla indica si la puerta correspondiente está abierta o cerrada. Cada edificio tiene como máximo 40 puertas:
```alg
const
MAX_GATES: integer = 40;
end const
type
tGates = record
stateGates: vector[MAX_GATES] of boolean;
numGates: integer;
end record
end type
var
bulding1: tGates;
bulding2: tGates;
end var
```
```c
2024-05-19 20:22:09 +02:00
#include <stdio.h>
2024-05-19 16:57:11 +02:00
#include <stdbool.h>
#define MAX_GATES 40
typedef struct {
bool stateGates[MAX_GATES];
int numGates;
} tGates;
tGates bulding1;
tGates bulding2;
```
2024-05-12 11:49:58 +02:00
### 16.1.2 Acceso a los elementos de una tabla
2024-05-19 20:22:09 +02:00
Podemos acceder a los diferentes elementos de una tabla de manera similar a como lo hacíamos con los vectores, pero teniendo en cuenta que se trata de una tupla.
```alg
const
K: integer = 10;
MAX_GATES: integer = 40;
end const
type
tGates = record
stateGates: vector[MAX_GATES] of boolean;
numGates: integer;
end record
end type
{header of fillTable}
action fillTable (out Building: tGates);
algorithm checkDoors
var
building1: tGates;
building2: tGates;
p: integer;
b: boolean;
c: boolean;
end var
c := true;
fillTable(building1);
{the following statements treat the element of the table as variables}
{assigns the third element of the table building1 to the variable b}
b:= building1.stateGates[3];
{assigns the value of the variable c to the Kth gate of building1, assuming that K is a constant such that 1 ≤ K≤ 40}
building1.stateGates[K]:= c;
{assigns the value of the first gate of building1 to the gate 2*p +5 as long as 1 ≤ 2*p+5 ≤ 40}
building1.stateGates[2*p+5]:= building1.stateGates[1];
end algorithm
```
```c
#include <stdio.h>
#include <stdbool.h>
#define K 10
#define MAX_GATES 40
typedef struct {
bool stateGates[MAX_GATES];
int numGates;
} tGates;
void fillTable (tGates *Building);
int main(int argc, char** argv) {
tGates building1;
tGates building2;
int p;
bool b;
bool c = true;
/* the following statements treat the elements of the table as variables */
fillTable(&building1);
/* assigns the third element of the table building1 to the variable b */
b = building1.stateGates[2];
/* assigns the value of the variable c to the kth gate of building1, assuming that k is a constant such that 1 ≤ K
≤ 40 */
building1.stateGates[K-1] = c;
/* assigns the value of the first gate of building1 to the gate 2*p +5 as long as 1 ≤ 2*p+5 ≤ 40 */
building1.stateGates[2*p+5-1] = building1.stateGates[0];
return 0;
}
```
Comentarios:
- En el lenguaje algorítmico las tablas se indexan empezando con el número 1 mientras en C se indexan a partir del 0.
- Algunos lenguajes de programación permiten indexar con culquier rango, por ejemplo, del 10 al 60.
- Durante la ejecución es importante controlar que el índice siempe esté dentro de los límites esperados para evitar errores.
2024-05-12 11:49:58 +02:00
### 16.1.3 Ejemplo
2024-05-19 20:22:09 +02:00
Una empresa con diversos cines de tipo multisala. Sabemos que el cine más grande tiene 20 salas.
Creamos una tabla para guardar la recaudación de cada sala pero que sirva para cualquiera de los cines. Es decir, definimos una tabla que pueda guardar hasta 20 importes reales. La tabla no siempre se llemará ya que dependerá del número de salas que tenga cada cine.
```alg
const
MAX_THEATERS: integer = 20;
end const
type
tTheaters = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
var
santalo: tTheaters;
sants: tTheaters;
end var
```
```c
#include <stdio.h>
/* Definition of a constant to indicate the size of the table */
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheaters;
/*var declaration assuming two movies */
tTheaters santalo;
tTheaters sants;
```
2024-05-12 11:49:58 +02:00
## 16.2 Inicialización de una tabla
2024-05-19 20:22:09 +02:00
Las variables de tipo tabla, como todas las variables, deben inicializarse antes de ser utilizadas. En el caso de las tablas, la manera de inicializarlas es indicando que está vacía.
Lo hacemos asignando 0 al número de elementos ocupados en la tabla. Volviendo al anterior ejemplo, si queremos indicar que los cines inicialmente no tienen ninguna recaudación, debemos hacer lo siguiente:
```alg
const
MAX_THEATERS: integer = 20;
end const
type
tTheaters = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
algorithm initTheaters
var
santalo: tTheaters;
sants: tTheaters;
end var
{table initialitation, setting the number of theater to 0}
santalo.numTheaters := 0;
sants.numTheaters := 0;
end algorithm
```
```c
#include <stdio.h>
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheaters;
/var declaration */
tTheaters santalo;
tTheaters sants;
int main(int argc, char** argv) {
/* table initialitation, setting the number of theater to 0 */
santalo.numTheaters = 0;
sants.numTheaters = 0;
return 0;
}
```
2024-05-12 11:49:58 +02:00
## 16.3 Carga de datos a partir de una secuencia
2024-05-19 20:22:09 +02:00
Una operación habitual es carga la tabla con información recogida en el canal estándar de entrada.
Los datos vienen dados con una serie de reales (una secuencia de reales) que leemos del canal estándar y el valor -1.0 indica que es el final de los datos (fin de la secuencia de entrada).
Continuamos con el ejemplo anterior y, para una mejor estructura, aislamos la lectura de la información en un acción.
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
action fillTable(out movieTheater: tTheater);
var
temp: real;
end var
{table initialization}
movieTheater.numTheaters:=0;
temp:= readReal(); {we read the first number}
{iteration while the read number is not -1}
while temp ≠ END_SEQ do
{Save the read number in the table.}
movieTheater.collect[movieTheater.numTheaters+1]:= temp;
movieTheater.numTheaters:= movieTheater.numTheaters + 1;
temp:= readReal();
end while
end action
```
```c
#include <stdio.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void fillTable(tTheater *movieTheater) {
/* var definition */
float temp;
/* table initialization */
movieTheater->numTheaters = 0;
scanf("%f", &temp); /* we read the first number */
/* iteration while the read number is not -1 */
while (temp != END_SEQ) {
/* Save the read number in the table. */
movieTheater->collect[movieTheater->numTheaters] = temp;
movieTheater->numTheaters = movieTheater->numTheaters + 1;
temp = readReal();
}
}
```
Comentarios:
- Cada elemento de la tabla se trata como una variable.
- Antes de entrar en la iteración se lee el primer elemento. Así, antes de entrar en el bucle ya se puede comprobar si el valor del elemento es el final de la secuencia o no.
- En cada iteración incrementamos el valor de `numTheaters` para indicar que hay un elemento más en la tabla.
- A la acción le pasamos un valor de entrada/salida de tipo tabla. La acción modifica el valor de la tabla con los datos introducidos.
- En el lenguaje algorítmico el índice empieza en 1 mientras que en C empieza en 0.
- Cuando se pasa una estructura como parámetro de entrada/salida para acceder a los campos de la tupla (record) en lenguaje algorítmico se usa el punto (.) y en C se usa la notación ->.
2024-05-12 11:49:58 +02:00
## 16.4 Recorrido de una tabla
2024-05-19 20:22:09 +02:00
Para recorrer una tabla se puede hacer de dos maneras:
- Recorrido secuencial: se recorren todos los elementos de la tabla uno a uno.
- Recorrido por búsqueda: se busca un elemento en la tabla y se recorre a partir de él.
Siguiendo con el ejemplo anterior, vamos a realizar la simulación de un aumento del precio al 20%. Con lo cuál, vamos a aislar en una acción la operación de recorrer la tabla para multiplicar cada uno de sus elementos por 1.2.
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
action update(inout movieTheater: tTheater);
var
i: integer;
end var
{iteration. We can use a for ... do iteration because we know the exact number of element of the table}
for i:= 1 to movieTheater.numTheaters do
movieTheater.collect[i]:= movieTheater.collect[i]*1.2;
end for
end action
```
```c
#include <stdio.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void update(tTheater *movieTheater) {
/* var definition */
int i;
/* iteration. We can use a for ... do iteration because we know the exact number of element of the table */
for (i = 0; i < movieTheater->numTheaters; i++) {
movieTheater->collect[i] = movieTheater->collect[i] * 1.2;
}
}
```
Comentarios:
- Para realizar el recorrido completo, podemos hacerlo utilizando el for ... do para iterar sobre todos los elementos de la tabla ya que sabemos exactamente cuántos elementos hay presentes.
- En C la iteración se hace de 0 a `movieTheater.numTheaters - 1` ya que el índice de la tabla empieza en 0 y en lenguaje algorítmico empieza en 1.
2024-05-12 11:49:58 +02:00
### 16.4.1 Ejemplo: lectura y recurrido
2024-05-19 20:22:09 +02:00
Ahora vamos a realizar un algoritmo que lea del canal estándar la secuencia con la recaudación de las salas del cine, la muestre por el canal estándar, calcule el porcentaje indicado de sus elementos y la vuelva a mostrar por el canal estándar de salida.
Usaremos una acción para mostrar el contenido de la tabla:
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
algorithm simulation
var
santalo: tTheater;
end var
fillTable(santalo);
printTable(santalo);
update(santalo);
printTable(santalo);
end algorithm
action fillTable(out movieTheater: tTheater);
var
temp: real;
end var
{table initialization}
movieTheater.numTheaters:=0;
temp:= readReal(); {we read the first number}
{iteration while the read number is not -1}
while temp ≠ END_SEQ do
{Save the read number in the table}
movieTheater.numTheaters:=movieTheater.numTheaters + 1;
movieTheater.collect[movieTheater.numTheaters]:= temp;
temp:= readReal();
end while
end action
action update(inout movieTheater: tTheater)
var
i: integer;
end var
{iteration. We can use a for ... do iteration because we know the exact number of element of the table}
for i:=1 to movieTheater.numTheaters do
movieTheater.collect[i]:= movieTheater.collect[i] * 1.20;
end for
end action
action printTable(in movieTheater: tTheater)
var
i: integer;
end var
{iteration. We can use a for ... do iteration because we know the exact number of element of the table}
for i:=1 to movieTheater.numTheaters do
writeReal(movieTheater.collect[i]);
end for
end action
```
```c
#include <stdio.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void fillTable(tTheater *movieTheater) {
/* var definition */
float temp;
/* table initialization */
movieTheater->numTheaters = 0;
scanf("%f", &temp); /* we read the first number */
/* iteration while the read number is not -1 */
while (temp != END_SEQ) {
/* Save the read number in the table */
movieTheater->collect[movieTheater->numTheaters] = temp;
movieTheater->numTheaters = movieTheater->numTheaters + 1;
scanf("%f", &temp);
}
}
void update(tTheater *movieTheater) {
/* var definition */
int i;
/* iteration. We can use a for ... do iteration because we know the exact number of element of the table */
for (i = 0; i < movieTheater->numTheaters; i++) {
movieTheater->collect[i] = movieTheater->collect[i] *1.20 ;
}
}
void printTable(tTheater movieTheater) {
/* var definition */
int i;
/* iteration. We can use a for ... do iteration because we know the exact number of element of the table */
for (i=0; i< movieTheater.numTheaters; i++) {
printf("%f ", movieTheater.collect[i]);
}
}
int main(int argc, char** argv) {
tTheater santalo;
fillTable(&santalo);
printTable(santalo);
update(&santalo);
printTable(santalo);
return 0;
}
```
Comentarios:
- Hemos escrito un algoritmo completo que hemos estructurado con acciones donde cada acción hace una operación concreta sobre una tabla de valores de tipo real.
- Las acciones incluyen la inserción de elementos en la tabla y el control de cuántos se han insertado, un recorrido para aumentar la recaudación y otro recorrido para mostrar por el canal estándar los valores guardados en la tabla.
- En la acción de escritura, la tabla se pasa como parámetro solamente de entrada. Fijaos en que en C, en este caso, la notación utilizada para acceder a los campos de la tupla (record) es el punto (.).
2024-05-12 11:49:58 +02:00
## 16.5 Búsqueda de un elemento en una tabla
2024-05-20 21:26:21 +02:00
Para realizar una búsqueda debemos hacer un recorrido de todos los elementos de la tabla, pero a diferencia del recorrido secuencial, en este caso, paramos cuando encontramos el elemento que buscamos. Por lo tanto la iteración debe ser con un bucle while y no con un for.
2024-05-12 11:49:58 +02:00
2024-05-20 21:26:21 +02:00
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
2024-05-19 20:22:09 +02:00
2024-05-20 21:26:21 +02:00
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
2024-05-19 20:22:09 +02:00
2024-05-20 21:26:21 +02:00
algorithm simulation
var
movieTheater: tTheater;
value: real;
position: integer;
end var
fillTable(movieTheater);
value:= readReal();
searchValue(movieTheater, value, position);
writeInteger(position);
end algorithm
action fillTable(out movieTheater: tTheater);
var
i: integer;
temp: real;
end var
{table initialization}
movieTheater.numTheaters:=0;
temp:= readReal(); {we read the first digit}
{iteration while the read number is not -1}
while temp ≠ END_SEQ do
{Save the read number in the table}
movieTheater.numTheaters:=movieTheater.numTheaters + 1;
movieTheater.collect[movieTheater.numTheaters]:= temp;
temp:= readReal();
end while
end action
action searchValue(in movieTheater: tTheater, in value: real, out position: integer)
var
i: integer;
found: boolean;
end var
{variable initialization. i is set to 1 to start searching in the table at postion 1 and found to false to indicate that so far value has not been found. The out parameter position is set to -1 by default}
i:= 1;
found:= false;
position:= -1;
{iteration while there still are elements in the table and the value has not been found}
while i≤ movieTheater.numTheaters and not found do
if movieTheater.collect[i] ≥ value then
found:= true;
else
i:=i + 1;
end if
end while
if found then
position := i;
end if
end action
```
```c
#include <stdio.h>
#include <stdbool.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void fillTable(tTheater *movieTheater) {
/* var definition */
float temp;
/* table initialization */
movieTheater->numTheaters = 0;
scanf("%f", &temp); /* read the first digit */
/* iteration while the read number is not -1 */
while (temp != END_SEQ) {
/* Save the read number in the table */
movieTheater->collect[movieTheater->numTheaters] = temp;
movieTheater->numTheaters = movieTheater->numTheaters + 1;
scanf("%f", &temp);
}
}
2024-05-12 11:49:58 +02:00
2024-05-20 21:26:21 +02:00
void searchValue(tTheater movieTheater, float value, int *position) {
/* var definition */
int i;
bool found;
/* variable initialization. i is set to 0 to start searching in the table at postion 0 and found to false to indicate that so far value has not been found. The out parameter position is set to -1 by default */
i = 0;
found = false;
*position = -1;
/* iteration while there still are elements in the table and the value has not been found */
while (i < movieTheater.numTheaters && !found) {
if (movieTheater.collect[i] >= value) {
found = true;
} else {
i++;
}
}
if (found) {
*position = i;
}
}
int main(int argc, char** argv) {
tTheater movieTheater;
float value;
int position;
fillTable(&movieTheater);
printf("%d", movieTheater.numTheaters);
scanf("%f", &value);
searchValue(movieTheater, value, &position);
printf("%d", position);
return 0;
}
```
Comentarios:
- Se inicializa el valor de la variable `position` a -1 para indicar que no se ha encontrado el valor buscado.
- Para buscar el elemento iteramos con el bucle while controlando si se encuentra el valor o no. Si se encuentra no llega al final de la tabla. No podemos usar un for ya que no sabemos en qué momento podemos encontrar el valor buscado.
- En el lenguaje algorítmico el índice empieza en 1 mientras que en C empieza en 0.
- El parámetro `position` es de salida, por eso se utiliza el caracter * delante del nombre del parámetro.
## 16.6 Ordenación de las tablas
2024-05-12 11:49:58 +02:00
### 16.6.1 Inserción ordenada
2024-05-20 21:26:21 +02:00
Vamos a modificar en el ejemplo anterior la acción de lectura de las recaudaciones para que se mantenga la tabla siempre ordenada.
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
action fillTable(out movieTheater: tTheater);
var
i, j: integer;
found: boolean;
temp: real;
end var
{table initialization}
movieTheater.numTheaters:=0;
temp:= readReal(); {we read the first digit}
{iteration while the read number is not -1}
while temp ≠ END_SEQ do
{find the right location}
i:= 1;
found:= false;
{search for the right location}
while i ≤ movieTheater.numTheaters and not found do
if movieTheater.collect[i] ≥ temp then
found:= true;
else
i:= i + 1;
end if
end while
{all the values to the right of the position found must be moved to one position to the right}
if movieTheaters.numTheathers > 0 then
for j:= movieTheaters.numTheathers to i step -1 do
movieTheater.collect[j+1]:= movieTheater.collect[j];
end for
end if
{insert new value}
movieTheater.collect[i]:= temp;
movieTheater.numTheaters:= movieTheater.numTheaters + 1;
temp:= readReal();
end while
end action
```
```c
#include <stdio.h>
#include <stdbool.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void fillTable(tTheater *movieTheater) {
/* var definition */
float temp;
int i, j;
bool found;
/* table initialization */
movieTheater->numTheaters = 0;
scanf("%f", &temp); /* read the first digit */
/* iteration while the read number is not -1 */
while (temp != END_SEQ) {
i = 0;
found = false;
/* search for the right location */
while (i < movieTheater->numTheaters && !found) {
if (movieTheater->collect[i] >= temp) {
found = true;
} else {
i++;
}
}
/* move values to the right */
if (movieTheater->numTheaters > 0) {
for (j = movieTheater->numTheaters; j >= i; j--) {
movieTheater->collect[j] = movieTheater->collect[j-1];
}
}
/* insert new value */
movieTheater->collect[i] = temp;
movieTheater->numTheaters = movieTheater->numTheaters + 1;
scanf("%f", &temp);
}
}
int main(int argc, char** argv) {
tTheater santalo;
int i;
fillTable(&santalo);
for (i = 0; i < santalo.numTheaters; i++) {
printf("%f ", santalo.collect[i]);
}
return 0;
}
```
Comentarios:
- Para mantener la tabla ordenada cada vez que se inserte un valor nuevo, primnero es necesario buscar el lugar adecuado que le corresponde según los valores que ya hay en la tabla y el que queremos insertar. Para encontrarlo usamos una búsqueda, que corresponde al while más interno.
- Una vez encontrado el sitio correcto, es necesario desplazar hacia la derecha 1 casilla todos los elementos que quedan a la derecha del punto donde se debe insertar. Para hacerlo, usamos una iteración de tipo for, ya que savemos cuántos elementos se debe mover.
- En el algoritmo hemos uspuesto que en la secuencia de entrada nunca habrá más números de los permitidos. Si no se sabe con seguridad, sería necesario añadir al algoritmo un control para evitar que se intente insertar un elemento fuera del rango de la tabla.
2024-05-12 11:49:58 +02:00
### 16.6.2 Algoritmo de ordenación
2024-05-20 21:26:21 +02:00
La ordenación de las tablas es un tema bastante extenso, vamos a ver uno fácil de entender, aunque no sea el más eficiente.
Si la tabla tiene n elementos, el algoritmo consiste en iterar n veces de la siguiente manera:
1. La primera iteración es un recorrido por toda la tabla para encontrar el elemento menor. Cunado se identifica, se intercambian los elementos de la posición 1 y la posición del más pequeño, de tal manera que al acabar la primera iteración sabemos que el primer elemento de la tabla es el menor.
2. En la segunda iteración ya no hace falta recorrer toda la tabla, sino que podemos empezar en el segundo elemento y hacer el mismo proceso pero desde la posición 2.
3. En la tercer iteración se repite el procedimiento pero empezando en la posición 3.
4. Y así se repite el procedimiento hasta la última iteración.
Ahora vamos a retomar el ejemplo anterior y rellenamos la tabla con las recaudaciones usando el algoritmo inicial y ordenando como la anterior metodología.
```alg
const
MAX_THEATERS: integer = 20;
END_SEQ: real = -1.0;
end const
type
tTheater = record
collect: vector[MAX_THEATERS] of real;
numTheaters: integer;
end record
end type
algorithm simulation
var
santalo: tTheater;
end var
fillTable(santalo);
sort(santalo);
for i:= 1 to santalo.numTheaters do
writeReal(santalo.collect[i]);
end for
end algorithm
action fillTable(out movieTheater: tTheater);
var
i: integer;
temp: real;
end var
{table initialization}
movieTheater.numTheaters:=0;
temp:= readReal(); {we read the first digit}
{iteration while the read number is not -1}
while temp ≠ END_SEQ do
{Save the read number in the table}
movieTheater.numTheaters:=movieTheater.numTheaters + 1;
movieTheater.collect[movieTheater.numTheaters]:= temp;
temp:= readReal();
end while
end action
action sort(inout movieTHeater:tTheater)
var
i, j, posMin: integer;
aux: integer;
end var
i:= 1;
while j ≤ movieTheater.numTheaters do
posMin:= i;
j:= i + 1;
while j ≤ movieTheater.numTheaters do
if movieTheater.collect[j] < movieTheater.collect[posMin] then
posMin:= j;
end if
j:= j + 1;
end while
aux:= movieTheater.collect[posMin];
movieTheater.collect[posMin]:= movieTheater.collect[i];
movieTheater.collect[i]:= aux;
i:= i + 1;
end while
end action
```
```c
#include <stdio.h>
#define END_SEQ -1.0
#define MAX_THEATERS 20
typedef struct {
float collect[MAX_THEATERS];
int numTheaters;
} tTheater;
void fillTable(tTheater *movieTheater) {
/* var definition */
int i;
float temp;
/* table initialization */
movieTheater->numTheaters = 0;
scanf("%f", &temp); /* read the first digit */
/* iteration while the read number is not -1 */
while (temp != END_SEQ) {
/* Save the read number in the table */
movieTheater->collect[movieTheater->numTheaters] = temp;
movieTheater->numTheaters = movieTheater->numTheaters + 1;
scanf("%f", &temp);
}
}
void sort(tTheater *movieTheater) {
/* var definition */
int i, j, posMin;
float aux;
i = 0;
while (i < movieTheater->numTheaters) {
posMin = i;
j = i + 1;
while (j < movieTheater->numTheaters) {
if (movieTheater->collect[j] < movieTheater->collect[posMin]) {
posMin = j;
}
j++;
}
aux = movieTheater->collect[posMin];
movieTheater->collect[posMin] = movieTheater->collect[i];
movieTheater->collect[i] = aux;
i++;
}
}
int main(int argc, char** argv) {
tTheater santalo;
int i;
fillTable(&santalo);
sort(&santalo);
for (i = 0; i < santalo.numTheaters; i++) {
printf("%f ", santalo.collect[i]);
}
return 0;
}
```
2024-05-12 11:49:58 +02:00
### 16.6.3 Importancia del orden en las tablas
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
#### 16.6.3.1 Ejemplo
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
#### 16.6.3.2 Ejemplo
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
## 16.7 Tabla de tipos estructurados
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
### 16.7.1 Tablas de tuplas
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
### 16.7.2 Tabla de tablas
2024-05-20 21:26:21 +02:00
2024-05-12 11:49:58 +02:00
[Volver arriba](#pr-1)