1 Hora
1 Día
1 Semana
1 Mes
Siempre
Ingresar con nombre de usuario, contraseña y duración de la sesión
| Portal Hacker | Editorial | Descargas | Ezine |
25 de Julio de 2008, 11:45:20
Noticias:
Para ver este enlace
Registrate
o
Inicia Sesion
Privacidad - Necesitas que se depure cierta informacion en nuestro portal?
Foros pOrtal Hacker
Programacion
Programación en general
JAVA
(Moderador:
kamui23
)
Ejercicios resueltos [programacion estructurada]
0 Usuarios y 1 Visitante están viendo este tema.
« anterior
próximo »
Páginas:
1
2
3
Autor
Tema: Ejercicios resueltos [programacion estructurada] (Leído 3021 veces)
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Ejercicios resueltos [programacion estructurada]
«
:
14 de Marzo de 2008, 01:58:48 »
>> ARITMETICA <<
- Hallar A+B-C+100
Código:
class JavaAritmetica1
{
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = Leer.datoInt ();
System.out.print ("Inserte B: ");
B = Leer.datoInt ();
System.out.print ("Inserte C: ");
C = Leer.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
Hallar (a-b)(a+b)
Código:
class JavaAritmetica2
{
public static void main (String elMago [])
{
int a, b;
System.out.print ("Inserte valor a: ");
a = Leer.datoInt ();
System.out.print ("Inserte valor b: ");
b = Leer.datoInt ();
System.out.println ("(" + a + "-" + b + ") " + "(" + a + "+" + b + ") " + "= " + ((a - b) * (a + b)));
}
}
Leer un numeo de tres digitos y sumarlos
Código:
class JavaAritmetica3
{
public static void main (String elMago [])
{
int numero, sumDig = 0;
System.out.print ("Inserte numero de tres digitos: ");
numero = Leer.datoInt ();
if (numero <= 100)
System.out.println ("ERROR: El numero no tiene 3 digitos");
else
{
int aux = numero; //en aux salvamos numero
while (numero != 0)
{
sumDig = sumDig + (numero % 10); //sumamos a sumDig el ultimo digito de numero
numero = numero / 10; //eliminamos el ultimo digito de numero
}
System.out.println ("La suma de los digitos de " + aux + " es: " + sumDig);
}
}
}
Dado un numero verificar:
- Que tenga dos digitos
- Verificar si sus digitos son pares
- Promediar sus digitos
Código:
class JavaAritmetica4
{
public static void main (String args [])
{
int numero;
System.out.print ("Inserte un numero de dos digitos pares: ");
numero = Leer.datoInt ();
int aux = numero;
if (numero < 100 && numero > 9)
{
int d1 = numero % 10;
numero = numero / 10;
int d2 = numero % 10;
if (d1 % 2 == 0 && d2 % 2 == 0)
System.out.println ("El promedio de los digitos de: " + aux + " es: " + ((d1 + d2) / 2));
}
}
}
Dado un numero entero, determinar si es positivo, negativo o nulo
Código:
class JavaAritmetica5
{
public static void main (String args [])
{
int numero;
System.out.print ("Inserte un numero: ");
numero = Leer.datoInt ();
if (numero == 0)
System.out.println ("El numero " + numero + " es NULO");
else
{
if (numero < 0)
System.out.println ("El numero " + numero + " es NEGATIVO");
else
System.out.println ("El numero " + numero + " es POSITIVO");
}
}
}
Dados seis numero determinar el menor de ellos
Código:
class JavaAritmetica6
{
public static void main (String args [])
{
int a, b, c, d, e, f;
System.out.print ("Inserte num.1: ");
a = Leer.datoInt ();
System.out.print ("Inserte num.2: ");
b = Leer.datoInt ();
System.out.print ("Inserte num.3: ");
c = Leer.datoInt ();
System.out.print ("Inserte num.4: ");
d = Leer.datoInt ();
System.out.print ("Inserte num.5: ");
e = Leer.datoInt ();
System.out.print ("Inserte num.6: ");
f = Leer.datoInt ();
int menor = a;
if (b < menor)
menor = b;
if (c < menor)
menor = c;
if (d < menor)
menor = d;
if (e < menor)
menor = e;
if (f < menor)
menor = f;
System.out.println ("\nEl menor de:" + a + "," + b + "," + c + "," + d + "," + e + "," + f + ",");
System.out.println ("Es: " + menor);
}
}
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #1 :
14 de Marzo de 2008, 01:59:17 »
>> SERIES <<
Generar 5,10,15,20,25,30,...
Código:
class JavaSeries1
{
public static void main (String args [])
{
int n, c = 1, serie = 5;
System.out.print ("Cantidad d terminos: ");
n = Leer.datoInt ();
while (c <= n)
{
System.out.print ("," + serie);
serie += 5;
c++;
}
}
}
Si n=7 generar 7,6,5,4,3,2,1
Código:
class JavaSeries2
{
public static void main (String args [])
{
int n, c = 1;
System.out.print ("Cantidad d terminos: ");
n = Leer.datoInt ();
int serie = n;
while (c <= n)
{
System.out.print (serie + ",");
serie--;
c++;
}
}
}
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #2 :
14 de Marzo de 2008, 01:59:43 »
>> VECTORES <<
/*Dado el vector T de tamao n. Si el tamao es par invertir los elementos de la mitad de los elementos
Ejemplo: v=[1][2][3][4][5][6] v(invertido)=[3][2][1][6][5][4]
*/
Código:
class JavaVectores1
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void invierte (int V [], int d)
{
int aux1;
int fin1 = d / 2;
for (int i = 1 ; i <= (d / 2) / 2 ; i++)
{
aux1 = V [i];
V [i] = V [fin1];
V [fin1] = aux1;
fin1--;
}
fin1 = d;
for (int j = (d / 2) + 1 ; j <= (d / 2) + 1 ; j++)
{
aux1 = V [j];
V [j] = V [fin1];
V [fin1] = aux1;
fin1--;
}
}
public static void main (String args [])
{
JavaVectores1 h = new JavaVectores1 ();
int V [] = new int [20];
System.out.print ("Inserte dimen. del vector: ");
int d = Leer.datoInt ();
h.llenar (V, d);
System.out.println ("\nVECTOR ORIGINAL: ");
h.mostrar (V, d);
System.out.println ("\nVECTOR LUEGO DE LA INVERSION: ");
h.invierte (V, d);
h.mostrar (V, d);
}
}
/*Dado un polinomio evualuarlo en el punto x (todo en un vector)*/
Código:
class JavaVectores2
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
int potencia (int b, int e)
{
int p = 1;
for (int i = 1 ; i <= e ; i++)
{
p = p * b;
}
return (p);
}
void evalua (int V [], int d, int x)
{
int s = 0;
for (int i = 1 ; i <= d ; i += 2)
{
s = s + (V [i] * potencia (x, V [i + 1]));
}
System.out.println ("\n\nX es igual a: " + s);
}
public static void main (String args [])
{
JavaVectores2 h = new JavaVectores2 ();
int V [] = new int [20];
System.out.print ("Inserte dimen. del vector: ");
int d = Leer.datoInt ();
System.out.print ("Inserte valor de (x): ");
int x = Leer.datoInt ();
h.llenar (V, d);
System.out.println ("\nVECTOR: ");
h.mostrar (V, d);
h.evalua (V, d, x);
}
}
Dado un vector ordenarlo de forma ascendente y desendente
Código:
class ej1
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte posc.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void orden_desc (int V [], int d)
{
int aux;
for (int i = 1 ; i <= d - 1 ; i++)
{
for (int j = i + 1 ; j <= d ; j++)
{
if (V [i] <= V [j])
{
aux = V [i];
V [i] = V [j];
V [j] = aux;
}
}
}
}
void orden_asc (int V [], int d)
{
for (int i = 1 ; i <= d - 1 ; i++)
{
for (int j = i + 1 ; j <= d ; j++)
{
if (V [i] > V [j])
{
int aux = V [i];
V [i] = V [j];
V [j] = aux;
}
}
}
}
public static void main (String args [])
{
int V [] = new int [20];
int d;
ej1 h = new ej1 ();
System.out.print ("Inserte dimension de vector: ");
d = Leer.datoInt ();
h.llenar (V, d);
h.mostrar (V, d);
System.out.println ("\n\nORDENADA DESCENDENTEMENTE");
h.orden_desc (V, d);
h.mostrar (V, d);
System.out.println ("\n\nORDENADA ASCEDENTEMENTE");
h.orden_asc (V, d);
h.mostrar (V, d);
}
}
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #3 :
14 de Marzo de 2008, 02:00:16 »
>> MATRICES <<
/*Dadas dos matrices A y B intercambiar los minimos de A con los maximos de B*/
Código:
class JavaMatrices1
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
int menor (int M [] [], int f, int c)
{
int men = M [1] [1];
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
if (M [i] [j] < men)
men = M [i] [j];
}
}
return (men);
}
int maximo (int M [] [], int f, int c)
{
int max = M [1] [1];
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
if (M [i] [j] < max)
max = M [i] [j];
}
}
return (max);
}
void intercambiar (int A [] [], int fa, int ca, int B [] [], int fb, int cb)
{
int min_a = menor (A, fa, ca);
int max_b = maximo (B, fb, cb);
//para cambiar los minimos de A con los maximos de B
for (int i = 1 ; i <= fa ; i++)
{
for (int j = 1 ; j <= ca ; j++)
{
if (A [i] [j] == min_a)
A [i] [j] = max_b;
}
}
//para intercambiar los maximos de con los minimos de A
for (int i = 1 ; i <= fb ; i++)
{
for (int j = 1 ; j <= cb ; j++)
{
if (B [i] [j] == max_b)
B [i] [j] = min_a;
}
}
}
public static void main (String args [])
{
JavaMatrices1 h = new JavaMatrices1 ();
int A [] [] = new int [20] [20];
int B [] [] = new int [20] [20];
System.out.print ("Insert filas de A: ");
int fa = Leer.datoInt ();
System.out.print ("Insert columnas de A: ");
int ca = Leer.datoInt ();
System.out.print ("Insert filas de B: ");
int fb = Leer.datoInt ();
System.out.print ("Insert columnas de B: ");
int cb = Leer.datoInt ();
//lectura de matrices
System.out.println ("\nINSERTANDO DATOS EN MATRIS A: \n");
h.llenar (A, fa, ca);
System.out.println ("\nINSERTANDO DATOS EN MATRIS B: \n");
h.llenar (B, fb, cb);
System.out.println ("\nMATRICES ORIGINALMENTE INSERTADAS: ");
h.mostrar (A, fa, ca);
System.out.println ();
h.mostrar (B, fb, cb);
System.out.println ();
//intercambiando elementos
h.intercambiar (A, fa, ca, B, fb, cb);
System.out.println ("\nMATRICES DESPUES DEL INTERCAMBIO:");
h.mostrar (A, fa, ca);
System.out.println ();
h.mostrar (B, fb, cb);
}
}
/*Dada una matris cuadrada invertir su diagonal principal*/
Código:
class JavaMatrices2
{
void llenar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.println ();
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void invierte (int M [] [], int d)
{
int fin = d;
for (int i = 1 ; i <= d / 2 ; i++)
{
int aux = M [i] [i];
M [i] [i] = M [d] [d];
M [d] [d] = aux;
fin--;
}
}
public static void main (String args [])
{
JavaMatrices2 h = new JavaMatrices2 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte dimen. de la matris cuadrada: ");
int d = Leer.datoInt ();
h.llenar (M, d);
System.out.print ("\nMATRIS ORIGINAL: ");
h.mostrar (M, d);
System.out.print ("\n\nMATRIS CON LA DIAGONAL PRINCIPAL INVERTIDA: ");
h.invierte (M, d);
h.mostrar (M, d);
}
}
/*Dada una matris cuadrada invertir su diagonal secundaria*/
Código:
class JavaMatrices3
{
void llenar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.println ();
for (int j = 1 ; j <= d ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void invierte (int M [] [], int d)
{
int fin = d;
for (int i = 1 ; i <= d / 2 ; i++)
{
int aux = M [i] [d];
M [i] [d] = M [d] [i];
M [d] [i] = aux;
fin--;
}
}
public static void main (String args [])
{
JavaMatrices3 h = new JavaMatrices3 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte dimen. de la matris cuadrada: ");
int d = Leer.datoInt ();
h.llenar (M, d);
System.out.print ("\nMATRIS ORIGINAL: ");
h.mostrar (M, d);
System.out.print ("\n\nMATRIS CON LA DIAGONAL SECUNDARIA INVERTIDA: ");
h.invierte (M, d);
h.mostrar (M, d);
}
}
/*Dada dos matrices de diferentes tamanios R y S mostrar los elementos comunes de R en S*/
Código:
class JavaMatrices4
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void comunes (int R [] [], int fr, int cr, int S [] [], int fs, int cs)
{
System.out.print ("\n\nLos elementos comunes de R en S son: ");
for (int i = 1 ; i <= fr ; i++)
{
for (int j = 1 ; j <= cr ; j++)
{
for (int k = 1 ; k <= fs ; k++)
{
for (int l = 1 ; l <= cs ; l++)
{
if (R [i] [j] == S [k] [l])
System.out.print ("[" + R [i] [j] + "]");
}
}
}
}
}
public static void main (String args [])
{
JavaMatrices4 h = new JavaMatrices4 ();
int R [] [] = new int [20] [20];
int S [] [] = new int [20] [20];
System.out.print ("Inserte filas de R: ");
int fr = Leer.datoInt ();
System.out.print ("Inserte columnas de R: ");
int cr = Leer.datoInt ();
System.out.print ("Inserte filas de S: ");
int fs = Leer.datoInt ();
System.out.print ("Inserte columnas de S: ");
int cs = Leer.datoInt ();
System.out.print ("\nLLENANDO MATRIS R: \n");
h.llenar (R, fr, cr);
System.out.print ("\nLLENANDO MATRIS S: \n");
h.llenar (S, fs, cs);
System.out.print ("\nLA MATRICES R : ");
h.mostrar (R, fr, cr);
System.out.print ("\nLA MATRICES S : ");
h.mostrar (S, fs, cs);
h.comunes (R, fr, cr, S, fs, cs);
}
}
/*Dada una matris intercambiar los elementos de la primera columna con la ultima columna*/
Código:
class JavaMatrices5
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void intercambiar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
int aux = M [i] [1];
M [i] [1] = M [i] [c];
M [i] [c] = aux;
}
}
public static void main (String args [])
{
JavaMatrices5 h = new JavaMatrices5 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte columnas de la matris: ");
int c = Leer.datoInt ();
System.out.print ("\nLLENANDO MATRIS : \n");
h.llenar (M, f, c);
System.out.print ("\nLA MATRIS ORIGINAL : ");
h.mostrar (M, f, c);
System.out.print ("\n\nLA MATRICES INTERCAMBIADA : ");
h.intercambiar (M, f, c);
h.mostrar (M, f, c);
}
}
/* Contar el numero de digitos de cada elemento de una matris */
Código:
class JavaMatrices6
{
void llenar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrar (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void cuenta (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("\n[" + M [i] [j] + "] tiene: " + digitos (M [i] [j]) + " digito(s)");
}
}
}
int digitos (int n)
{
int contador = 0;
while (n != 0)
{
n = n / 10;
contador++;
}
return (contador);
}
public static void main (String args [])
{
JavaMatrices6 h = new JavaMatrices6 ();
int M [] [] = new int [20] [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte columnas de la matris: ");
int c = Leer.datoInt ();
System.out.print ("\nLLENANDO MATRIS M: \n");
h.llenar (M, f, c);
System.out.print ("\nLA MATRIS: ");
h.mostrar (M, f, c);
System.out.print ("\n\nCONTEO DE DIGITOS: ");
h.cuenta (M, f, c);
}
}
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #4 :
14 de Marzo de 2008, 02:00:46 »
>> MATRICES Y VECTORES<<
/*Dada la matrix de m*n y el vector de tamanio n, determinar que columna de la matris
es igual al vector*/
Código:
class JavaMatrisVector2
{
void llenarMatris (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrarMatris (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void llenarVector (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos.[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrarVector (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void procedure (int M [] [], int f, int c, int V [], int d)
{
for (int i = 1 ; i <= f ; i++)
{
int sw = 1;
for (int j = 1 ; j <= c ; j++)
{
for (int k = 1 ; k <= d ; k++)
{
if (M [j] [i] != V [k])
sw = 0;
}
}
if (sw == 1)
System.out.println ("\n\nLa columna " + i + " es igual al vector");
}
}
public static void main (String args [])
{
JavaMatrisVector2 h = new JavaMatrisVector2 ();
int M [] [] = new int [20] [20];
int V [] = new int [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte dimension del vector: ");
int d = Leer.datoInt ();
System.out.print ("\nLLENANDO MATRIS: \n");
h.llenarMatris (M, f, d);
System.out.print ("\nLLENANDO EL VECTOR: \n");
h.llenarVector (V, d);
System.out.print ("\nLA MATRIS: ");
h.mostrarMatris (M, f, d);
System.out.print ("\n\nEL VECTOR: \n");
h.mostrarVector (V, d);
h.procedure (M, f, d, V, d);
}
}
/*Dada una matris Z almacenar en un vector A la suma por sus columnas
y en un vector B la suma por sus filas*/
Código:
class JavaMatrisVector3
{
void llenarMatris (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("Inserte pos[" + i + "][" + j + "]: ");
M [i] [j] = Leer.datoInt ();
}
}
}
void mostrarMatris (int M [] [], int f, int c)
{
for (int i = 1 ; i <= f ; i++)
{
System.out.println ();
for (int j = 1 ; j <= c ; j++)
{
System.out.print ("[" + M [i] [j] + "]");
}
}
}
void mostrarVector (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void vectorA (int M [] [], int f, int c, int A [], int d)
{
for (int i = 1 ; i <= f ; i++)
{
int suma = 0;
for (int j = 1 ; j <= c ; j++)
{
suma = suma + M [j] [i];
}
A [i] = suma;
}
}
void vectorB (int M [] [], int f, int c, int B [], int d)
{
for (int i = 1 ; i <= f ; i++)
{
int suma = 0;
for (int j = 1 ; j <= c ; j++)
{
suma = suma + M [i] [j];
}
B [i] = suma;
}
}
public static void main (String args [])
{
JavaMatrisVector3 h = new JavaMatrisVector3 ();
int Z [] [] = new int [20] [20];
int A [] = new int [20];
int B [] = new int [20];
System.out.print ("Inserte filas de la matris: ");
int f = Leer.datoInt ();
System.out.print ("Inserte columnas de la matris: ");
int c = Leer.datoInt ();
System.out.print ("\nLLENANDO MATRIS: \n");
h.llenarMatris (Z, f, c);
System.out.print ("\nLA MATRIZ Z: ");
h.mostrarMatris (Z, f, c);
System.out.println ("\n\nSUMA POR COLUMNAS DE LA MATRIS (vector A): ");
h.vectorA (Z, f, c, A, c);
h.mostrarVector (A, c);
System.out.println ("\n\nSUMA POR FILAS DE LA MATRIS (vector B): ");
h.vectorB (Z, f, c, B, f);
h.mostrarVector (B, f);
}
}
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
D21
NZ1
Desconectado
Mensajes: 43
R007: Club Hacker [ EL-MAGO ]
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #5 :
14 de Marzo de 2008, 02:01:36 »
/*Sumar dos vectores recursivamente*/
Código:
class restaVectores
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void suma (int A [], int B [], int C [], int i)
{
if (i != 0)
{
C [i] = A [i] + B [i];
suma (A, B, C, i - 1);
}
}
public static void main (String args [])
{
restaVectores h = new restaVectores ();
System.out.print ("Inserte dim. del vector: ");
int d = Leer.datoInt ();
int A [] = new int [20];
int B [] = new int [20];
int C [] = new int [20];
System.out.println ("VECTOR A");
h.llenar (A, d);
System.out.println ("\nVECTOR B");
h.llenar (B, d);
h.suma (A, B, C, d);
System.out.println ("\n\nVECTOR A");
h.mostrar (A, d);
System.out.println ("\n\nVECTOR B");
h.mostrar (B, d);
System.out.println ("\n\nVECTOR RESULTANTE C");
h.mostrar (C, d);
}
}
/*restar dos vectores recursivamente*/
Código:
class restaVectores
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void resta (int A [], int B [], int C [], int i)
{
if (i != 0)
{
C [i] = A [i] - B [i];
resta (A, B, C, i - 1);
}
}
public static void main (String args [])
{
restaVectores h = new restaVectores ();
System.out.print ("Inserte dim. del vector: ");
int d = Leer.datoInt ();
int A [] = new int [20];
int B [] = new int [20];
int C [] = new int [20];
System.out.println ("VECTOR A");
h.llenar (A, d);
System.out.println ("\nVECTOR B");
h.llenar (B, d);
h.resta (A, B, C, d);
System.out.println ("\n\nVECTOR A");
h.mostrar (A, d);
System.out.println ("\n\nVECTOR B");
h.mostrar (B, d);
System.out.println ("\n\nVECTOR RESULTANTE C");
h.mostrar (C, d);
}
}
/*multiplicar dos vectores recursivamente*/
Código:
class multiplicarVectores
{
void llenar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("Inserte pos[" + i + "]: ");
V [i] = Leer.datoInt ();
}
}
void mostrar (int V [], int d)
{
for (int i = 1 ; i <= d ; i++)
{
System.out.print ("[" + V [i] + "]");
}
}
void multipli (int A [], int B [], int C [], int i)
{
if (i != 0)
{
C [i] = A [i] * B [i];
multipli (A, B, C, i - 1);
}
}
public static void main (String args [])
{
multiplicarVectores h = new multiplicarVectores ();
System.out.print ("Inserte dim. del vector: ");
int d = Leer.datoInt ();
int A [] = new int [20];
int B [] = new int [20];
int C [] = new int [20];
System.out.println ("VECTOR A");
h.llenar (A, d);
System.out.println ("\nVECTOR B");
h.llenar (B, d);
h.multipli (A, B, C, d);
System.out.println ("\n\nVECTOR A");
h.mostrar (A, d);
System.out.println ("\n\nVECTOR B");
h.mostrar (B, d);
System.out.println ("\n\nVECTOR RESULTANTE C");
h.mostrar (C, d);
}
}
SOLO SE QUEDAN POSTS CON MAS EJERCICIOS RESUELTOS.
En línea
]|I{•------» SOLO LOS QUE DEJAN DE INTENTAR FRACASAN «------•}I|[
Para ver este enlace
Registrate
o
Inicia Sesion
www.ClubHacker
.org
daom11
NZ2
Desconectado
Mensajes: 136
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #6 :
15 de Marzo de 2008, 10:26:51 »
exelente aporte gracias!!
aunque estaría bien que tambien pusieras los mismos ejercicios de forma orientada a objetos para que todos pudieran ver la diferencia!!
saluditos....
En línea
CHR0N05
Colaborador
Conectado
Mensajes: 1,153
Chronos es Dios de Dioses!!...
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #7 :
25 de Marzo de 2008, 08:33:53 »
Wenas!!
Código:
class JavaAritmetica1
{
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = Leer.datoInt ();
System.out.print ("Inserte B: ");
B = Leer.datoInt ();
System.out.print ("Inserte C: ");
C = Leer.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
uff... a ver... a decir verdad, comenzando con Java... mmm, no me gusta ser tan noob... xD... bueno el asunto que estoy jugueteando con el JCreator 4.5 Pro y el NetBeans IDE 6.1,,, trate de compilar tal cual este código y no compila... las razones son:
Citar
cannot find symbol variable Leer
porque me sale este error
...
Renombré el archivo como la clase
JavaAritmetica
1
saludos
Modificado: xD... me tendrán en esta sección largo tiempo...
... y tambien he leido bastante...
«
Última modificación: 25 de Marzo de 2008, 08:36:57 por vVegeta
»
En línea
SOLO LOS QUE DEJAN DE INTENTAR, FRACASARÁN...
Para ver este enlace
Registrate
o
Inicia Sesion
daom11
NZ2
Desconectado
Mensajes: 136
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #8 :
26 de Marzo de 2008, 07:40:00 »
Hola vVegeta.
Lo que pasa es que el compilador no encuentra la clase
Leer
y por supuesto tampoco su método
datoInt()
, tienes de dos sopas
1.- Debes tener esa clase en el mismo proyecto para que te la reconosca, es decir debes tener otro archivo que se llame
Leer
y dentro un método
estático
que se llame
datoInt()
2.- O bien modificar tu código de la siguiente manera:
Código:
class JavaAritmetica1
import java.io.*;
{
public static int datoInt(){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
return Integer.parseInt(b.readLine());
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = JavaAritmetica1.datoInt ();
System.out.print ("Inserte B: ");
B = JavaAritmetica1.datoInt ();
System.out.print ("Inserte C: ");
C = JavaAritmetica1.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
de esta forma tendrás todo en el mismo archivo.
espero te sirva
saludos...
En línea
CHR0N05
Colaborador
Conectado
Mensajes: 1,153
Chronos es Dios de Dioses!!...
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #9 :
26 de Marzo de 2008, 09:41:04 »
Cita de: daom11 en 26 de Marzo de 2008, 07:40:00
Código:
class JavaAritmetica1
import java.io.*;
{
public static int datoInt(){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
return Integer.parseInt(b.readLine());
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = JavaAritmetica1.datoInt ();
System.out.print ("Inserte B: ");
B = JavaAritmetica1.datoInt ();
System.out.print ("Inserte C: ");
C = JavaAritmetica1.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
Oks... entendi, excepto unas duda que me acaban de salir...
Código:
class JavaAritmetica1
No deberia ser
class public JavaAritmetica
1
Código:
import java.io.*;
Y esto, importa una libreria llamada
java.io.*
... cierto
Código:
BufferedReader[/b]
Dice que lea cierto dato y que será un nuevo BufferReader.... es eso es lo que hace el BufferedReader ???... solamente la función de lectura del dato ???...
Y en el caso de que quisiese ingresar un dato manualmente... ???... deberia de crear una nueva clase ???
Saludos...
Ps. xD.. parece que hago demasiadas preguntas... xD
En línea
SOLO LOS QUE DEJAN DE INTENTAR, FRACASARÁN...
Para ver este enlace
Registrate
o
Inicia Sesion
WaesWaes
Moderador Global
Desconectado
Mensajes: 1,358
Señor de Outland, de la sentencia del Templo Negro
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #10 :
26 de Marzo de 2008, 02:52:41 »
vVegeta la clase no tiene siempre que ser publica eso va a depender si vos vas a llamar esa clase de otra clase o de una hija
igualmente el code esta mal.......tene
s que importar java.io.*; (ya te respondi otra pregunta) afuera de la clase.....porq
ue sino para empezar te va a decir que no abrite la clase "{}" y si abirs la clase ahi si te va a tirar errores XD
quedaria asi
Código:
import java.io.*;
class JavaAritmetica1 {
public static int datoInt(){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
return Integer.parseInt(b.readLine());
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = JavaAritmetica1.datoInt ();
System.out.print ("Inserte B: ");
B = JavaAritmetica1.datoInt ();
System.out.print ("Inserte C: ");
C = JavaAritmetica1.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100));
}
}
acordate que la entrada se va a guardar a un int por lo tanto debe ser un dato numerico de 4 bytes si mal no recuerdo asi que no podes poner ni letras ni caracteres que no sean numeros ni numeros que abarquen mas de 4 bytes
saludos
En línea
Para ver este enlace
Registrate
o
Inicia Sesion
Cita de: Mejor usuario de cph
1.WaesWaes, 2.Alfa, 3.TxShack
CHR0N05
Colaborador
Conectado
Mensajes: 1,153
Chronos es Dios de Dioses!!...
Re: Ejercicios resueltos [programacion estructurada]
«
Respuesta #11 :
26 de Marzo de 2008, 07:07:26 »
Cita de: WaesWaes en 26 de Marzo de 2008, 02:52:41
vVegeta la clase no tiene siempre que ser publica eso va a depender si vos vas a llamar esa clase de otra clase o de una hija
igualmente el code esta mal.......tene
s que importar java.io.*; (ya te respondi otra pregunta) afuera de la clase.....porq
ue sino para empezar te va a decir que no abrite la clase "{}" y si abirs la clase ahi si te va a tirar errores XD
quedaria asi
Código:
import java.io.*;
class JavaAritmetica1 {
public static int datoInt(){
try{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
return Integer.parseInt(b.readLine());
}catch(Exception e){
e.printStackTrace();
return -1;
}
}
public static void main (String mago [])
{
int A, B, C;
System.out.print ("Inserte A: ");
A = JavaAritmetica1.datoInt ();
System.out.print ("Inserte B: ");
B = JavaAritmetica1.datoInt ();
System.out.print ("Inserte C: ");
C = JavaAritmetica1.datoInt ();
System.out.println ("\n" + A + " + " + " " + B + " - " + C + " + " + 100 + " = " + (A + B - C + 100