Java - Cambio de código para máximos, mínimos y que arroje el promedio

 
Vista:
Imágen de perfil de Nataly

Cambio de código para máximos, mínimos y que arroje el promedio

Publicado por Nataly (1 intervención) el 28/10/2014 18:31:51
Cod

Buenas tardes, ¿Qué modificar en el código para que lea datos de tipo double y me arroje el valor mínimo, máximo y el promedio de los números?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
 
package arreglo;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class Arreglo {
 
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Se necesita un argumento, nombre del archivo con numeros!");
}
else
{
File file = new File(args[0]);
int l = (int) (file.length()/2);
int [] arr;
arr = new int[l];
 
Charset charset = Charset.forName("US-ASCII");
 
Path p1 = Paths.get(args[0]);
int cnt=0,elem;
 
try (BufferedReader reader = Files.newBufferedReader(p1, charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
try
{
elem = Integer.parseInt(line);
arr[cnt++]=elem;
}
catch (NumberFormatException nfe)
{System.out.println("No es un numero");}
}
reader.close();
} catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
// Imprimir el contenido inicial
System.out.println("Contenido inicial del arreglo de "+cnt+" elementos");
for (int i=0;i<cnt;i++) System.out.print(arr[i]+" ");
System.out.println();
// Colocar aqui funciones que modifican el arreglo arr[] que tiene cnt elementos 
// que ocupan casillas desde [0] hasta [cnt-1] 
for (int i=0;i<cnt;i++)
{
if (arr[i]<0) { arr[i]=-arr[i]+1; }
if (arr[i]>0) { arr[i]=arr[i]-1; }
}
// Imprimir el contenido modificado 
System.out.println("Contenido final del arreglo de "+cnt+" elementos");
for (int i=0;i<cnt;i++) System.out.print(arr[i]+" ");
System.out.println();
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Cambio de código para máximos, mínimos y que arroje el promedio

Publicado por Andrés (340 intervenciones) el 02/11/2014 05:43:12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class Arreglo {
 
    public static void main(String[] args) {
        if (args.length == 0) {
          System.out.println("Se necesita un argumento, nombre del archivo con numeros!");
        }
        else
        {
            File file = new File(args[0]);
            int l = (int) (file.length()/2);
            double [] arr;
            arr = new double[l];
 
        Charset charset = Charset.forName("US-ASCII");
 
        Path p1 = Paths.get(args[0]);
        int cnt=0;
        double elem;
 
        try (BufferedReader reader = Files.newBufferedReader(p1, charset)) {
          String line = null;
          while ((line = reader.readLine()) != null) {
            try
              {
                 elem = Double.parseDouble(line);
                 arr[cnt++]=elem;
              }
            catch (NumberFormatException nfe)
              {System.out.println("No es un numero");}
          }
          reader.close();
        } catch (IOException x) {
           System.err.format("IOException: %s%n", x);
         }
         // Imprimir el contenido inicial
        System.out.println("Contenido inicial del arreglo de "+cnt+" elementos");
         for (int i=0;i<cnt;i++) System.out.print(arr[i]+" ");
        System.out.println();
         // Colocar aqui funciones que modifican el arreglo arr[] que tiene cnt elementos
         // que ocupan casillas desde [0] hasta [cnt-1]
        for (int i=0;i<cnt;i++)
        {
          if (arr[i]<0) { arr[i]=-arr[i]+1; }
          if (arr[i]>0) { arr[i]=arr[i]-1; }
        }
         // Imprimir el contenido modificado
        System.out.println("Contenido final del arreglo de "+cnt+" elementos");
         for (int i=0;i<cnt;i++) System.out.print(arr[i]+" ");
        System.out.println();
 
        //ubicar el maximo, minimo y promedio
        //tambien podriamos usar los métodos min y max de Collections, pero!,
        //Hay que calcular el promedio 
        double min = 0d, max =0d, sum=0d;
        for(double elemento: arr) {
          if(elemento< min) {
        	  min = elemento;
          }
 
          if(elemento > max) {
        	  max = elemento;
          }
 
          sum += elemento;
         }
 
        System.out.println("min:: "+min);
        System.out.println("max:: "+max);
        System.out.println("promedio:: "+(sum/arr.length));
 
        }
 
    }
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar