Está en la página 1de 10

25 EJERCICIOS DE ARRAYS EN C {

int x,cont,z,i,tabla[100];
i=0;
1. Que rellene un array con los 100 primeros números for (x=1;x<=100;x++)
enteros y los muestre en pantalla en orden ascendente. {
cont=0;
#include <stdio.h> for (z=1;z<=x;z++)
#include <stdlib.h> {
int main(void) if (x%z==0)
{ {
int x,tabla[100]; cont++;
}
for (x=1;x<=100;x++) }
{ if (cont==2 || z==1 || z==0)
tabla[x]=x; {
} tabla[i]=x;
for (x=1;x<=100;x++) i++;
{ }
printf("%d\n",tabla[x]); }
} for (x=0;x<i;x++)
{
system("PAUSE"); printf("%d\n",tabla[x]);
return 0; }
}
system("PAUSE");
2. Que rellene un array con los 100 primeros números return 0;
enteros y los muestre en pantalla en orden }
descendente.
4. Que rellene un array con los números pares
#include <stdio.h> comprendidos entre 1 y 100 y los muestre en pantalla
#include <stdlib.h> en orden ascendente.
int main(void)
{ #include <stdio.h>
int x,tabla[100]; #include <stdlib.h>
for (x=1;x<=100;x++)
{ int main(void)
tabla[x]=x; {
} int x,cont,z,i,tabla[100];
for (x=100;x>=1;x--) i=0;
{ for (x=1;x<=100;x++)
printf("%d\n",tabla[x]); {
} cont=0;
system("PAUSE"); if (x%2==0)
return 0; {
} tabla[i]=x;
i++;
3. Que rellene un array con los números primos }
comprendidos entre 1 y 100 y los muestre en pantalla }
en orden ascendente. for (x=0;x<i;x++)
{
#include <stdio.h> printf("%d\n",tabla[x]);
#include <stdlib.h> }
int main(void) system("PAUSE");
return 0; res=res-tabla[x];
} mul=mul*tabla[x];
div=div/tabla[x];
5. Que rellene un array con los números impares }
comprendidos entre 1 y 100 y los muestre en pantalla printf("Suma: %d\n",sum);
en orden ascendente. printf("Resta: %d\n",res);
printf("Multiplicación: %d\n",mul);
#include <stdio.h> printf("División: %d\n",div);
#include <stdlib.h>
system("PAUSE");
int main(void) return 0;
{ }
int x,cont,z,i,tabla[100];
i=0; 7. Que lea 10 números por teclado, los almacene en un
for (x=1;x<=100;x++) array y los ordene de forma ascendente.
{
cont=0; #include <stdio.h>
if (x%2==1) #include <stdlib.h>
{ int main()
tabla[i]=x; {
i++; float aux, numeros[10];
} int i,j,n=10;
} for (i=0;i<n;i++){
for (x=0;x<i;x++) printf("Escriba un número");
{ scanf("%f",&numeros[i]);
printf("%d\n",tabla[x]); }
} for(i=0;i<n-1;i++)
system("PAUSE"); {
return 0; for(j=i+1;j<n;j++)
} {
if(numeros[i]<numeros[j])
6. Que lea 10 números por teclado, los almacene en un {
array y muestre la suma, resta, multiplicación y división aux=numeros[i];
de todos. numeros[i]=numeros[j];
numeros[j]=aux;
#include <stdio.h> }
#include <stdlib.h> }
int main(void) }
{ for (i=n-1;i>=0;i--){
int x,tabla[10]; printf("%f\n",numeros[i]);
int sum,res,mul,div; }
for (x=0;x<10;x++) system("PAUSE");
{ return 0;
printf("Introduzca número\n"); }
scanf("%d",&tabla[x]);
} 8. Que lea 10 números por teclado, 5 para un array y 5
sum=tabla[0]; para otro array distinto. Mostrar los 10 números en
res=tabla[0]; pantalla mediante un solo array.
mul=tabla[0];
div=tabla[0]; #include <stdio.h>
for (x=1;x<10;x++) #include <stdlib.h>
{ int main()
sum=sum+tabla[x]; {
int aux, numeros1[5],numeros2[5],numeros3[10]; 10. Que lea 5 números por teclado, los copie a otro
int i,j; array multiplicados por 2 y los muestre todos ordenados
usando un tercer array.
for (i=0;i<5;i++){
printf("Escriba un número"); #include <stdio.h>
scanf("%d",&numeros1[i]); #include <stdlib.h>
} int main()
for (i=0;i<5;i++){ {
printf("Escriba un número"); int aux, numeros1[5],numeros2[5],numeros3[10];
scanf("%d",&numeros2[i]); int i,j;
} for (i=0;i<5;i++){
for(i=0;i<5;i++) printf("Escriba un número");
{ scanf("%d",&numeros1[i]);
numeros3[i]=numeros1[i]; }
} for(i=0;i<5;i++)
for(i=0;i<5;i++) {
{ numeros2[i]=numeros1[i]*2;
numeros3[5+i]=numeros2[i]; }
} for(i=0;i<5;i++)
for (i=0;i<10;i++){ {
printf("%d\n",numeros3[i]); numeros3[i]=numeros1[i];
} }
system("PAUSE"); for(i=0;i<5;i++)
return 0; {
} numeros3[5+i]=numeros2[i];
}
9. Que lea 5 números por teclado, los copie a otro array for (i=0;i<10;i++){
multiplicados por 2 y muestre el segundo array. printf("%d\n",numeros3[i]);
}
#include <stdio.h> system("PAUSE");
#include <stdlib.h> return 0;
int main() }
{
int aux, numeros1[5],numeros2[5]; 11. Que rellene un array con los 100 primeros números
int i,j; pares y muestre su suma.
for (i=0;i<5;i++){
printf("Escriba un número"); #include <stdio.h>
scanf("%d",&numeros1[i]); #include <stdlib.h>
}
int main(void)
for(i=0;i<5;i++) {
{ int x,cont,sum,i,tabla[100];
numeros2[i]=numeros1[i]*2; i=0;
} sum=0;
for (x=1;x<=100;x++)
for (i=0;i<5;i++){ {
printf("%d\n",numeros2[i]); cont=0;
} if (x%2==0)
system("PAUSE"); {
return 0; tabla[i]=x;
} i++;
}
}
for (x=0;x<i;x++) aux=numeros[i];
{ numeros[i]=numeros[j];
sum=sum+tabla[x]; numeros[j]=aux;
} }
}
printf("%d\n",sum); }
system("PAUSE"); for (i=n-1;i>=0;i--){
return 0; printf("%f\n",numeros[i]);
} }
system("PAUSE");
12. Que lea 10 números por teclado, los almacene en un return 0;
array y muestre la media. }

#include <stdio.h> 14. Que rellene un array con 20 números y luego busque
#include <stdlib.h> un número concreto.
int main()
{ #include <stdio.h>
float sum, numeros1[10]; #include <stdlib.h>
int i; int main(int argc, char *argv[])
sum=0; {
for (i=0;i<10;i++){ int i,x=0,vector[20], n=20, dato, centro,inf=0,sup=n-1;
printf("Escriba un número");
scanf("%f",&numeros1[i]); for (i=0;i<20;i++){
} printf("Escriba un número");
for(i=0;i<10;i++) scanf("%d",&vector[i]);
{ }
sum=sum+numeros1[i]; printf("Escriba el número a buscar");
} scanf("%d",&dato);
printf("%f\n",sum/10); while(inf<=sup)
system("PAUSE"); {
return 0; centro=(sup+inf)/2;
} if (vector[centro]==dato)
{
13. Que mediante un array almacene números tanto printf("Existe\n");
positivos como negativos y los muestre ordenados. x=1;
break;
#include <stdio.h> }
#include <stdlib.h> else if(dato < vector [centro] )
{
int main() sup=centro-1;
{ }
float aux, numeros[10]; else
int i,j,n=10; {
for (i=0;i<n;i++){ inf=centro+1;
printf("Escriba un número"); }
scanf("%f",&numeros[i]); }
}
for(i=0;i<n-1;i++) if (x==0)
{ {
for(j=i+1;j<n;j++) printf("No existe\n");
{ }
if(numeros[i]<numeros[j]) system("PAUSE");
{ return 0;
printf("M");
} }
//rey
15. Que pinte un tablero de ajedrez, los peones con la else if ((x==0 && y==4) ||
letra P, las torres con T, los caballos con C, los alfiles con (x==7 && y==4)
A, el rey con R y la reina con M. )
{
#include <stdio.h> printf("R");
#include <stdlib.h> }
int main(int argc, char *argv[]) else
{ {
int x,y; printf(" ");
for (x=0;x<8;x++) }
{ }
for (y=0;y<8;y++) printf("\n");
{ }
//peones
if (x==1 || x==6) system("PAUSE");
{ return 0;
printf("P"); }
}
//torres 16. Que muestre los primeros 100 números de izquierda
else if ((x==0 && y==0) || a derecha usando un array de dos dimensiones.
(x==7 && y==0) ||
(x==0 && y==7) || #include <stdio.h>
(x==7 && y==7) #include <stdlib.h>
) int main(int argc, char *argv[])
{ {
printf("T"); int x,y, numeros[10][10];
}
//caballos for (x=0;x<10;x++)
else if ((x==0 && y==1) || {
(x==7 && y==1) || for (y=0;y<10;y++)
(x==0 && y==6) || {
(x==7 && y==6) numeros[x][y]=(x*10)+1+y;
) }
{
printf("C"); }
} for (x=0;x<10;x++)
//alfiles {
else if ((x==0 && y==2) || for (y=0;y<10;y++)
(x==7 && y==2) || {
(x==0 && y==5) || printf("%d ",numeros[x][y]);
(x==7 && y==5) }
) printf("\n");
{ }
printf("A"); system("PAUSE");
} return 0;
//reina }
else if ((x==0 && y==3) ||
(x==7 && y==3) 17. Que muestre los primeros 100 números de izquierda
) a derecha usando un array de dos dimensiones, la
{
última fila a mostrará la suma de sus respectivas printf("El número es: %d\n",numeros[x][y]);
columnas. system("PAUSE");
return 0;
#include <stdio.h> }
#include <stdlib.h>
int main(int argc, char *argv[]) 19. Que rellene una matriz de 3x3 y muestre su
{ traspuesta (la traspuesta se consigue intercambiando
int x,y,sum, numeros[11][10]; filas por columnas y viceversa).
for (y=0;y<10;y++)
{ #include <stdio.h>
sum=0; #include <stdlib.h>
for (x=0;x<10;x++) int main(int argc, char *argv[])
{ {
numeros[x][y]=(x*10)+1+y; int x,y,num=0, numeros[4][4];
sum=sum+numeros[x][y];
} for (x=0;x<3;x++)
numeros[10][y]=sum; {
} for (y=0;y<3;y++)
for (x=0;x<11;x++) {
{ numeros[x][y]=num;
for (y=0;y<10;y++) num++;
{ }
printf("%d ",numeros[x][y]); }
} printf("El array original es: \n\n\n");
printf("\n");
} for(x = 0;x < 3;x++)
system("PAUSE"); {
return 0; for(y = 0;y < 3;y++)
} {
printf(" %d ", numeros[x][y]);
18. Que rellene un array de dos dimensiones con }
números pares, lo pinte y después que pida una printf("\n\n\n");
posición X,Y y mostrar el número correspondiente. }
printf("La traspuesta es: \n\n\n");
#include <stdio.h>
#include <stdlib.h> for(x = 0;x < 3;x++)
{
int main(int argc, char *argv[]) for(y = 0;y < 3;y++)
{ {
int x,y,num=2, numeros[3][3]; printf(" %d ", numeros[y][x]);
}
for (x=0;x<3;x++) printf("\n\n\n");
{ }
for (y=0;y<3;y++) system("PAUSE");
{ return 0;
numeros[x][y]=num; }
num=num*2;
} 20. Que lea una cadena y la muestre al revés.
}
printf("Introduzca coordenada x: "); #include <stdio.h>
scanf("%d",&x); #include <stdlib.h>
printf("Introduzca coordenada y: "); int main(int argc, char *argv[])
scanf("%d",&y); {
int indice,x; default:
char frase[50]; break;
printf("Introduzca una frase: "); }
gets(frase);
}
for(x = 0;x < 50;x++) printf("\n\nEn la frase hay %d vocales\n\n",sum);
{ printf("\n\n");
if (frase[x]=='\0') system("PAUSE");
{ return 0;
indice=x; }
break;
} 22. Que lea una cadena y diga cuantas mayúsculas hay.
}
printf("La frase al reves es: \n\n"); #include <stdio.h>
for(x = indice-1;x >=0;x--) #include <stdlib.h>
{ int main(int argc, char *argv[])
printf("%c",frase[x]); {
} int sum=0,x;
printf("\n\n"); char frase[50];
system("PAUSE");
return 0; printf("Introduzca una frase: ");
} gets(frase);

21. Que lea una cadena y diga cuantas vocales hay. for(x = 0;x < 50;x++)
{
#include <stdio.h> if (frase[x]>=65 && frase[x]<=90)
#include <stdlib.h> {
int main(int argc, char *argv[]) sum++;
{ }
int sum=0,x; }
char frase[50]; printf("\n\nEn la frase hay %d mayúsculas\n\n",sum);
printf("Introduzca una frase: "); printf("\n\n");
gets(frase); system("PAUSE");
return 0;
for(x = 0;x < 50;x++) }
{
switch (frase[x]) 23. Que lea una cadena y la encripte sumando 3 al
{ código ASCII de cada carácter. Mostrar por pantalla.
case 'a':
sum++; #include <stdio.h>
break; #include <stdlib.h>
case 'e': int main(int argc, char *argv[])
sum++; {
break; int sum=0,x;
case 'i': char frase[50];
sum++;
break; printf("Introduzca una frase: ");
case 'o': gets(frase);
sum++; for(x = 0; x < 50;x++)
break; {
case 'u': if (frase[x]!='\0')
sum++; {
break; frase[x]=frase[x]+3;
} printf("Introduzca un stock: ");
} scanf("%d",&prod.cantidad);
printf("\n\nLa nueva frase es:\n\n",sum); for(x = 9; x >=0; x--)
printf("\n\n%s\n\n",frase); {
printf("\n\n"); if (x!=0)
system("PAUSE"); {
return 0; strcpy(productos[x].nombre,productos[x-
} 1].nombre);
productos[x].precio=productos[x-1].precio;
24. Que gestione los datos de stock de una tienda de productos[x].cantidad=productos[x-
comestibles, la información a recoger será: nombre del 1].cantidad;
producto, precio, cantidad en stock. La tienda dispone }
de 10 productos distintos. El programa debe ser capaz else
de: {
strcpy(productos[x].nombre,prod.nombre);
Dar de alta un producto nuevo. productos[x].precio=prod.precio;
Buscar un producto por su nombre. productos[x].cantidad=prod.cantidad;
Modificar el stock y precio de un producto dado. }
}
#include <stdio.h> printf("\nProducto creado. \n\n");
#include <stdlib.h> }
struct producto { else if (opcion==2)
char nombre[50]; {
float precio; printf("Introduzca un nombre: ");
int cantidad; gets(prod.nombre);
}; gets(prod.nombre);
int main(int argc, char *argv[])
{ for(x = 0; x < 10;x++)
struct producto prod,productos[10]; {
int x,opcion=1;
for (x=0;x<10;x++) if
{ (strcmp(productos[x].nombre,prod.nombre)==0)
strcpy(productos[x].nombre,"X"); {
productos[x].precio=0; printf("\nNombre:
productos[x].cantidad=0; %s\n",productos[x].nombre);
} printf("Precio: %f\n",productos[x].precio);
printf("Cantidad en Stock:
while ((opcion==1 || opcion==2 || opcion==3) && %d\n",productos[x].cantidad);
(opcion!=4)) }
{ }
printf("1- Alta de producto\n"); printf("\n\n");
printf("2- Buscar por nombre\n"); }
printf("3- Modificar stock y precio\n"); else if (opcion==3)
printf("4- Salir\n"); {
printf("Introduzca una opción: "); printf("Introduzca un nombre: ");
scanf("%d",&opcion); gets(prod.nombre);
if (opcion==1) gets(prod.nombre);
{
printf("Introduzca un nombre: "); for(x = 0; x < 10;x++)
gets(prod.nombre); {
gets(prod.nombre); if
printf("Introduzca un precio: "); (strcmp(productos[x].nombre,prod.nombre)==0)
scanf("%f",&prod.precio); {
printf("Introduzca un precio: "); printf("4- Media de todas las notas inferiores a
scanf("%f",&productos[x].precio); 5\n");
printf("Introduzca un stock: "); printf("5- Alumno con mejores notas\n");
scanf("%d",&productos[x].cantidad); printf("6- Alumno con peores notas\n");
printf("\nProducto modificado."); printf("7- Salir\n");
} printf("Introduzca una opción: ");
} scanf("%d",&opcion);
printf("\n\n"); if (opcion==1)
} {
} printf("Introduzca un nombre: ");
system("PAUSE"); gets(alum.nombre);
return 0; gets(alum.nombre);
} for(x = 0; x < 5;x++)
{
25. Que gestiona las notas de una clase de 20 alumnos if
de los cuales sabemos el nombre y la nota. El programa (strcmp(alumnos[x].nombre,alum.nombre)==0)
debe ser capaz de: {
printf("\nNombre: %s\n",alumnos[x].nombre);
Buscar un alumno. printf("Nota: %f\n",alumnos[x].nota);
Modificar su nota. }
Realizar la media de todas las notas. }
Realizar la media de las notas menores de 5. printf("\n\n");
Mostrar el alumno que mejores notas ha sacado. }
Mostrar el alumno que peores notas ha sacado. else if (opcion==2)
{
#include <stdio.h> printf("Introduzca un nombre: ");
#include <stdlib.h> gets(alum.nombre);
struct alumno { gets(alum.nombre);
char nombre[50];
float nota; for(x = 0; x < 5;x++)
}; {
int main(int argc, char *argv[]) if
{ (strcmp(alumnos[x].nombre,alum.nombre)==0)
struct alumno alum,alumnos[5]; {
int x,opcion=1; printf("Introduzca una nota: ");
float sum=0,cont=0,mejor,peor; scanf("%f",&alumnos[x].nota);
for (x=0;x<5;x++) printf("\nNota modificada.");
{ }
printf("Introduzca nombre alumno:"); }
gets(alumnos[x].nombre); printf("\n\n");
gets(alumnos[x].nombre); }
printf("Introduzca nota:"); else if (opcion==3)
scanf("%f",&alumnos[x].nota); {
} sum=0;
for(x = 0; x < 5;x++)
while ((opcion==1 || opcion==2 || {
opcion==3 || opcion==4 || sum=sum+alumnos[x].nota;
opcion==5 || opcion==6) && (opcion!=7)) }
{ printf("\nLa media de las notas es de: %f
printf("1- Buscar un alumno\n"); \n",(sum/5));
printf("2- Modificar nota\n"); }
printf("3- Media de todas las notas\n"); else if (opcion==4)
{
sum=0;
cont=0;
for(x = 0; x < 5;x++)
{
if (alumnos[x].nota<5)
{
sum=sum+alumnos[x].nota;
cont++;
}
}
printf("\nLa media de las notas inferiores a 5 es:
%f \n",sum/cont);
}
else if (opcion==5)
{
mejor=0;
for(x = 0; x < 5;x++)
{
if (alumnos[x].nota>mejor)
{
mejor=alumnos[x].nota;
alum.nota=alumnos[x].nota;
strcpy(alum.nombre,alumnos[x].nombre);
}
}
printf("\nEl alumno con mejores notas es: %s
\n",alum.nombre);
}
else if (opcion==6)
{
peor=10;
for(x = 0; x < 5;x++)
{
if (alumnos[x].nota<peor)
{
peor=alumnos[x].nota;
alum.nota=alumnos[x].nota;
strcpy(alum.nombre,alumnos[x].nombre);
}
}
printf("\nEl alumno con peores notas es: %s
\n",alum.nombre);
}
}
system("PAUSE");
return 0;
}

También podría gustarte