Está en la página 1de 3

Estructura de datos

ESTRUCTURAS VARIAS - EJERCICIOS DE APOYO

1. HALLAR EL PROMEDIO DE ELEMENTOS DE UNA CIFRA


(3, 56, 7, 8, 23, 11) ==> prom=18/3=6

SOLUCIÓN VECTOR
float promediocifra(int V[], int n)
{ float prom=0,c=0;
int i;
for (i = 0; i <= n-1; i++)
{ if(V[i]>=0 && V[i]<=9)
{ prom=prom+V[i];
c=c+1;
}
}
if(c!=0)
prom=prom/c;
return(prom);
}

SOLUCIÓN COLA
int main()
{ nodo *frente=NULL;
nodo *fin=NULL;
int dato,prom,c;
//Llena datos en la cola
do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarcola(frente,fin,dato);
}while(!(dato==0));

//Ejercicio: Promedio
prom=0;
c=0;
while(frente!=NULL)
{ sacarcola(frente,fin,dato);
if(dato>=0 && dato<=9)
{ prom=prom+dato;
c=c+1;
}
}
if(c!=0)
prom=prom/c;
cout<<"El prom es: "<<prom;
return 0;
}
1 Lic. Patricia Fernández Quisbert
Estructura de datos

2. HALLAR EL MENOR (5, 7, 8, 3, 11) ==> men=3

SOLUCIÓN VECTOR
void menor(int V[], int n)
{ int men,i;
men=V[0];
for (i = 0; i <= n-1; i++)
{ if(V[i]<men)
{ men=V[i];
}
}
cout<<"El menor es: "<<men;
}

3. ROTAR LOS ELEMENTOS A LA DERECHA


(5, 3, 8, 2, 11)
==> (11, 5, 3, 8, 2)

SOLUCIÓN VECTOR
void rotarder(int V[], int n)
{ int aux,i;
aux=V[n-1];
for (i = n-2; i >= 0; i--) //DESC
{ V[i+1]=V[i];

}
V[0]=aux;
}

SOLUCIÓN PILA
int main()
{ nodo *Pila=NULL;
nodo *Paux=NULL;
int dato,aux;
do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarpila(Pila,dato);
}while(!(dato==0));

//ROTAR
sacarpila(Pila, aux);
while(Pila!=NULL) //Traslada Pila a Paux
{ sacarpila(Pila, dato);
agregarpila(Paux,dato);
}
2 Lic. Patricia Fernández Quisbert
Estructura de datos

//RECONSTRUIR
agregarpila(Pila,aux);
while(Paux!=NULL) //Traslada Paux a Pila
{ sacarpila(Paux, dato);
agregarpila(Pila,dato);
}

cout<<"\nLa pila es: ";


while(Pila!=NULL)
{ sacarpila(Pila, dato);
cout<<dato<<",";
}
return 0;
}

SOLUCIÓN COLA
int main()
{ nodo *frente=NULL;
nodo *fin=NULL;
nodo *frenteaux=NULL;
nodo *finaux=NULL;
int dato,aux;
//Llena datos en la cola
do{cout<<"Numero : ";
cin>>dato;
if(dato!=0)
agregarcola(frente,fin,dato);
}while(!(dato==0));
//Ejercicio: Rotar
while(frente->siguiente!=NULL) //Traslada (no ult) Cola a Caux
{ sacarcola(frente,fin,dato);
agregarcola(frenteaux,finaux,dato);
}
sacarcola(frente,fin,aux);
//RECONSTRUIR
agregarcola(frente, fin, aux);
while(frenteaux!=NULL) //Traslada Caux a Cola
{ sacarcola(frenteaux, finaux, dato);
agregarcola(frente, fin, dato);
}
//Imprime la cola
cout<<"\nLa cola es: ";
while(frente!=NULL)
{ sacarcola(frente,fin,dato);
cout<<dato<<",";
}
return 0;
}
3 Lic. Patricia Fernández Quisbert

También podría gustarte