Docsity

Semana 12 algoritmia y programación, Ejercicios de Algoritmos y Programación

Toggle sidebar
bg1
PROGRAMA DE ESTUDIO: INGENIERIA DE COMPUTACIÓN Y SISTEMAS
CURSO: ICSI-506- ALGORITMIA Y PROGRAMACIÓN
TRABAJO Nº 8: MÉTODOS Y ARREGLOS
DOCENTE:
1
TRABAJO 8 - MÉTODOS Y ARREGLOS
Resolver los siguientes problemas utilizando métodos y arreglos
Indicaciones
• Resolver los siguientes problemas utilizando métodos y arreglos
PROBLEMAS
1. Diseñe un algoritmo que lea las edades de N alumnos y reporte:
Todos los datos ingresados
La lista de las edades mayores de edad, indicando cuantas son.
La lista de las edades menores de edad, indicando cuantas son.
La edad promedio.
Edad[],n
Edad[],n,pro,d,c
import java.io.*;
import java.util.Scanner;
public class p1
{static Scanner sc = new Scanner(System.in);
static final int max = 100;
public static void main(String[] args)throws IOException
{
int n, may, men;
int Edad[] = new int[max];
n = LeerN();
LeerDatos(n, Edad);
ReporteDatos(n, Edad);
may = CalcularMay(n, Edad);
men = CalcularMen(n, Edad);
CalcularProm(n, Edad);
Reportar(may, men);
}
static int LeerN(){
int n;
do{
System.out.println("Ingrese cantidad de datos: ");
n = sc.nextInt();
}while(n <= 0 || n > 100);
return n;
}
static void LeerDatos(int n, int Edad[]){
for(int i = 0; i < n; i++){
do{
System.out.println("Ingrese Edad: ");
Edad[i] = sc.nextInt();
}while(Edad[i] <= 0);
}
return;
}
static void ReporteDatos(int n, int Edad[]){
System.out.print("\nReporte de datos: ");
for(int i = 0; i < n; i++)
{
System.out.print(Edad[i]+" ");
}
return;
}
static int CalcularMay(int n, int Edad[]) {
int may = 0;
System.out.print("\nReporte de mayores: ");
for(int i = 0; i < n; i++) {
if(Edad[i] >= 18)
{
System.out.print(Edad[i]+" ");
may++;
}
}
return may;
}
static int CalcularMen(int n, int Edad[]) {
int men = 0;
System.out.print("\nReporte de menores: ");
for(int i = 0; i < n; i++) {
if(Edad[i] < 18){
System.out.print(Edad[i]+" ");
men++;
}
}
return men;
}
static void CalcularProm(int n, int Edad[]){
int se = 0, prom;
for(int i = 0; i< n; i++){
se+=Edad[i];
}
prom = se / n;
System.out.println("Promedio de edades: "+prom);
return;
}
static void Reportar(int may, int men){
System.out.println("N° de mayores de edad: "+may);
System.out.println("N° de menores de edad: "+men);
return;
}
}
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Semana 12 algoritmia y programación y más Ejercicios en PDF de Algoritmos y Programación solo en Docsity!

CURSO: ICSI- 506 - ALGORITMIA Y PROGRAMACIÓN

TRABAJO Nº 8: MÉTODOS Y ARREGLOS

TRABAJO 8 - MÉTODOS Y ARREGLOS

Resolver los siguientes problemas utilizando métodos y arreglos

Indicaciones

• Resolver los siguientes problemas utilizando métodos y arreglos

PROBLEMAS

1. Diseñe un algoritmo que lea las edades de N alumnos y reporte:

Todos los datos ingresados

La lista de las edades mayores de edad, indicando cuantas son.

La lista de las edades menores de edad, indicando cuantas son.

La edad promedio.

Edad[],n Edad[],n,pro,d,c

