Sei sulla pagina 1di 16

Errores

1. [001.txt] Detecta los errores de los siguientes trozos de código:


class base1 {
int b11 ;
protected :
int b12 ;
public :
void almacenarb1 ( int i , int j ) { b11=i ; b12=j ; }
};
class base2 {
int b21 ;
protected :
int b22 ;
public :
void almacenarb2 ( int i , int j ) { b21=i ; b22=j ; }
};
c l a s s d e r i v a d a : protected base1 , private b a s e 2 {
int d ;
public :
void almacenar ( int k ) {d=k ; }
void mos tr ar ( ) { cout<<b11<<b12<<b21<<b22 ; }
};
main ( ) {
derivada d ;
d . almacenarb1 ( 1 , 2 ) ;
d . almacenarb2 ( 3 , 4 ) ;
d . almacenar ( 5 ) ;
d . mos tr ar ( ) ;
}

Solución:

class base1 {
int b11 ;
protected :
int b12 ;
public :
void almacenarb1 ( int i , int j ) { b11=i ; b12=j ; }
};
class base2 {
int b21 ;
protected :
int b22 ;
public :
void almacenarb2 ( int i , int j ) { b21=i ; b22=j ; }
};
c l a s s d e r i v a d a : protected base1 , private b a s e 2 {
int d ;
public :
void almacenar ( int k ) {d=k ; }
// b11 de b a s e 1 y b21 de b a s e 2 son p r i v a t e ,
// l u e g o d e r i v a d a no puede a c c e d e r a e l l o s en mostrar ( )
void mos tr ar ( ) {
// cout<<b11<<b12<<b21<<b22 ;
}
};
main ( ) {
derivada d ;

Página 1 de 16
Errores

// almacenarb1 s e h e r e d a como p r o t e c t e d ,
// l u e g o no s e puede u s a r d e s d e main
// d . almacenarb1 ( 1 , 2 ) ;
// almacenarb2 s e h e r e d a como p r i v a t e ,
// l u e g o no s e puede u s a r d e s d e main
// d . almacenarb2 ( 3 , 4 ) ;
d . almacenar ( 5 ) ;
d . mos tr ar ( ) ;
}

2. [002.txt] Detecta los errores de los siguientes trozos de código:


