Solucion Ejercicio 1 de Vectores

download Solucion Ejercicio 1 de Vectores

of 3

Transcript of Solucion Ejercicio 1 de Vectores

Solucion Ejercicio 1 - Laboratorio 8 - Vectorespackage problemavector1;

import java.util.Scanner;

public class ProblemaVector1 {

private Scanner teclado; private int[] n;

public void cargar() { teclado = new Scanner(System.in); n = new int[8]; for (int f = 0; f < 8; f++) { System.out.print("Ingrese valor del vector:"+f); n[f] = teclado.nextInt(); } } public void sumavector() { int suma = 0; for (int f = 0; f < 8; f++) { suma = suma + n[f]; } System.out.print("La suma del vector n es = " + suma); } public void sumamayores36() { int suma36 = 0; for (int f = 0; f < 8; f++) { if (n[f] > 36) { suma36 = suma36 + n[f]; } } System.out.print("La suma de elem. dell vector > 36 es = " + suma36); }

public void cantidadvaloresmayor50() { int cantidad = 0; for (int f = 0; f < 8; f++) { if (n[f] > 50) { cantidad = cantidad + 1; } } System.out.print("La cantidad de elementos > 50 es = " + cantidad); } public static void main(String[] args) { ProblemaVector1 pv = new ProblemaVector1(); pv.cargar(); pv.sumavector(); pv.sumamayores36(); pv.cantidadvaloresmayor50(); // TODO code application logic here } }

Solucion Ejercicio 2 - Laboratorio 8 - VectoresEnunciado2. Realizar un programa que pida la carga de dos vectores numricos enteros de 4 elementos. Obtener la suma de los dos vectores, dicho resultado guardarlo en un tercer vector del mismo tamao. Sumar componente a componente.package sumarvector;import java.util.Scanner;

/**** @author arroyopaz*/

public class SumarVector { private Scanner teclado; private int[] a, b, c;

public void ingresarvectorA() { teclado = new Scanner(System.in); a = new int[4]; System.out.println("Vector A"); for (int i = 0; i < 4; i++) { System.out.print("Ingrese componente " + (i+1)+" = "); a[i] = teclado.nextInt(); } }

public void ingresarvectorB() { teclado = new Scanner(System.in); b = new int[4]; System.out.println("Vector B"); for (int i = 0; i < 4; i++) { System.out.print("Ingrese componente " + (i+1)+ " = "); b[i] = teclado.nextInt(); } }

public void sumarvector() { c = new int[4]; for (int i = 0; i < 4; i++) { c[i] = a[i]+b[i]; } } public void mostarvectorC() { System.out.println("Suma de Vector A + B"); for (int i = 0; i < 4; i++) { System.out.println("Componente "+(i+1)+" = "+c[i]); } }

public static void main(String[] args) { // TODO code application logic here SumarVector sv1 = new SumarVector(); sv1.ingresarvectorA(); sv1.ingresarvectorB(); sv1.sumarvector(); sv1.mostarvectorC(); } }