SHOW:
|
|
- or go back to the newest paste.
1 | #include <iostream> | |
2 | using namespace std; | |
3 | ||
4 | ||
5 | int BusqMaxySiguienteEnVector(int v[], int n, int clave){ | |
6 | ||
7 | //si para hacer una tarea tan simple como encontrar el maximo | |
8 | //necesitas tantas variables, desconfiá. tiene que haber un modo más simple. | |
9 | int posic; | |
10 | int j=1; | |
11 | int u=n-1; | |
12 | int maximo; | |
13 | int segundo; | |
14 | int posicSegundo; | |
15 | ||
16 | //este primer if para saber si el primero o el segundo son maximo | |
17 | //deberia poder resolverlo el mismo algoritmo, o a lo sumo una pequeña modificacion: | |
18 | //tomar el primero como máximo siempre. | |
19 | if (v[0]>v[1]) | |
20 | { | |
21 | posic=0; | |
22 | maximo=v[0]; | |
23 | ||
24 | posicSegundo=1; | |
25 | segundo=v[1]; | |
26 | ||
27 | }else{ | |
28 | posic=1; | |
29 | maximo=v[1]; | |
30 | ||
31 | posicSegundo=0; | |
32 | segundo=v[0]; | |
33 | } | |
34 | ||
35 | ||
36 | for(j=1; j<u; j++) | |
37 | { | |
38 | if(v[j] > maximo){ | |
39 | segundo=maximo; | |
40 | posicSegundo=posic; | |
41 | posic=j; | |
42 | maximo=v[j]; | |
43 | ||
44 | //esto no lo entiendo. el maximo siempre va a ser mayor al segundo | |
45 | }else if (maximo>segundo){ | |
46 | posicSegundo=j; | |
47 | segundo=v[j]; | |
48 | } | |
49 | } | |
50 | return posic; | |
51 | } | |
52 | ||
53 | int main(){ | |
54 | ||
55 | int j; | |
56 | int arr [30]; | |
57 | int maximo; | |
58 | int segundo; | |
59 | ||
60 | for (j=0; j<30; j++){ | |
61 | ||
62 | arr[j]=j; | |
63 | } | |
64 | ||
65 | //no se que es el tercer parametro, y como la funcion solo puede devolver un valor | |
66 | //mejor pasarle donde guardar el maximo y el segundo como parametro | |
67 | maximo = BusqMaxySiguienteEnVector(arr, 30, 100); | |
68 | ||
69 | ||
70 | cout <<"Maximo es: "<< maximo << " y " << "segundo es: "<< segundo << endl; | |
71 | ||
72 | ||
73 | return 0; | |
74 | } |