INFORMÁTICA
Control 4. Tema 5
GRADO EN INFORMÁTICA 30 de Noviembre de 2022 UNIVERSITAT DE VALÈNCIA
Apellidos: Nombre:
Cognoms / Surname: Nom / Name:
1 pts
1.- Sea el siguiente código, di qué se muestra por pantalla:
Siga el següent codi, digues qué es mostra per pantalla:
What this code displays on screen:
int x = 1, i = 5;
Pantalla / Screen:
while (x >= 0)
CD5ACD5BT
{
for (int x = -1; x <= 0; x++)
cout << char (int ('D') + x);
x--;
cout << i;
if (x >= 0)
{
float i = 2;
cout << 'A';
}
else
{
i = -1;
cout << 'B';
}
}
if (x >= 1)
cout << 'O';
else
cout << 'T';
1.6 pts
2.- Explica el o los errores en cada uno de los siguientes trozos de código y como deberían corregirse.
Explica el o els errors en cada un dels següents fragments de codi i com deurien corregir-se.
Explain the error (s) in each of the following pieces of code and how they should be corrected.
Función que calcula y devuelve la suma de x e y
void Suma(int & x, int & y, int res) Function that calculates and returns the sum of x and y
{
res = x + y; int void Suma(int & x, int & y, int res)
{
return res; int res;
} res = x + y;
return res;
}
Función que nos dice si dos valores enteros pasados como parámetros son
void Consecutivos(int x, int y);
{ consecutivos o no.
aux_b = y == x + 1 Function that tells us if two integer values passed as parameters are consecutive or not
|| y = x - 1;
bool void Consecutivos(int x, int y);
return aux_b; {
} bool aux_b;
aux_b = y == x + 1
|| y = x - 1;
return aux_b;
}
1/6
2,2 ptos
3.- Realiza la traza e indica que se muestra por pantalla: int main(void)
{
Realitza la traça i indica que es mostra per pantalla: int a, b;
Do the trace and indicates that it is displayed on the screen
int Funcion(int a, int & b) (1) a = 2;
{ (2) b = 3;
(5) a = a - b; (3) a = a + Funcion(b, a);
(6) b = b - 2 * a; (4) b = b + a;
if (a - 2 < b) cout << a << '+' << b;
(7) b = Funcion(b, a);
return 0;
cout << a << ':' << b << ' '; }
return a + b;
} Pantalla / Screen
4:-9 -9:-5 -5:-14 -33+-30
a b
(1) 2 ?
(2) 2 3 a1 b1/a
(3) 2 3 1 2
(5)1 2 3 1 2
(6)1 0 3 1 0 a2 b2/a1
(7)1 0 3 1 0 0 1
(5)2 0 3 1 0 -1 1
(6)2 0 3 3 0 -1 3 a3 b3/a2
(7)2 0 3 3 0 -1 3 3 -1
(5)3 0 3 3 0 -1 3 4 -1
(6)3 0 3 3 0 -9 3 4 -9 -> 4:-9
(7)2 0 3 -5 0 -9 -5 <- 4 -9 = -5 -| -> -9:-5
(7)1 -14 3 -5 -14 <- -9 -5 = -14 -| -> -5:-14
(3) -14-19 = -33 3 <- -5 -14 = -19 -|
(4) -33 -30 -> -33+-30
2/6
INFORMÁTICA
Control 4. Tema 5
GRADO EN INFORMÁTICA 30 de Noviembre de 2022 UNIVERSITAT DE VALÈNCIA
Apellidos: Nombre:
Cognoms / Surname: Nom / Name:
1,5 ptos
4.- Indica el prototipo de las siguientes funciones.
Indica el prototip de les següents funcions.
Indicates the prototype of the following functions
Una función MostrarPrimos que muestre por pantalla todos los números primos comprendidos en el intervalo [a, b].
Una funció MostrarPrimos que mostre per pantalla tots els números primers compresos en l'intervalo [a, b].
A MostrarPrimos function that displays all prime numbers in the interval [a, b] on the screen.
void MostrarPrimos(int a, int b);
Una función MedDesvFichero que recibe el nombre de un fichero y devuelve la media y desviación estándar de los
enteros contenidos en el fichero, si se ha podido abrir el fichero.
Una funció MedDesvFichero que rep el nom d'un fitxer i retorna la mitjana i desviació estàndard dels enters continguts en el
fitxer, si s'ha pogut obrir el fitxer.
A MedDesvFichero function that receives the name of a file and returns the mean and standard deviation of the integers contained in the
file, if the file has been opened.
bool MedDesvFichero(string, float & med, float & desv);
void MedDesvFichero(string, float & med, float & desv, bool &);
float MedDesvFichero(string, float & desv, bool &);
Una función SumaCifras que determine la suma de las cifras de un número entero.
Una funció SumaCifras que determine la suma de les xifres d'un número enter.
A SumaCifras function that determines the sum of the digits of a whole number
int SumaCifras(int);
void SumaCifras(int, int &);
void SumaCifras(int &);
1 ptos
5.- Dada la siguiente función recursiva, escribe el esquema recursivo correspondiente:
Donada la següent funció recursiva, escriu l'esquema recursiu corresponent:
Given the following recursive function, write the corresponding recursive schema
int F(int x, int y)
{
int res;
if (x > y)
res = f(y, x);
else if (x == 0 || x == y)
res = 1;
else
res = f(x - 1, y - 1) + f(x - 1, y);
return res;
}
f ( y , x) si x> y
F ( x, y ) = 1 si x = 0∨ x = y
f ( x − 1, y − 1) + f ( x − 1, y ) en otro caso
3/6
1.2 pts
6.- Supongamos que tenemos una función SumarDivisoresPropios que determina la suma de los divisores
propios de un entero pasado como parámetro.
int SumarDivisoresPropios(int num);
Realiza una función que determine si dos números pasados como parámetros son o no amigos. Dos números son
amigos si la suma de los divisores propios del primer número es el segundo número y si además la suma de los
divisores propios del segundo número es el primer número.
Suposem que tenim una funció SumarDivisoresPropios que determina la suma dels divisors propis d'un enter passat com a
paràmetre.
int SumarDivisoresPropios(int num);
Realitza una funció que determine si dos números passats com a paràmetres són o no amics. Dos números són amics si la
suma dels divisors propis del primer número és el segon número i si a més la suma dels divisors propis del segon número és
el primer número.
Suppose we have a SumaDivisoresPropios function that determines the sum of the proper divisors of an integer parameter.
int SumarDivisoresPropios(int num);
Write a function that determines whether two numbers passed as parameters are friends or not. Two numbers are friends if the
sum of the divisors of the first number is the second number and if the sum of the divisors of the second number is the first
number.
bool SonAmigos(int, int);
bool SonAmigos(int x, int y)
{
return SumarDivisoresPropios(x) == y || SumarDivisoresPropios(y) == x;
}
4/6
INFORMÁTICA
Control 4. Tema 5
GRADO EN INFORMÁTICA 30 de Noviembre de 2022 UNIVERSITAT DE VALÈNCIA
Apellidos: Nombre:
Cognoms / Surname: Nom / Name:
1.5 pts
7.- Supongamos que tenemos las dos funciones anteriores. Realiza una nueva función a la que le pasemos un string
con el nombre de un fichero de tipo texto que contiene enteros y un entero y nos devuelva si hay o no en el
fichero un entero amigo del entero pasado como parámetro. La función devolverá, además, el entero amigo si se
ha encontrado.
Suposem que tenim les dues funcions anteriors. Realitza una nova funció a la qual li passem un string amb el nom d'un
fitxer de tipus text que conté enters i un enter i ens retorne si hi ha o no en el fitxer un enter amic de l'enter passat com a
paràmetre. La funció retornarà, a més, l'enter amic si s'ha trobat
Suppose we have the two previous functions. Performs a new function to which we pass a string with the name of a text file that
contains integers and an integer and returns whether or not there is a friend integer in the file passed as a parameter. The function
will also return the entire friend if it has been found
bool HayAmigo(string nom, int num, int & amigo)
{
bool hay_amigos = false;
ifstream f;
f.open(nom);
if (f)
{
while (!hay_amigos && f >> amigo)
if (SonAmigos(num, amigo))
hay_amigos = true;
f.close();
}
return hay_amigos;
}
bool HayAmigo(string nom, int num, int & amigo, bool hay_amigos)
{
bool ok;
ifstream f;
f.open(nom);
if (!f)
ok = false;
else
{
ok = true;
hay_amigos = false;
while (!hay_amigos && f >> amigo)
if (SonAmigos(num, i_aux))
hay_amigos = true;
f.close();
}
return hay_amigos;
}
5/6
1 pto adicional (solo si se ha aprobado el examen)
8.- Realiza un programa principal que pida un entero y el nombre de un fichero por teclado y, utilizando
necesariamente las funciones de los ejercicios 6 y 7, nos diga si se ha encontrado o no un entero amigo del
entero pedido por teclado y si se ha encontrado, qué entero es.
No vuelvas a copiar las funciones, incluye solo las cabeceras de todas ellas.
Realitza un programa principal que demane un enter i el nom d'un fitxer per teclat i, utilitzant necessàriament les funcions
dels exercicis 6 i 7, ens diga si s'ha trobat o no un enter amic de l'enter demanat per teclat i si s'ha trobat, quin enter és.
No tornes a copiar les funcions, sols inclou les capçaleres de totes elles.
Write a main program that asks for an integer and the name of a file by keyboard and, using necessarily the functions of exercises 6 &
7, tell us whether or not an entire friend of the integer requested by keyboard has been found and if it has been found, what integer
is.
Do not copy the functions again, include only the head of all of them.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int SumarDivisoresPropios(int);
bool SonAmigos(int, int)
bool HayAmigo(string, int, int &);
int main(void)
{
string nom;
int num, amigo;
ifstream f;
cout << "Dame nombre del fichero: ";
cin >> nom;
cout << "Dame entero: ";
cin >> num;
if (HayAmigo(nom, num, amigo))
cout << "Hay un amigo y es " << amigo << endl;
else
cout << "No habia amigos en el fichero.\n";
return 0;
}
6/6