Arreglos 1D, 2D, 3D.
-
Upload
susan-gil -
Category
Technology
-
view
54 -
download
4
Transcript of Arreglos 1D, 2D, 3D.
ÍNDICE.
ARREGLOS DE 1D…………..…………….3 BILLETES…………………………………………………….4INTERFAZ BILLETES……………………………………..8RECORRIDO2……………………………………………….10INTERFAZ RECORRIDO2………………………………...12VECTOR……………………………………………………....14INTERFAZ VECTOR………………………………………..15PROYECTIL………………………………………………….16INTERFAZ PROYECTIL…………………………………...18VENTAS1……………………………………………………...20INTERFAZ VENTAS1………………………………………22VENTAS………………………………………………………24INTERFAZ VENTAS……………………………………….26ARREGLOS DE 2D………………………………….28
BIDIMENCIONAL2………………………………………….29INTERFAZ BIDIMENCIONAL2…………………………..31INICARREGLO……………………………………………….32INTERFAZ INICARREGLO………………………………..33CALIF2D………………………………………………………34INTERFAZ CALIF2D……………………………………….37ARREGLO DE 3D……………………………………41
billetes:
import javax.swing.*;
public class Billetes{
public static void main(String arg[]){
int cant;
int dinero[]= new int [10];
String c = JOptionPane.showInputDialog("DIGITA LA CANTIDAD MONETARIA A DESGLOSAR:");
cant = Integer.parseInt(c);
int i=0;
while(cant > 0){
while(cant-1000>=0){
cant = cant-1000;
dinero[0]++;
}
while(cant-500>=0){
cant = cant-500;
dinero[1]++;
}
while(cant-200>=0){
cant = cant-200;
3
dinero[2]++;
}
while(cant-100>=0){
cant = cant-100;
dinero[3]++;
}
while(cant-50>=0){
cant = cant-50;
dinero[4]++;
}
while(cant-20>=0){
cant = cant-20;
dinero[5]++;
}
while(cant-10>=0){
cant = cant-10;
dinero[6]++;
}
while(cant-5>=0){
cant = cant-5;
dinero[7]++;
}
while(cant-2>=0){
cant = cant-2;
4
dinero[8]++;
}
while(cant-1>=0){
cant = cant-1;
dinero[9]++;
}
for(i=0;i<10;i++){
if(i==0)
System.out.println(dinero[i]+ " BILLETES DE 1000");
if(i==1)
System.out.println(dinero[i]+ " BILLETES DE 500");
if(i==2)
System.out.println(dinero[i]+ " BILLETES DE 200");
if(i==3)
System.out.println(dinero[i]+ " BILLETES DE 100");
if(i==4)
System.out.println(dinero[i]+ " BILLETES DE 50");
if(i==5)
System.out.println(dinero[i]+ " BILLETES DE 20");
if(i==6)
System.out.println(dinero[i]+ " MONEDA(S) DE 10");
5
if(i==7) System.out.println(dinero[i]+ " MONEDA(S) DE 5");
if(i==8) System.out.println(dinero[i]+ " MONEDA(S) DE 2");
if(i==9) System.out.println(dinero[i]+ " MONEDA(S) DE 1");
}
System.out.println("DESGLOCE DE LA CANTIDAD: " +c);
}
}
}
6
RECORRIDO2.Import java.util.*;
public class Recorrido2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numAlum, i;
double suma = 0, media;
do {
System.out.print("Número de alumnos de la clase: ");
numAlum = sc.nextInt();
} while (numAlum <= 0);
double[] notas = new double[numAlum]; //se crea el array
// Entrada de datos. Se asigna a cada elemento del array
// la nota introducida por teclado
for (i = 0; i < notas.length; i++) {
System.out.print("Alumno " + (i + 1) + " Nota final: ");
notas[i] = sc.nextDouble();
}
// Sumar todas las notas
for (i = 0; i < notas.length; i++) {
suma = suma + notas[i];
}
// Calcular la media
media = suma / notas.length;
8
// Mostrar la media
System.out.printf("Nota media del curso: %.2f %n", media);
// Mostrar los valores superiores a la media
System.out.println("Listado de notas superiores a la media: ");
for (i = 0; i < notas.length; i++) {
if (notas[i] > media) {
System.out.println("Alumno numero " + (i + 1)+ " Nota final: " + notas[i]);
}
}
}
}
9
VECTOR.public class vector {
public static void main(String[] args) {
int mayor,menor,suma;
int [] nums={3,4,8,2};//asignamos directamente los valores del vector
suma=0;
menor=nums[0];
mayor=nums[0];
for(int i=0;i<nums.length;i++){
if (nums[i]>mayor){
mayor=nums[i];
}
if (nums[i]<menor){
menor=nums[i];
11
}
suma+=nums[i];
}
System.out.println("El mayor es"+mayor);
System.out.println("El menor es"+menor);
System.out.println("La suma es"+suma);
}
}
VECTOR.
12
PROYECTIL.import javax.swing.*;
public class Proyectil{
public static void main (String args []){
String leer;
double magnitud, direccion, sum;
double vector[]=new double[3];
int ban=1;
while(ban==1)//miestras hayan datos a procesar
{ //leemos el valor de las constantes
leer=JOptionPane.showInputDialog("Ingrese el valor de a:");
vector[0]=Double.parseDouble(leer);
leer=JOptionPane.showInputDialog("Ingrese el valor de b:");
vector[1]=Double.parseDouble(leer);
leer=JOptionPane.showInputDialog("Ingrese el valor de c:");
vector[2]=Double.parseDouble(leer);
//calculamos la magnitud 18 //la cual es la raíz cuadrada de las suma+ 19 //de las componentes al cuadrado
13
sum=(vector[0]*vector[0])+(vector[1]*vector[1])+(vector[2 ]*vector[2]);
magnitud=Math.sqrt(sum);
//La dirección viene dada por la tangente inversa
//del cociente de las componentes x,y
direccion=Math.atan(vector[1]/vector[0]);
JOptionPane.showMessageDialog(null, "El valor de la magnitud es: "+magnitud+"\n y con un angulo direccional de: "+direccion+" Radianes");
leer=JOptionPane.showInputDialog("¿Hay más datos a procesar?\n\nSi=1 y No=0");
ban=Integer.parseInt(leer);
}//del while
}//del main
}//de la clase
14
Ventas1import javax.swing.*;
public class ventas1{
public static void main (String args[]){
double calificaciones[], calif, suma, promedio, varianza;
int tabla_frec[] = new int[1000000];
String s = JOptionPane.showInputDialog("dame el numero de productos vendidos");
int n = Integer.parseInt(s);
calificaciones = new double[n];
// Pedimos calificaciones de cada alumno y la almacenamos en el arreglo
for(int i = 0; i <n; i++){
s = JOptionPane.showInputDialog("Dame dame el costo del producto "+i);
calif = Double.parseDouble(s);
calificaciones[i] = calif;
tabla_frec[(int) calif]++;
}
// Desplegamos las calificiones
for(int i= 0; i < n; i++){
System.out.println("El producto en la posición "+i+" del arreglo es "+calificaciones[i]);
}
16
// Calculamos promedio
suma = 0.0;
for(int i = 0; i < n; i++){
suma = suma + calificaciones[i];
}
promedio = suma / n;
System.out.println("El total vendido es "+suma);
double sumatoria = 0;
for(int i= 0; i < n; i++){
sumatoria = sumatoria + Math.pow(calificaciones[i] - promedio, 2);
}
}
}
17
Ventas.import javax.swing.*;
public class ventas{
public static void main (String args[]){
double calificaciones[], calif, suma, promedio, varianza;
int tabla_frec[] = new int[1000000];
String s = JOptionPane.showInputDialog("dame el numero de productos vendidos");
int n = Integer.parseInt(s);
calificaciones = new double[n];
// Pedimos los costos y los almacenamos en el arreglo
for(int i = 0; i <n; i++){
s = JOptionPane.showInputDialog("Dame dame el costo del producto "+i);
calif = Double.parseDouble(s);
calificaciones[i] = calif;
tabla_frec[(int) calif]++;
}
// Desplegamos los productos
for(int i= 0; i < n; i++){
System.out.println("El producto en la posición "+i+" del arreglo es "+calificaciones[i]);
19
}
// Calculamos promedio
suma = 0.0;
for(int i = 0; i < n; i++){
suma = suma + calificaciones[i];
}
promedio = suma / n;
System.out.println("El promedio de ventas del dia es "+promedio);
double sumatoria = 0;
for(int i= 0; i < n; i++){
sumatoria = sumatoria + Math.pow(calificaciones[i] - promedio, 2);
}
}
}
20
BIDIMENSIONAL 2D.import java.util.*;
public class Bidimensional2 {
public static void main(String[] args) {
final int FILAS = 5, COLUMNAS = 4;
Scanner sc = new Scanner(System.in);
int i, j, mayor, menor;
int filaMayor, filaMenor, colMayor, colMenor;
int[][] A = new int[FILAS][COLUMNAS];
System.out.println("Lectura de elementos de la matriz: ");
for (i = 0; i < FILAS; i++) {
for (j = 0; j < COLUMNAS; j++) {
System.out.print("A[" + i + "][" + j + "]= ");
A[i][j] = sc.nextInt();
}
}
System.out.println("valores introducidos:");
for (i = 0; i < A.length; i++) {
for (j = 0; j < A[i].length; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println();
}
23
mayor = menor = A[0][0];//se toma el primero como mayor y menor
filaMayor = filaMenor = colMayor = colMenor = 0;
for (i = 0; i < A.length; i++) { //
for (j = 0; j < A[i].length; j++) {
if (A[i][j] > mayor) {
mayor = A[i][j];
filaMayor = i;
colMayor = j;
} else if (A[i][j] < menor) {
menor = A[i][j];
filaMenor = i;
colMenor = j;
}
}
}
System.out.print("Elemento mayor: " + mayor);
System.out.println(" Fila: "+ filaMayor + " Columna: " + colMayor);
System.out.print("Elemento menor: " + menor);
System.out.println(" Fila: "+ filaMenor + " Columna: " + colMenor);
}
}
24
BIDIMENSIONAL 2D.
Inicarreglo.public class InicArreglo{
//crea e imprime arreglos bidimensionales.
public static void main(String args[]){
int [][] arreglo1 = {{1, 2, 3}, {4, 5, 6}};
int [][] arreglo2 = {{1, 2}, {3}, {4, 5, 6}};
System.out.println("Los valores en el arrlo1 por filas son: ");
imprimirArreglo(arreglo1);//muestra arreglo1 por filas.
System.out.println("\nLos valores en el arreglo2 por filas son: ");
imprimirArreglo(arreglo2);//muestra arreglo2 por filas.
}//fin de main.
//imprimmir filas y columnas de un arreglo bidimensional.
public static void imprimirArreglo(int [][] arreglo)
{
//itera a travez de las filas del arreglo.
for(int fila = 0; fila < arreglo.length; fila++){
//itera a travez de las columnas de la fila actual.
for(int columna = 0; columna < arreglo[fila].length; columna++)
System.out.printf("%d ", arreglo[fila][columna]);
System.out.println();// inicia nueva linea de salida.
}//fin de for externo
}//fin del metodo imprimirArreglo
25
Clif2d.import javax.swing.*;
public class Calif2D{
public static void main (String args[]){
int calif [][], m, a;
double promMat[], promAlum[]; // arreglos para almacenar promedios de cada materia y cada alumno
double suma;
String s = JOptionPane.showInputDialog("Dame el número de materias");
m = Integer.parseInt(s);
s = JOptionPane.showInputDialog("Dame el número de alumnos");
a = Integer.parseInt(s);
calif = new int[m][a];
// Pedimos (llenamos) el arreglo 2D de calificaciones
for(int i =0; i < m; i++){
for(int j=0; j < a; j++){
s = JOptionPane.showInputDialog("Dame la calificacion "+i+","+j);
calif[i][j] = Integer.parseInt(s);
} // for j
} // for i
//Imprimimos el arreglo de calificaciones
for(int i =0; i < m; i++){
for(int j=0; j < a; j++){
27
System.out.print(calif[i][j]+"\t");
} // for j
System.out.println();
} // for i
// calculo del promedio por materia
promMat = new double[m];
for(int i =0; i < m; i++){
suma = 0.0;
for(int j=0; j < a; j++){
suma = suma + calif[i][j];
}
promMat[i] = suma / a;
} // for i
System.out.println("---------------------------------------------------------------------------------------");
System.out.println("Promedio de Materias");
for(int i =0; i < m; i++)
System.out.println("El promedio de la materia "+i+" es; "+promMat[i]);
// calculo del promedio por alumno
promAlum = new double[a];
for(int i =0; i < a; i++){
suma = 0.0;
28
for(int j=0; j < m; j++){
suma = suma + calif[j][i];
}
promAlum[i] = suma / m;
} // for i
System.out.println("---------------------------------------------------------------------------------------");
System.out.println("Promedio de Alumnos");
for(int i =0; i < a; i++)
System.out.println("El promedio del alumno "+i+" es; "+promAlum[i]);
} // main
} // class
29
MAIN.import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader leer=new BufferedReader(new InputStreamReader(System.in));
System.out.println("introduzca el numero de columnas");
int col=Integer.parseInt(leer.readLine());
System.out.println("introduzca el numero de filas");
int fil=Integer.parseInt(leer.readLine());
System.out.println("introduzca el numero de la tercera dimension:");
int prof=Integer.parseInt(leer.readLine());
int matriz[][][]=new int[fil][col][prof];
//recorrido con tres for
for(int i=0; i<fil;i++){
for(int j=0;j<col;j++){
for(int z=0;z<prof;z++){
System.out.println ("Introduzca el valor de matriz en la posicion:"+i +j +z);
matriz[i][j][z]=Integer.parseInt(leer.readLine());
}
}
}
32
for(int i=0; i<fil;i++){
for(int j=0;j<col;j++){
for(int z=0;z<prof;z++){
System.out.println("la matriz esta compuesta por:"+matriz[i][j][z]);
}
}
}
}
}
33