import java.io.*; import java.util.Scanner; public class p1 {static Scanner sc = new Scanner(System.in); static final int max = 100; public static void main(String[] args)throws IOException { int n, may, men; int Edad[] = new int[max]; n = LeerN(); LeerDatos(n, Edad); ReporteDatos(n, Edad); may = CalcularMay(n, Edad); men = CalcularProm(n, Edad); CalcularMen(n, Edad); }^ Reportar(may, men); static int LeerN(){ int n; do{ System.out.println("Ingrese cantidad de datos: "); }while(n <= 0 || n > 100);^ n = sc.nextInt(); }^ return n; static void LeerDatos(int n, int Edad[]){ for(int i = 0; i < n; i++){ do{ System.out.println("Ingrese Edad: "); }while(Edad[i] <= 0);^ Edad[i] = sc.nextInt(); return;^ } } static void ReporteDatos(int n, int Edad[]){ System.out.print("\nReporte de datos: "); for(int i = 0; i < n; i++) { }^ System.out.print(Edad[i]+" "); }^ return; static int CalcularMay(int n, int Edad[]) { int may = 0; System.out.print("\nReporte de mayores: "); for(int i = 0; i < n; i++) { if(Edad[i] >= 18) { System.out.print(Edad[i]+" "); may++; }^ } return may; } static int CalcularMen(int n, int Edad[]) int men = 0; { System.out.print("\nReporte de menores: "); for(int i = 0; i < n; i++) { if(Edad[i] < 18){ System.out.print(Edad[i]+" "); }^ men++; } return men; } static void CalcularProm(int n, int Edad[]){ int se = 0, prom; for(int i = 0; i< n; i++){ se+=Edad[i]; } prom = se / n; return;^ System.out.println("Promedio de edades: "+prom); } static void Reportar(int may, int men){ System.out.println("N° de mayores de edad: "+may); System.out.println("N° de menores de edad: "+men); return; }^ }

CURSO: ICSI- 506 - ALGORITMIA Y PROGRAMACIÓN

TRABAJO Nº 8: MÉTODOS Y ARREGLOS

  1. Diseñe un algoritmo que permita ingresar N números enteros positivos en un arreglo A y N números enteros positivos en un arreglo B (como máximo 100 para cada uno). Reportar el arreglo A, y después el arreglo B. Luego, sumar cada elemento A[i], del arreglo A con cada elemento B[i], del arreglo B y almacenar cada resultado en un arreglo C, en la posición C[i]. Al final reportar el arreglo resultante C.

N,a[i],b[i] A[i],b[i],c[i]

import java.io.*; import java.util.Scanner; public class p {static Scanner sc = new Scanner(System.in); static int max = 100; public static void main(String[] args) throws IOException { int n; int[] A = new int[max]; int[] B = new int[max]; int[] C = new int[max]; n = LeerN("\nCantidad de números a ingresar: "); ingresar(n, A, B); suma(n, A, B, C); reportar(n, A, B, C); } static int LeerN(String msj) { int n; do{ System.out.println(msj); n = sc.nextInt(); }while (n <= 0 || n>100); return n; } static void ingresar(int n, int A[], int B[]){ System.out.println("\nIngreso de datos A y B"); for (int i = 0; i < n; i++){ A[i] = LeerN("Ingrese A(" + (i + 1) + "): "); B[i] = LeerN("Ingrese B(" + (i + 1) + "): "); } return; } static void suma(int n, int[] A, int[] B, int C[]){ for(int i = 0; i < n; i++) { C[i] = A[i] + B[i]; } return; } static void reportar(int n, int A[], int B[], int C[]){ System.out.println("\nReporte de C[]"); for(int i = 0; i < n; i++){ System.out.println(A[i] + " + " + B[i] + " = " + C[i]); } return; } }

CURSO: ICSI- 506 - ALGORITMIA Y PROGRAMACIÓN

TRABAJO Nº 8: MÉTODOS Y ARREGLOS

DOCENTE: 4

  1. Ingresar N números enteros positivos a un arreglo A. Reportar A. Luego en un arreglo B, en cada posición B[i], almacenar la cantidad de dígitos de cada A[i]. Luego reportar A, y B en paralelo.