class racional
{ int numerador ;
int denominador ;
public :
r a c i o n a l ( int n , int d ) ;
i s t r e a m &operator<<(i s t r e a m &f , const r a c i o n a l &r ) ;
};
i s t r e a m &operator<<(i s t r e a m &f , const r a c i o n a l &r )
{
f <<numerador<<ends<<denominador ;
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
class racional
{ int numerador ;
int denominador ;
public :
r a c i o n a l ( int n , int d ) ;
// e l metodo no e s de l a c l a s e pues e l argumento i m p l i c i t o e s un ostream .
// podemos d e c l a r a r l o como f r i e n d , para que acceda a l o s miembros p r i v a d o s
// i s t r e a m &o p e r a t o r <<(i s t r e a m &f , c o n s t r a c i o n a l &r ) ;
friend ostream &operator<<(ostream &f , const r a c i o n a l &r ) ;
};
// o p e r a t o r << s e d e f i n e para ostream , no para i s t r e a m
// i s t r e a m &o p e r a t o r <<(i s t r e a m &f , c o n s t r a c i o n a l &r )
ostream &operator<<(ostream &f , const r a c i o n a l &r )
{
// f <<numerador<<ends<<denominador ;
// como no e s un metodo miembro hay que a c c e d e r a
// l o s miembros a t r a v e s de l a v a r i a b l e r
f <<r . numerador<<ends<<r . denominador ;
// d e v o l v e r e l f l u j o que r e c i b e , para poder encadenar l a s l l a m a d a s
return f ;
}
// F a l t a d e f i n i r e l c o n s t r u c t o r que s e ha d e c l a r a d o .
r a c i o n a l : : r a c i o n a l ( int n , int d )
: numerador ( n ) , denominador ( d ) {
}

3. [003.txt]

Página 2 de 16
Errores

c l a s s punto
{ int x ;
int y ;
public :
punto ( int x=0, int y ) ;
r e g i s t e r void d i b u j a r x ( )
{ cout<<x ;
void d i b u j a r y ( )
{ cout<<y ; }
}
}p1 ;
int i=p1 . d i b u j a r x ( ) ;

Solución:

#include <i o s t r e a m >


using namespace s t d ;
c l a s s punto {
int x ;
int y ;
public :
// e l c o n s t r u c t o r ha de t e n e r v a l o r e s por d e f e c t o
// c o n t i g u o s h a s t a e l u l t i m o
// punto ( i n t x =0, i n t y ) ;
punto ( int x=0, int y=0) ;
// item un metodo no puede d e v o l v e r un t i p o r e g i s t e r
// , y ademas e s v o i d , no d e v u e l v e nada .
// r e g i s t e r v o i d d i b u j a r x ( )
void d i b u j a r x ( ) {
cout<<x ;
// d i b u j a r y ( ) s e r i a una f u n c i o n anidada .
// Se p e r m i t e en C, pero no en C++
/∗
void dibujary () {
cout<<y ;
}
∗/
}
}p1 ;
// s i asignamos a i e l v a l o r de d i b u j a r x ( ) e n t o n c e s e s t e
// d e b e r i a d e c l a r a r s e d e v o l v i e n d o i n t y d e v o l v e r a l g u n v a l o r e n t e r o .
// i n t i=p1 . d i b u j a r x ( ) ;

4. [004.txt]
union
{
int ∗ e l 1 ;
float ∗ el2 ;
}u1 ;
int i ( 3 ) ;
u1 . e l 1=&i ;
int ∗q=&u1 . e l 1 ;
cout<<” Valor e n t e r o a l que apunta q e s : ”<<∗∗q ;
cout<<” Valor e n t e r o a l que apunta e l 1 e s : ”<<u1−>e l 1 ;

Página 3 de 16
Errores

Solución:

#include <i o s t r e a m >


using namespace s t d ;
int main ( ) {
union {
int ∗ e l 1 ;
float ∗ el2 ;
}u1 ;
int i ( 3 ) ;
u1 . e l 1=&i ;
// s i q e s un p u n t e r o a i n t debemos a s i g n a r l e u1 . e l 1
// que tambien l o es , no su d i r e c c i o n .
// i n t ∗ q=&u1 . e l 1 ;
int ∗q=u1 . e l 1 ;
// S i queremos a s i g n a r l e l a d i r e c c i o n de u1 . e l 1 , d e b e d e c l a r a r s e como i n t ∗∗ q
,
// en e s e c a s o l a s i g u i e n t e l i n e a e s t a r i a c o r r e c t a .
// cout <<”Valor e n t e r o a l que apunta q e s : ”<<∗∗q ;
cout<<” Valor e n t e r o a l que apunta q e s : ”<<∗q ;
// u1 no e s un puntero , l u e g o para a c c e d e r a e l 1 usaremos u1 . e l 1 , y no u1−>
el1
// cout <<”Valor e n t e r o a l que apunta e l 1 e s : ”<<u1−>e l 1 ;
cout<<” Valor e n t e r o a l que apunta e l 1 e s : ”<<u1 . e l 1 ;
}

5. [005.txt] Detecta los posibles errores en los siguientes segmentos de programa e intenta corregirlos razonando
su corrección:
char op ;
switch ( op ) {
case ”v” : cout<<” V i s u a l i z a r ” ;
case ” c ” : cout<<” Crear ” ;
char ∗ v e c t o r ;
i f ( ( v e c t o r=new char ∗ [ 5 ] )==NULL)
cout<<” E r r o r . F a l t a de memoria ” ;
else crear ( vector [ 2 ] ) ;
break ;
}
void f u n c i o n ( ) {
struct c a n c i o n
{ char t i t u l o [ ] ;
enum { rock , pop , c o p l a , l i g e r o } e s t i l o ;
unsigned int u n i d a d e s ; }
extern c a n c i o n c1 ;
s t a t i c int c o d i g o ( 1 ) ;
.....
cout<<” E s t i l o de c a n c i o n : ”<<c1 . e s t i l o ;
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
void c r e a r ( char ∗ ) ;

Página 4 de 16
Errores

int main ( ) {
char op ;
switch ( op ) {
//Cambiamos c o m i l l a s d o b l e s por s i m p l e s
case ’ v ’ : cout<<” V i s u a l i z a r ” ;
// D e b e r i a h a b e r un b r e a k a q u i !
case ’ c ’ : cout<<” Crear ” ;
// v e c t o r e s un char ∗ y new e s t a creando un a r r a y de p u n t e r o s a char ,
// a l que apunta v e c t o r , d e b e r i a d e c l a r a r s e como char ∗∗ v e c t o r
// char ∗ v e c t o r ;
char ∗∗ v e c t o r ;
i f ( ( v e c t o r=new char ∗ [ 5 ] )==NULL)
cout<<” E r r o r . F a l t a de memoria ” ;
//No p a r e c e muy l o g i c o que l a f u n c i o n c r e a r r e c i b a e l
// t e r c e r e l e m e n t o d e l v e c t o r como parametro
else crear ( vector [ 2 ] ) ;
break ;
}
}
void f u n c i o n ( ) {
struct c a n c i o n {
//Un a r r a y v a c i o ?
// char t i t u l o [ ] ;
char t i t u l o [ 4 0 ] ;
enum { rock , pop , c o p l a , l i g e r o } e s t i l o ;
unsigned int u n i d a d e s ;
// F a l t a e l punto y coma d e s p u e s de l a l l a v e de l a e s t r u c t u r a
// }
};
// Al d e c l a r a r s t r u c t c a n c i o n d e n t r o de
// f u n c i o n , no puede h a b e r un e l e m e n t o e x t e r n de e s e t i p o
// e x t e r n c a n c i o n c1 ;
c a n c i o n c1 ;
s t a t i c int c o d i g o ( 1 ) ;
// . . . . .
cout<<” E s t i l o de c a n c i o n : ”<<c1 . e s t i l o ;

6. [006.txt] Describe la salida proporcionada por los siguientes trozos de código:


int &cambiar ( int &a , int &b ) ;
int main ( )
{
int i ( 3 ) , j ( 5 ) ;
cambiar ( i , j ) =15;
cout<<i <<ends<<j ;
return 0 ;
}
int &cambiar ( int &a , int &b )
{ i f ( a<b ) return a ;
e l s e return b ; }

Solución:

Página 5 de 16
Errores

/∗
La f u n c i o n cambiar d e v u e l v e una r e f e r e n c i a a un e n t e r o , e l menor de l o s
dos que r e c i b e . D e v o l v e r a una r e f e r e n c i a a su parametro a , a l c u a l
l e enviamos l a r e f e r e n c i a a i . Al e j e c u t a r s e l a f u n c i o n ,
i queda con e l v a l o r a s i g n a d o de 1 5 . La s a l i d a s e r a :
15 5
∗/

7. [007.txt]
void main ( )
{
deque<int> d ;
deque<int > : : i t e r a t o r i t e ;
f or ( int i =0; i <10; i ++){
d . push back ( i ) ;
d . push front ( i ) ;}
d . pop front () ;
d . pop back ( ) ;
d . e r a s e ( d . b e g i n ( )+ 1 ) ;
d . e r a s e ( d . end ( )− 1 ) ;
f or ( i t e= d . b e g i n ( ) ; i t e !=d . end ( ) ; i t e ++)
cout <<∗i t e <<” ” ;
cout<<e n d l ;
}

Solución:

#include <i o s t r e a m >


#include <deque>
using namespace s t d ;
// v o i d main ( )
int main ( )
{
deque<int> d ;
deque<int > : : i t e r a t o r i t e ;
f or ( int i =0; i <10; i ++){
d . push back ( i ) ;
d . push front ( i ) ;}
d . pop front () ;
d . pop back ( ) ;
d . e r a s e ( d . b e g i n ( )+ 1 ) ;
d . e r a s e ( d . end ( )− 1 ) ;
f or ( i t e= d . b e g i n ( ) ; i t e !=d . end ( ) ; i t e ++)
cout <<∗i t e <<” ” ;
cout<<e n d l ;
}

// end ( ) de deque d e v u e l v e e l e l e m e n t o s i g u i e n t e a l u l t i m o ,
// a s i que a l b o r r a r d . end ( )−1 estamos borrando e l u l t i m o .
// Primero agregamos a l p r i n c i p i o y a l f i n a l d e s d e e l 0 a l 9 i n c l u i d o s .
// 98765432100123456789
// Luego q u i t a m o s e l primero ( e l 9)
// 8765432100123456789
// Quitamos e l u l t i m o ( e l o t r o 9)

Página 6 de 16
Errores

// 876543210012345678
// Borramos e l primero + 1 ( a q u i s i , \ v e r b @ b e g i n ( )@ d e v u e l v e e l primer
e l e m e n t o ) que e s e l 7
// 86543210012345678
// Borramos \ verb@end ( )−1@, e s d e c i r , e l u l t i m o , o sea , e l 8
// 8654321001234567
// Desde e l p r i n c i p i o h a s t a uno a n t e s de \ verb@end ( )@, e s d e c i r , h a s t a e l
ultimo elemento
// 8654321001234567

8. [008.txt] Detecta los posibles errores en los siguientes segmentos de programa e intenta corregirlos razonando
su corrección:
c l a s s Empleado {
public :
Empleado ( int s ) : s a l a r i o ( s ) {}
int L e e S a l a r i o ( ) { return s a l a r i o ; }
protected :
int s a l a r i o ;
};
class Estudiante {
public :
E s t u d i a n t e ( f l o a t no ) : nota ( no ) {}
void ModificaNota ( f l o a t &no = 0 ) { nota = no ; }
protected :
f l o a t nota ;
};
c l a s s B e c a r i o : private Empleado , public E s t u d i a n t e {
public :
B e c a r i o ( ) {}
B e c a r i o ( char ∗n , int s , f l o a t no )
{ s t r c p y ( nombre , n ) ; }
char ∗LeeNombre ( ) { return nombre ; }
private :
char nombre [ 3 0 ] ;
};
int main ( )
{
B e c a r i o b1 ( ” Pepe ” , 3 0 0 , 7 . 5 ) ;
v e c t o r <B e c a r i o > v e c t o r b ;
v e c t o r <B e c a r i o > : : i t e r a t o r i t e ;
c o u t << ” S a l a r i o : ” << b1 . L e e S a l a r i o ( ) << e n d l ;
v e c t o r b . push back ( b1 ) ;
i t e = vectorb . begin () ;
c o u t <<”Nombre : ” << i t e . LeeNombre ( ) <<e n d l ;
return 0 ;
}

Solución:

#include <i o s t r e a m >


#include <c s t r i n g >
#include <v e c t o r >
using namespace s t d ;
c l a s s Empleado {

Página 7 de 16
Errores

public :
Empleado ( int s ) : s a l a r i o ( s ) {}
int L e e S a l a r i o ( ) { return s a l a r i o ; }
protected :
int s a l a r i o ;
};
class Estudiante {
public :
E s t u d i a n t e ( f l o a t no ) : nota ( no ) {}
// e s t e parametro no puede t e n e r v a l o r por d e f e c t o pues s e toma por
referencia
// v o i d ModificaNota ( f l o a t &no = 0) { nota = no ; }
void ModificaNota ( f l o a t &no ) { nota = no ; }
protected :
f l o a t nota ;
};
c l a s s B e c a r i o : private Empleado , public E s t u d i a n t e {
public :
// Los c o n s t r u c t o r e s han de l l a m a r a l o s c o n s t r u c t o r e s
// adecuados de Empleado y E s t u d i a n t e
// B e c a r i o ( ) {}
// B e c a r i o ( char ∗n , i n t s , f l o a t no )
// { s t r c p y ( nombre , n ) ; }
Becario ( )
: E s t u d i a n t e ( 0 . 0 ) , Empleado ( 0 ) {
}
B e c a r i o ( char ∗n , int s , f l o a t no ) :
E s t u d i a n t e ( no ) , Empleado ( s ) {
s t r c p y ( nombre , n ) ;
}
char ∗LeeNombre ( ) { return nombre ; }
private :
char nombre [ 3 0 ] ;
};
int main ( ) {
B e c a r i o b1 ( ” Pepe ” , 3 0 0 , 7 . 5 ) ;
v e c t o r <B e c a r i o > v e c t o r b ;
v e c t o r <B e c a r i o > : : i t e r a t o r i t e ;
// no podemos a c c e d e r a l metodo L e e S a l a r i o ( ) de B e c a r i o pues ,
// a l h e r e d a r de manera p r i v a d a l o ha o c u l t a d o .
// c o u t << ” S a l a r i o : ” << b1 . L e e S a l a r i o ( ) << e n d l ;
v e c t o r b . push back ( b1 ) ;
i t e = vectorb . begin () ;
// s i i t e e s un i t e r a d o r , usaremos l a f l e c h a
// c o u t <<”Nombre : ” << i t e . LeeNombre ( ) <<e n d l ;
c o u t <<”Nombre : ” << i t e −>LeeNombre ( ) <<e n d l ;
return 0 ;
}

9. [009.txt]
int main ( )
{ enum { Norte , Sur , Este , Oeste } zona ;
char ∗ p u n t o s C a r d i n a l e s [ ] = { ” Norte ” , ” Sur ” , ” Este ” , ” Oeste ” } ;
char ∗nombre ;
try {
c o u t << ” \ n I n t r o d u c e e l nombre de l a zona : ” ;

Página 8 de 16
Errores

c i n >> nombre ;
c o u t << ” \ n I n t r o d u c e e l punto c a r d i n a l : ” ;
c i n >> zona ;
i f ( zona == ” Norte ” )
throw 0 ;
e l s e cout<<”\nLa zona e s : ” <<p u n t o s C a r d i n a l e s [ zona ] ;
catch ( int )
{ c o u t << ”\ nHace mucho f r i o . \ n” ; }
}
return 0 ;
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
int main ( ) {
enum { Norte , Sur , Este , Oeste } zona ;
// c o n v e r s i o n f o r z a d a de char ∗ a c o n s t char ∗
// char ∗ p u n t o s C a r d i n a l e s [ ] ={ ” Norte ” , ” Sur ” , ” E s t e ” ,” Oeste ” } ;
const char ∗ p u n t o s C a r d i n a l e s [ ] = { ” Norte ” , ” Sur ” , ” Este ” , ” Oeste ” } ;
// S i no r e s e r v a m o s e s p a c i o no podemos u s a r c i n
// char ∗ nombre ;
char nombre [ 5 0 ] ;
try {
c o u t << ”\ n I n t r o d u c e e l nombre de l a zona : ” ;
c i n >> nombre ;
c o u t << ”\ n I n t r o d u c e e l punto c a r d i n a l : ” ;
// a un enumerado no s e l e puede a s i g n a r un e n t e r o , a l r e v e s s i .
//En l u g a r de p e d i r zona d e b e r i a p e d i r s e un e n t e r o numZona y
// l u e g o cambiar l a l i n e a de comparacion i f ( zona == ” Norte ” )
// por i f ( numZona == Norte ) y l u e g o o b t e n e r e l nombre con
// p u n t o s C a r d i n a l e s [ numZona ]
// c i n >> zona ;
int numZona ;
c i n >> numZona ;
i f ( numZona == Norte )
throw 0 ;
// e l s e cout <<”\nLa zona e s : ” <<p u n t o s C a r d i n a l e s [ zona ] ;
e l s e cout<<”\nLa zona e s : ” <<p u n t o s C a r d i n a l e s [ numZona ] ;
// f a l t a una l l a v e de c i e r r e a n t e s de c a t c h
}
catch ( int )
{ c o u t << ”\ nHace mucho f r i o . \ n” ; }
// s o b r a e s t a l l a v e
// }
return 0 ;
}

10. [010.txt] ¿Existen errores en el siguiente código? Razónalo e intenta corregirlos:


c l a s s Fecha {
int dia , mes , anho ;
public :
Fecha ( int d , int m, int a ) { d i a = d ; mes = m; anho = a ; }
int g e t D i a ( ) { return d i a ; } ;
friend int sumaFecha ( int num , Fecha f ) { return num + this−>d i a ; }

Página 9 de 16
Errores

};

Solución:

c l a s s Fecha {
int dia , mes , anho ;
public :
Fecha ( int d , int m, int a ) { d i a = d ; mes = m; anho = a ; }
int g e t D i a ( ) { return d i a ; } ;
// f r i e n d i n t sumaFecha ( i n t num , Fecha f ) { r e t u r n num + t h i s −>d i a ; }
friend int sumaFecha ( int num , Fecha f ) { return num + f . d i a ; }
};
/∗
En una f u n c i o n g l o b a l no e x i s t e e l p u n t e r o t h i s , aunque s e d e c l a r e amiga .
Puede c a m b i a r s e t h i s −>d i a por f . g e t D i a ( ) y d e f i n i r l a f u n c i o n f u e r a de
l a c l a s e , no n e c e s i t a s e r amiga . S i s e d e f i n e d e n t r o como f u n c i o n miembro
e n t o n c e s p o d r i a p r e s c i n d i r s e d e l parametro Fecha f
∗/

11. [011.txt] ¿Son correctas las siguientes declaraciones y definiciones? Razónalo brevemente y con claridad.
c l a s s Fruta {
float precio ;
public :
Fruta ( int p r e c = 0 . 0 ) { p r e c i o = p r e c ; }
};
c l a s s Manzana : public Fruta {
s t r i n g nombre ;
public :
Manzana ( s t r i n g nom = ” r e i n e t a ” ) : Fruta ( int p r e c ) { nombre = nom ; }
s t r i n g getNombre ( ) { return nombre ; }
f l o a t g e t P r e c i o ( ) { return p r e c i o ; }
};

Solución:

#include <i o s t r e a m >


using namespace s t d ;
c l a s s Fruta {
// Hacemos p r e c i o p r o t e c t e d para que pueda a c c e d e r
// e l metodo g e t P r e c i o de Manzana
protected :
float precio ;
public :
// Decimales en un i n t
// Fruta ( i n t p r e c = 0 . 0 ) { p r e c i o = p r e c ; }
Fruta ( f l o a t p r e c = 0 . 0 ) { p r e c i o = p r e c ; }
};
c l a s s Manzana : public Fruta {
s t r i n g nombre ;
public :
// Error en l a l l a m a d a a l c o n s t r u c t o r s u p e r i o r
//Manzana ( s t r i n g nom = ” r e i n e t a ”)
Manzana ( int prec , s t r i n g nom = ” r e i n e t a ” )
// : Fruta ( i n t p r e c ) {

Página 10 de 16
Errores

: Fruta ( p r e c ) {
nombre = nom ;
}
s t r i n g getNombre ( ) { return nombre ; }
f l o a t g e t P r e c i o ( ) { return p r e c i o ; }
};

12. [012.txt]
Completar el siguiente trozo de código de forma que se traten correctamente las excepciones:
int l e e r E d a d ( ) ;
void main ( ) {
int edad ;
edad = l e e r E d a d ( ) ;
i f ( edad < 0 )
throw edad ;
e l s e i f ( ( edad > 90 ) && ( edad < 120 ) )
throw ” A l u c i n a n t e ” ;
e l s e i f ( edad > 120 )
throw s t a t i c c a s t <f l o a t > ( edad ) ;
}
int l e e r E d a d ( ) {
int e ;
c o u t << ” I n t r o d u c e l a edad : ” ;
c i n >> e ;
return e ;
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
int l e e r E d a d ( ) ;
int main ( ) {
int edad ;
edad = l e e r E d a d ( ) ;
try {
i f ( edad < 0 )
throw edad ;
e l s e i f ( ( edad > 90 ) && ( edad < 120 ) )
throw ” A l u c i n a n t e ” ;
e l s e i f ( edad > 120 )
throw s t a t i c c a s t <f l o a t > ( edad ) ;
} catch ( int e ) {

} catch ( char ∗ e ) {

} catch ( f l o a t e ) {

}
}
int l e e r E d a d ( ) {
int e ;
c o u t << ” I n t r o d u c e l a edad : ” ;
c i n >> e ;

Página 11 de 16
Errores

return e ;
}

13. [013.txt]
Indicar cuál será la salida del siguiente programa, explicando brevemente cómo se llega a esa conclusión.
#include <i o s t r e a m >
using namespace s t d ;
#include <v e c t o r >
double suma ( v e c t o r < f l o a t > v ) ;
void main ( ) {
vector < float > vect ;
f or ( unsigned int i = 0 ; i != 1 0 ; i++ )
v e c t . push back ( i ) ;
c o u t << ”La suma e s : ” << suma ( v e c t ) << e n d l ;
}
double suma ( v e c t o r < f l o a t > v ) {
double r e s u l t a d o ( 0 ) ;
f or ( v e c t o r < f l o a t >:: i t e r a t o r i t e = v . b e g i n ( ) ; i t e != v . end ( ) ; i t e ++ )
r e s u l t a d o += ∗ i t e ;
return r e s u l t a d o ;
}

Solución:

/∗
Aparecera l a f r a s e ”La suma e s : 4 5 ” . R e a l i z a l a suma de l o s 10 p r i m e r o s
numeros f l o a t que guarda en un v e c t o r .
Para r e c o r r e r e l v e c t o r s e usa un i t e r a d o r que empieza apuntando a l
primer e l e m e n t o y que termina cuando apunta a l que d e b e r i a e s t a r
despues del ultimo .
∗/

14. [014.txt] Detecta los posibles errores en los siguientes segmentos de programa e intenta corregirlos razonando
su corrección:
i n l i n e main ( )
{ int x , y ;
c i n >>x>>y ;
cout<<” El r e s u l t a d o de m u l t i p l i c a r x e y e s : ”<<m u l t i p l i c a r ( x , y ) ;
}
int m u l t i p l i c a r ( int &a , int b )
{ return ( ∗ a ) ∗b ; }

Solución:

#include <i o s t r e a m >


using namespace s t d ;
int m u l t i p l i c a r ( int a , int b ) ;
// i n l i n e main ( ) {
int main ( ) {
int x , y ;
c i n >>x>>y ;

Página 12 de 16
Errores

cout<<” El r e s u l t a d o de m u l t i p l i c a r x e y e s : ”<<m u l t i p l i c a r ( x , y ) ;
}
/∗
e s t a a c c e d i e n d o a l v a l o r de a con ∗a ,
como s i f u e s e un puntero , y s o l o e s una r e f e r e n c i a .
D e b e r i a s e r r e t u r n a∗ b ; y tampoco hace f a l t a que s e
p a s e por r e f e r e n c i a .
∗/
// i n t m u l t i p l i c a r ( i n t &a , i n t b ) {
int m u l t i p l i c a r ( int a , int b ) {
return a ∗b ;
}

15. [015.txt]
float v (8) ;
f l o a t c a l c u l o ( f l o a t x=v , int y=1, f l o a t z =3.5)
{ return x+y+z ; }
bool f u n c i o n 1 ( )
{ i f ( c a l c u l o ( 3 . 2 , 7 ) !=10) return true ;
e l s e return f a l s e ; }

Solución:

float v (8) ;
f l o a t c a l c u l o ( f l o a t x=v , int y=1, f l o a t z =3.5) {
return x+y+z ;
}
bool f u n c i o n 1 ( ) {
i f ( c a l c u l o ( 3 . 2 , 7 ) !=10)
return true ;
e l s e return f a l s e ;
}
/∗ Esta t o d o b i e n , s e r e a l i z a n c o n v e r s i o n e s a u t o m a t i c a s ∗/

16. [016.txt]
int f u n c i o n 1 ( r e g i s t e r int x , f l o a t y )
{ extern int a ;
c i n >>a ;
return ( x−y )+a ; }

Solución:

#include <i o s t r e a m >


using namespace s t d ;
// d e c l a r a que d e v o l v e r a un i n t pero d e v u e l v e un f l o a t .
// i n t f u n c i o n 1 ( r e g i s t e r i n t x , f l o a t y )
f l o a t f u n c i o n 1 ( r e g i s t e r int x , f l o a t y )
{ extern int a ;
c i n >>a ;
return ( x−y )+a ; }

Página 13 de 16
Errores

17. [017.txt]
struct alumno
{ char nombre [ 4 0 ] ;
enum { Suspenso , Aprobado , Notable , S o b r e s a l i e n t e , M a t r i c u l a } nota ; }
alumno c u r s o [ 1 5 0 ] ;
....
void v i s u a l i z a r ( alumno c u r s o [ ] )
{ char ∗ c a l i f i c a c i o n e s [ ]={ ” Suspenso ” , ” Aprobado ” ,
” Notable ” , ” S o b r e s a l i e n t e ” , ” M a t r i c u l a ” } ;
f or ( int i =1; i <=150; i ++)
{ cout<<”Alumno : ”<<c u r s o [ i ]<<” Nota : ”<< c a l i f i c a c i o n e s [ nota ] ; }
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
struct alumno
{ char nombre [ 4 0 ] ;
// f a l t a un punto y coma a l f i n a l de l a s t r u c t
//enum { Suspenso , Aprobado , N o t a b l e , S o b r e s a l i e n t e , M a t r i c u l a } nota ; }
enum { Suspenso , Aprobado , Notable , S o b r e s a l i e n t e , M a t r i c u l a } nota ; } ;
alumno c u r s o [ 1 5 0 ] ;
// . . . .
void v i s u a l i z a r ( alumno c u r s o [ ] )
// { char ∗ c a l i f i c a c i o n e s [ ]={” Suspenso ” , ” Aprobado ” ,
{ const char ∗ c a l i f i c a c i o n e s [ ]={ ” Suspenso ” , ” Aprobado ” ,
” Notable ” , ” S o b r e s a l i e n t e ” , ” M a t r i c u l a ” } ;
// f o r ( i n t i =1; i <=150; i ++)
f or ( int i =1; i <150; i ++)
// { cout <<”Alumno : ”<<c u r s o [ i ]<<” Nota : ”<< c a l i f i c a c i o n e s [ nota ] ; }
{ cout<<”Alumno : ”<<c u r s o [ i ] . nombre<<” Nota : ”<< c a l i f i c a c i o n e s [ c u r s o [ i ] .
nota ] ; }
}

18. [018.txt] Completa los siguientes segmentos de programa para obtener las salidas indicadas: Con esta sentencia
for se pretende calcular la siguiente suma: 1/2 + 1/3 + 1/4 + ... + 1/50
f l o a t suma ( 0 ) , j ;
int i =2;
f or ( );
c o u t <<” R e s u l t a d o : ”<<suma ;

Solución:

#include <i o s t r e a m >


using namespace s t d ;
int main ( ) {
f l o a t suma ( 0 ) , j ;
int i =2;
f or ( j =2; j <=50; suma += ( 1 / j ++)) ;
c o u t <<” R e s u l t a d o : ”<<suma ;
}

Página 14 de 16
Errores

19. [019.txt] El siguiente programa usa punteros a funciones para obtener el mayor y menor de los elementos
introducidos en un array bidimensional
#include <i o s t r e a m . h>
const int f i l =5; const int c o l =5;
float buscar ( float matriz [ ] [ c o l ] , ) ;
f l o a t mayor ( f l o a t x , f l o a t y ) ;
f l o a t menor ( f l o a t x , f l o a t y ) ;
int menu ( ) ;
void main ( )
{ f l o a t m a t r i z [ f i l ] [ c o l ]={0} , ;
cout<<” I n t r o d u c e l o s e l e m e n t o s de l a m a t r i z \n” ;
f or ( int i =0; i < f i l ; i ++)
f or ( int j =0; j <c o l ; j ++) {
cout<<” Matriz [ ”<<i <<” ] [ ”<<j <<” ] ” ; c i n >>m a t r i z [ i ] [ j ] ;
}
switch ( menu ( ) ) {
case 1 : p=mayor ; break ;
case 2 : p=menor ; break ;
}
cout<<” El e l e m e n t o l o c a l i z a d o e s : ”<<b u s c a r ( matriz , p )<<e n d l ;
}
int menu ( ) {
int op =0;
do {
cout<<”MENU\n 1 . Mayor\n 2 . Menor” ; c i n >>op ;
} while ( op <1) | | ( op >2) ) ;
return op ;
}
float buscar ( float matriz [ ] [ c o l ] , ) {
f l o a t r e s u l=m a t r i z [ 0 ] [ 0 ] ;
f or ( int i =0; i < f i l ; i ++)
f or ( int j =0; j <c o l ; j ++)
return r e s u l ;
}
f l o a t mayor ( f l o a t x , f l o a t y ) {
i f ( x>y )
return x ;
e l s e return y ;
}
f l o a t menor ( f l o a t x , f l o a t y ) {
i f ( x<y )
return x ;
e l s e return y ;
}

Solución:

#include <i o s t r e a m >


using namespace s t d ;
const int f i l =5; const int c o l =5;
// f l o a t b u s c a r ( f l o a t m a t r i z [ ] [ c o l ] , ) ;
float buscar ( float matriz [ ] [ c o l ] , float (∗p) ( float x , float y ) ) ;
f l o a t mayor ( f l o a t x , f l o a t y ) ;
f l o a t menor ( f l o a t x , f l o a t y ) ;
// El p u n t e r o p ha de d e c l a r a r s e a n t e s de su uso como :
float (∗p) ( float x , float y ) ;
int menu ( ) ;

Página 15 de 16
Errores

//main d e b e d e v o l v e r un i n t
int main ( )
{ float matriz [ f i l ] [ c o l ]={0};
cout<<” I n t r o d u c e l o s e l e m e n t o s de l a m a t r i z \n” ;
f or ( int i =0; i < f i l ; i ++)
f or ( int j =0; j <c o l ; j ++) {
cout<<” Matriz [ ”<<i <<” ] [ ”<<j <<” ] ” ; c i n >>m a t r i z [ i ] [ j ] ;
}
switch ( menu ( ) ) {
case 1 : p=mayor ; break ;
case 2 : p=menor ; break ;
}
cout<<” El e l e m e n t o l o c a l i z a d o e s : ”<<b u s c a r ( matriz , p )<<e n d l ;
}
int menu ( ) {
int op =0;
do {
cout<<”MENU\n 1 . Mayor\n 2 . Menor” ; c i n >>op ;
// } w h i l e ( op <1) | | ( op >2) ) ;
} while ( ( op <1) | | ( op >2) ) ;
return op ;
}
//Ha de a g r e g a r s e un segundo parametro a l a f u n c i o n b u s c a r como :
float buscar ( float matriz [ ] [ c o l ] , float (∗p) ( float x , float y ) ) {
// f l o a t b u s c a r ( f l o a t m a t r i z [ ] [ c o l ] , ) {
// Dentro d e l b u c l e s e l l a m a a l a f u n c i o n a l a que apunte e l p u n t e r o p
float r e s u l = matriz [ 0 ] [ 0 ] ;
f or ( int i =0; i < f i l ; i ++){
f or ( int j =0; j <c o l ; j ++){
r e s u l = p( resul , matriz [ i ] [ j ] ) ;
}
return r e s u l ;
}

}
f l o a t mayor ( f l o a t x , f l o a t y ) {
i f ( x>y )
return x ;
e l s e return y ;
}
f l o a t menor ( f l o a t x , f l o a t y ) {
i f ( x<y )
return x ;
e l s e return y ;
}

Página 16 de 16

Potrebbero piacerti anche

  • SAP Logon Groups
    SAP Logon Groups
    Documento4 pagine
    SAP Logon Groups
    Pepe
    Nessuna valutazione finora
  • Varios 2
    Varios 2
    Documento15 pagine
    Varios 2
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Varios 1
    Varios 1
    Documento12 pagine
    Varios 1
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2005
    Sept 2005
    Documento7 pagine
    Sept 2005
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Punteros
    Punteros
    Documento7 pagine
    Punteros
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sobrecarga
    Sobrecarga
    Documento11 pagine
    Sobrecarga
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Practica 2010
    Practica 2010
    Documento18 pagine
    Practica 2010
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2007 P
    Sept 2007 P
    Documento5 pagine
    Sept 2007 P
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Preguntas Varias. Nivel 1
    Preguntas Varias. Nivel 1
    Documento13 pagine
    Preguntas Varias. Nivel 1
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2007 T
    Sept 2007 T
    Documento4 pagine
    Sept 2007 T
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2006
    Sept 2006
    Documento5 pagine
    Sept 2006
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2003
    Sept 2003
    Documento11 pagine
    Sept 2003
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Sept 2004
    Sept 2004
    Documento7 pagine
    Sept 2004
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Practica2010 2
    Practica2010 2
    Documento20 pagine
    Practica2010 2
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Practica2010 3
    Practica2010 3
    Documento19 pagine
    Practica2010 3
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Plantillas
    Plantillas
    Documento5 pagine
    Plantillas
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Practica 2008
    Practica 2008
    Documento7 pagine
    Practica 2008
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Ligadura
    Ligadura
    Documento10 pagine
    Ligadura
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Practica 2009
    Practica 2009
    Documento17 pagine
    Practica 2009
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2008 Practica
    Junio 2008 Practica
    Documento2 pagine
    Junio 2008 Practica
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2007 T
    Junio 2007 T
    Documento7 pagine
    Junio 2007 T
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2004
    Junio 2004
    Documento6 pagine
    Junio 2004
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2007 P
    Junio 2007 P
    Documento4 pagine
    Junio 2007 P
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Ficheros
    Ficheros
    Documento23 pagine
    Ficheros
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2006
    Junio 2006
    Documento7 pagine
    Junio 2006
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Funciones
    Funciones
    Documento2 pagine
    Funciones
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Cadenas
    Cadenas
    Documento5 pagine
    Cadenas
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Junio 2005
    Junio 2005
    Documento8 pagine
    Junio 2005
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Examen Ejemplo
    Examen Ejemplo
    Documento8 pagine
    Examen Ejemplo
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Compendio
    Compendio
    Documento13 pagine
    Compendio
    Antonio Vazquez Araujo
    Nessuna valutazione finora
  • Lista de Puertos
    Lista de Puertos
    Documento30 pagine
    Lista de Puertos
    Henry Vega Chupos
    Nessuna valutazione finora
  • Conectividad
    Conectividad
    Documento4 pagine
    Conectividad
    FM Reflejos Sierra de la Ventana
    Nessuna valutazione finora
  • Soluciones Mantenimiento Baterias
    Soluciones Mantenimiento Baterias
    Documento9 pagine
    Soluciones Mantenimiento Baterias
    Jossuarmm
    Nessuna valutazione finora
  • Configuración de Cuentas de Usuario Estándar
    Configuración de Cuentas de Usuario Estándar
    Documento4 pagine
    Configuración de Cuentas de Usuario Estándar
    Jose A Aparicio Fernandez
    Nessuna valutazione finora
  • Aviacion (0355) Server 2
    Aviacion (0355) Server 2
    Documento2 pagine
    Aviacion (0355) Server 2
    Marcia Ccasani Tito
    Nessuna valutazione finora
  • Mejora servicio educativo Reino de Bél
    Mejora servicio educativo Reino de Bél
    Documento30 pagine
    Mejora servicio educativo Reino de Bél
    Erikita Trujillo Ticona
    Nessuna valutazione finora
  • Comandos Xinetd
    Comandos Xinetd
    Documento4 pagine
    Comandos Xinetd
    david_cdba
    Nessuna valutazione finora
  • Aksh Optifibre Ad10-Xxx-S - Datasheet PDF
    Aksh Optifibre Ad10-Xxx-S - Datasheet PDF
    Documento9 pagine
    Aksh Optifibre Ad10-Xxx-S - Datasheet PDF
    Luis Chavez
    Nessuna valutazione finora
  • Sitema Ditribuido
    Sitema Ditribuido
    Documento53 pagine
    Sitema Ditribuido
    Ruben Calle
    Nessuna valutazione finora
  • Demodulacion Ask
    Demodulacion Ask
    Documento8 pagine
    Demodulacion Ask
    Juan Arnao Alarcon
    Nessuna valutazione finora
  • Manual Ilium S700
    Manual Ilium S700
    Documento39 pagine
    Manual Ilium S700
    Sofía Lecter
    Nessuna valutazione finora
  • Manual de Programacion PC KX-TDA100D
    Manual de Programacion PC KX-TDA100D
    Documento862 pagine
    Manual de Programacion PC KX-TDA100D
    Abdiel Castillo
    Nessuna valutazione finora
  • Diferencias Debian y Centos LINUX
    Diferencias Debian y Centos LINUX
    Documento9 pagine
    Diferencias Debian y Centos LINUX
    wwallace82
    Nessuna valutazione finora
  • Ventajas y Desventajas Del Internet en La Actual Id Ad
    Ventajas y Desventajas Del Internet en La Actual Id Ad
    Documento5 pagine
    Ventajas y Desventajas Del Internet en La Actual Id Ad
    Ronald André López Montero
    Nessuna valutazione finora
  • CCNA 1 Capitulo 3
    CCNA 1 Capitulo 3
    Documento4 pagine
    CCNA 1 Capitulo 3
    Wlladimir Chavarria
    Nessuna valutazione finora
  • Musi 009 T 4 Tra
    Musi 009 T 4 Tra
    Documento4 pagine
    Musi 009 T 4 Tra
    July m92
    0% (1)
  • Solución VOIP
    Solución VOIP
    Documento9 pagine
    Solución VOIP
    eliana mcm
    Nessuna valutazione finora
  • Creando Ayudas HTML (CHM)
    Creando Ayudas HTML (CHM)
    Documento7 pagine
    Creando Ayudas HTML (CHM)
    Cesar Llave
    Nessuna valutazione finora
  • Topologia y Tipologia Gloria
    Topologia y Tipologia Gloria
    Documento8 pagine
    Topologia y Tipologia Gloria
    Anonymous P8x71xaTp1
    Nessuna valutazione finora
  • Las Redes Sociales Lo Que Hacen Sus Hijos en Internet
    Las Redes Sociales Lo Que Hacen Sus Hijos en Internet
    Documento71 pagine
    Las Redes Sociales Lo Que Hacen Sus Hijos en Internet
    Ronald Rodolfo Lam Coleman
    Nessuna valutazione finora
  • Tipos de comunicaciones móviles
    Tipos de comunicaciones móviles
    Documento6 pagine
    Tipos de comunicaciones móviles
    phandres22
    Nessuna valutazione finora
  • Ma Lucent
    Ma Lucent
    Documento39 pagine
    Ma Lucent
    raj_singh_14
    Nessuna valutazione finora
  • Memoria
    Memoria
    Documento23 pagine
    Memoria
    Cuaderno Intercultural
    Nessuna valutazione finora
  • Celulares en Perú Renovación y Más Catálogo Movistar
    Celulares en Perú Renovación y Más Catálogo Movistar
    Documento1 pagina
    Celulares en Perú Renovación y Más Catálogo Movistar
    Gustavo Martinez
    Nessuna valutazione finora
  • Introducción A Las Redes Sociales
    Introducción A Las Redes Sociales
    Documento68 pagine
    Introducción A Las Redes Sociales
    saramiguelyaitana
    Nessuna valutazione finora
  • Ficha Tecnica - Mauell ME 3011
    Ficha Tecnica - Mauell ME 3011
    Documento4 pagine
    Ficha Tecnica - Mauell ME 3011
    Gabriel
    Nessuna valutazione finora
  • 5.capítulo 4 - S.E Carapongo
    5.capítulo 4 - S.E Carapongo
    Documento50 pagine
    5.capítulo 4 - S.E Carapongo
    Daniel Uculmana Lema
    100% (1)
  • R.T.N. 08019995344765 260014 Cotización
    R.T.N. 08019995344765 260014 Cotización
    Documento1 pagina
    R.T.N. 08019995344765 260014 Cotización
    Oscar Martinez
    Nessuna valutazione finora
  • Caracteristicas Electricas Rs 422 Rs 232
    Caracteristicas Electricas Rs 422 Rs 232
    Documento5 pagine
    Caracteristicas Electricas Rs 422 Rs 232
    Jesús CosLey
    Nessuna valutazione finora