diff --git a/fundamentos-programacion/PEC3/README.md b/fundamentos-programacion/PEC3/README.md index 80d508a..5d33fbc 100644 --- a/fundamentos-programacion/PEC3/README.md +++ b/fundamentos-programacion/PEC3/README.md @@ -24,14 +24,178 @@ ## 5. Estructura de control alternativa +Estructuras de control de flujo. + ### 5.1. Estructura alternativa +- *Consulta sobre el estado actual o condición* - Reacciona en función del valor obtenido. Tendrá como respuesta un valor lógico (verdadero o falso), pudiendo de esta forma tomar una decisión entre dos caminos distintos. +- *Qué hacer cuando la condición es verdadera* - Conjunto de instrucciones que se ejecutarán en caso verdadero. +- *Qué hacer cuando la condición es falsa (opcional)* - Conjunto de instrucciones que se ejecutarán en caso falso. +- *Fin* - Continua la ejecución del resto del programa. ### 5.2. Codificación de una estructura de control alternativa +| Partes | Codificiación en lenguaje algorítmico | +| ------------------------------------------- | ------------------------------------- | +| Consulta sobre el estado actual o condición | `If` *estructura condicial* `then` | +| Qué hacer cuando la condición es verdadero | *bloque de instrucciones* | +| Qué hacer cuando la condición es falso | `else` | +| | *bloque de instrucciones* | +| Marca de fin de estructura alternativa | `end if` | + + +Ejemplos: + +Si un número es par mostrar un mensaje. +```c +algorithm ex4 + var + n: integer; + end var + + n := readInteger(); + + if n mod 2 = 0 then + writeString("Es par"); + end if + +end algorithm + +#include + +int main(int argc, char** argv) { + int n; + scanf("%d", &n); + + if (n%2 == 0) { + printf("Es par"); + } + + return 0; + +} +``` + +Si la temperatura es menor que 21ºC encender la calefacción. Si la temperatura es mayor o igual que 21ºC apagar la calefacción. +```c +algorithm ex5 + var + t: real; + end var + t := readReal(); + if t < 21 then + switchOnHeating(); + else + switchOffHeating(); + end if +end algorithm + + +#include + +int main(int argc, char** argv) { + float t; + scanf("%f", &t); + + if (t < 21) { + switchOnHeating(); + } else { + switchOffHeating(); + } + return 0; +} +``` + +Si la temperatura es menor que 18ºC encender la calefacción. Si la temperatura es mayor que 26ºC encender el aire acondicionado. En cualquier otro caso apagar la calefacción y el aire acondicionado. +```c +algorithm ex6 + var + t: real; + end var + t := readReal(); + if t < 18 then + switchOnHeating(): + else + if t > 26 then + switchOnAirConditioning(); + else + switchOffHeating(); + switchOffAirConditioning(); + end if + end if +end algorithm + +#include + +int main(int argc, char** argv) { + float t; + scanf("%f", &t); + + if (t < 18) { + switchOnHeating(); + } else { + if (t > 26) { + switchOnAirConditioning(); + } else { + switchOffHeating(); + switchOffAirConditioning(); + } + } + return 0; +} +``` + +Si la edad es menor que 18 años mostrar un mensaje. Si la edad es mayor o igual que 18 años preguntar si tiene carnet de conducir. Si tiene carnet de conducir mostrar un mensaje. Si no tiene carnet de conducir mostrar otro mensaje. +```c +algorithm carPooling + var + age: integer; + hasDrivingLicense: boolean; + end var + writeString("How old are you?"); + age := readInteger(); + if age < 18 then + writeString("You still cannot drive"); + else + writeString("Do you have a driving licence?"); + hasDrivingLicense := readBoolean(); + if hasDrivingLicense then + writeString("Nice, we can share the car"); + else + writeString("I do. If you want we can use my car"); + end if + end if +end algorithm + +#include +#include +int main(int argc, char **argv) { + int age; + bool hasDrivingLicense; + int aux; + printf("How old are you?\n"); + scanf("%d", &age); + if (age < 18){ + printf("You still cannot drive\n"); + } else { + printf("Do you have driving licence?\n"); + scanf("%d", &aux); + hasDrivingLicense = aux; + if (hasDrivingLicense) { + printf("Nice, we can share the car\n"); + } else { + printf("I do. If you want we can use my car\n"); + } + } + return 0; +} +``` + ## 6. Tipos de datos: Vectores y matrices +Podemos definir variables que contienen varios elementos de un mismo tipo. Según estas variables tengan 1 o 2 dimensiones, las llamaremos vectores o matrices respectivamente. + ### 6.1. Vectores #### 6.1.1. Cadenas de caracteres o strings