Ayuda en recursividad
Publicado por Yasar (9 intervenciones) el 29/10/2020 06:21:08
Hola, necesito ayuda en este programa de recursividad ya que no hace bien la comparación.

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
import java.io.IOException;
import java.util.Scanner;
public class ProgramarUnAlgoritmoRecursivoQueMuestreElNumeroMenorDeUnVector {
public static int menorvec(int x [], int n, int menor) {
if (n == 0) {
if (menor > x [n]) {
return x [0];
}
else {
return menor;
}
}
else{
if (menor > x [0]) {
return menorvec(x, n - 1, x [n]);
}
else {
return menorvec (x, n - 1, menor);
}
}
}
public static void main(String[] args) throws IOException{
Scanner scanner = new Scanner(System.in);
int[] arreglo = null;
int menor;
int tamano = 0;
int resultado = 0;
System.out.println("Ingrese el tamaño del arreglo: ");
tamano = scanner.nextInt();
arreglo = new int[tamano];
for(int i = 0; i < tamano; i++) {
System.out.println("Ingrese el elemento numero " + (i + 0) + " :");
arreglo[i] = scanner.nextInt();
}
menor = arreglo[0];
resultado = menorvec(arreglo, tamano, menor);
System.out.println("El elemento menor de este vector es " + resultado);
}
}

Valora esta pregunta


0