N,a[i] A[i],b[i]

import java.io.*; import java.util.Scanner; public class p4{ static Scanner sc = new Scanner(System.in); static int MAX = 100 ; public static void main ( String [] args ) throws IOException { int n ; int A [ ] = new int [ MAX ] ; int B [ ] = new int [ MAX ] ; do { n = LeerN ( "Ingresar cantidad de números deseados : ") ; } while (n<=0 || n > 100 ) ; ingreso( n , A) ; reportea(n, A); Calculo( n , A , B) ; reporte( n , A , B ) ; } static int LeerN ( String msj ){ int n ; do { System.out.print( msj ) ; n = sc.nextInt(); } while ( n <= 0 || n>100) ; return n ; } static void ingreso( int n , int A [ ]) { for (int i = 0 ; i < n ; i ++ ) { System.out.print ("Ingresar número para A" + ( i + 1 ) + " : " ); do{ A [i] = sc.nextInt(); }while(A[i]<=0 || A[i]>100); } return ; } static void reportea(int n, int A[]){ System.out.print("\nArreglo a: "); for (int i =0;i<n;i++) { System.out.print(A[i]+" "); } } static void Calculo( int n , int A [ ] , int B [ ]){ int c,n1; for (int i = 0; i < n; i++) { c=0; n1=A[i]; while (n1!=0) { n1 /= 10; c++; } B[i]=c; } return ; } static void reporte( int n , int A [ ] , int B [ ] ) throws IOException { System.out.print("\nArreglo a: "); for (int i =0;i<n;i++) { System.out.print(A[i]+" "); } System.out.print("\nArreglo b: "); for (int i =0;i<n;i++) { System.out.print(B[i]+" "); } } }

CURSO: ICSI- 506 - ALGORITMIA Y PROGRAMACIÓN

TRABAJO Nº 8: MÉTODOS Y ARREGLOS

  1. Ingresar N números enteros positivos a un arreglo A. y N números enteros positivos en un arreglo B. Luego Reportar A y Reportar B. Después, en un arreglo P, en cada posición P[i], almacenar el producto por sumas sucesivas de A[i] * B[i]. Luego reportar A, B y C en paralelo.

N,a[i],b[i] A[i],b[i],c[i]

import java.io.; import java.util.Scanner; public class p5 { static final int max = 100; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int a[] = new int[max]; int b[] = new int[max]; int p[] = new int[max]; int n; n = leer(); ingresa(n, a, b); reporteab(n, a, b); calcular(n, a, b, p); reporte(n, a, b, p); } static int leer() throws IOException { Scanner sc = new Scanner(System.in); int n; do { System.out.println("Veces a repetir"); n = sc.nextInt(); } while (n <= 0); return n; } static void ingresa(int n, int a[], int b[]) throws IOException { Scanner sc = new Scanner(System.in); for (int i = 0; i < n; i++) { do { System.out.println("Ingresar un número"); a[i] = sc.nextInt(); } while (a[i] <= 0); do { System.out.println("Ingresa un número"); b[i] = sc.nextInt(); } while (b[i] <= 0); } } static void reporteab(int n, int a[], int b[]) throws IOException { System.out.print("\nArreglo A"); for (int i = 0; i < n; i++) { System.out.print(" "+a[i] + " "); } System.out.print("\nArreglo B"); for (int i = 0; i < n; i++) { System.out.print(" "+b[i]+" "); } } static void calcular(int n, int a[], int b[], int p[])throws IOException{ int prod,n1,n2; for (int i=0; i<n; i++) { prod=0; n1=a[i]; n2=b[i]; for (int c=0; c<n2 ; c++) { prod=prod+n1; } p[i]=prod; } } static void reporte(int n, int a[], int b[], int p[]){ System.out.println("\nReporte de los resultado"); for (int i = 0; i < n; i++) { System.out.println(a[i]+"\t\t"+b[i]+"\t=\t"+p[i]); } } }