Dev - C++ - Ejercicio de arreglos con subprogramas (Eliminar un vector de la posicion)

 
Vista:
Imágen de perfil de Brayan

Ejercicio de arreglos con subprogramas (Eliminar un vector de la posicion)

Publicado por Brayan (8 intervenciones) el 03/12/2016 07:11:55
Buenas noches, alguien podría ayudarme con este ejercicio? (Adjunto imagen), es que tengo el ejercicio de esta manera ( pego el código), y no me funciona, lega a una parte que me dice que "El programa.exe dejó de funcionar" Agradecería mucho de su gran ayuda! Gracias de igual manera!!
Ejercicio

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
#include <iostream>
using namespace std;
int i, j, val, vec2[100], n;
float vec[100];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void crear (float vec[100], int n){
	int i;
	for (i=1; i<=n; i++){
		cout<<"ingrese un valor en la posicion"<<i; cin>>vec[i];
	}
}
void valoresrepetidos (float vec[100], int n, int vec2[100]){
	int i, j, cont;
	for (i=1; n-1; i++){
		cont=1;
		for (j=i+1; i<=n; i++){
			if (vec[i]=vec[j]){
				cont=cont+1;
			}
		}
		vec2[i]=cont;
	}
}
float valoresmasrepetidos (int vec2[100], int n, float vec[100]){
	int mayor=vec2[1], valor;
	for (i=2; i<=n; i++){
		if (vec2[i]>mayor){
			mayor=vec[i];
			valor=vec[i];
		}
	}
	return valor;
}
void eliminar (float vec[100], int n, int val){
	int i, j;
	for (i=1; n-1; i++){
		if (vec[i]=val){
			for (j=i; n-1; j++){
				vec[j]= vec[j+1];
			}
			n=n-1;
		}
	}
	if (val=vec[i]){
		n=n-1;
	}
}
void imprimir (float vec[100], int n){
	int i;
	for (i=1; i<=n; i++){
		cout<<vec[i];
	}
}
int main(int argc, char** argv) {
	cout<<"Ingrese el tamaño del vector: "; cin>>n;
	crear (vec, n);
	valoresrepetidos (vec, n, vec2);
	valoresmasrepetidos (vec2, n, vec);
	eliminar (vec, n, val);
	imprimir (vec, n);
	return 0;
}

Dejaré esta version con un comentario (El void de imprimir ahora no esta, ya que lo metí en la función main):

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
#include <iostream>
using namespace std;
int i, j, val, vec2[100], n;
float vec[100];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void crear (float vec[100], int n){
	int i;
	for (i=1; i<=n; i++){
		cout<<"ingrese un valor en la posicion"<<i; cin>>vec[i];
	}
}
void valoresrepetidos (float vec[100], int n, int vec2[100]){
	int i, j, cont;
	for (i=1; n-1; i++){
		cont=1;
		for (j=i+1; i<=n; i++){
			if (vec[i]=vec[j]){
				cont=cont+1;
			}
		}
		vec2[i]=cont;
	}
}
float valoresmasrepetidos (int vec2[100], int n, float vec[100]){
	int mayor=vec2[1], valor;
	for (i=2; i<=n; i++){
		if (vec2[i]>mayor){
			mayor=vec[i];
			valor=vec[i];
		}
	}
	return valor;
}
void eliminar (float vec[100], int n, int val){
	int i, j;
	for (i=1; n-1; i++){
		if (vec[i]=val){
			for (j=i; n-1; j++){
				vec[j]= vec[j+1];
			}
			n=n-1;
		}
	}
	if (val=vec[i]){
		n=n-1;
	}
}
/*void imprimir (float vec[100], int n){
	int i;
	for (i=1; i<=n; i++){
		cout<<vec[i];
	}
}*/
int main(int argc, char** argv) {
	cout<<"Ingrese el tamaño del vector: "; cin>>n;
	for (i=1; i<=n; i++){
		cout<<vec[i];
	}
	crear (vec, n);
	valoresrepetidos (vec, n, vec2);
	valoresmasrepetidos (vec2, n, vec);
	eliminar (vec, n, val);
/*	imprimir (vec, n);*/
	return 0;
}
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: 661
Bronce
Ha mantenido su posición en Dev - C++ (en relación al último mes)
Gráfica de Dev - C++

Ejercicio de arreglos con subprogramas (Eliminar un vector de la posicion)

Publicado por agustin (522 intervenciones) el 03/12/2016 12:28:21
El codigo tenia muchos problemas como por ejemplo desbordamientos de buffer. Lo he corregido y modificado un poco para que elimine todos los miembros cuyo numero de repeticiones sea igual al numero de repeticiones mayor, o sea, si ingresas los valores:
1, 2, 3, 4, 5, 1, 2, 3, 1, 2
1 se repite 3 veces
2 se repite 3 veces
3 se repite 2 veces
4 y 5 no se repiten
Como hay un empate entre dos valores con el numero de repeticiones hay que eliminar ambos valores de la lista y eso es lo que hago:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
 
#define NELEMENTS 100
 
using namespace std;
 
enum {valor, nRepeticiones};
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//---------------------------------------------------------------------------
 
void clear(void){
    char a;
 
    do{
        a = cin.get();
    }while(a != '\n');
}
//---------------------------------------------------------------------------
 
void crear (int vec[NELEMENTS], int nElementos){
	for (int i = 0; i < nElementos; i++){
		cout << "ingrese un valor en la posicion [" << i << "]: ";
		cin >> vec[i];
	}
}
//---------------------------------------------------------------------------
 
int desecharMenoresDeListaDeRepetidos(int reps[NELEMENTS][2], int nRepetidos, int mayor){
    for (int i = 0; i < nRepetidos; i++){
        if(reps[i][nRepeticiones] < mayor){
	        for (int j = i; j < nRepetidos; j++){
                reps[j][valor] = reps[j+1][valor];
                reps[j][nRepeticiones] = reps[j+1][nRepeticiones];
                nRepetidos--;
            }
        }
    }
    return nRepetidos;
}
//---------------------------------------------------------------------------
 
void insertarEnListaDeRepetidos(int val, int repeticiones, int reps[NELEMENTS][2], int *nRepetidos){
    bool encontrado=false;
 
    for(int i = 0; i < *nRepetidos; i++){
        if(val == reps[i][valor])
            encontrado = true;
    }
    if(!encontrado){
        reps[*nRepetidos][valor] = val;
        reps[*nRepetidos][nRepeticiones] = repeticiones;
        (*nRepetidos)++;
    }
}
//---------------------------------------------------------------------------
 
int nRepsDelValorMasRepetido (int vec[NELEMENTS], int nElementos, int reps[NELEMENTS][2], int *nRepetidos){
	int mayor=0, contador;
 
	*nRepetidos=0;
	for (int i = 0; i < nElementos-1; i++){
		contador=0;
	    for (int j = i+1; j < nElementos; j++){
    		if (vec[j] == vec[i]){
		    	contador++;
    		}
		}
		if (contador > 0){
            insertarEnListaDeRepetidos(vec[i], contador, reps, nRepetidos);
            if(mayor < contador)
			    mayor = contador;
		}
	}
    *nRepetidos = desecharMenoresDeListaDeRepetidos(reps, *nRepetidos, mayor);
	return mayor;
}
//---------------------------------------------------------------------------
 
int eliminar(int vec[NELEMENTS], int nElementos, int reps[NELEMENTS][2], int nRepetidos, int nreps){
    for (int x = 0; x < nRepetidos; x++){
        for (int i = 0; i < nElementos; i++){
            if (vec[i] == reps[x][valor]){
                for (int j = i; j < nElementos; j++){
                    vec[j] = vec[j+1];
                }
                i--;
                nElementos--;
	        }
        }
    }
	return nElementos;
}
//---------------------------------------------------------------------------
 
void imprimir(int vec[NELEMENTS], int nElementos){
	for (int i=0; i<nElementos; i++){
		cout << vec[i] << " ";
	}
}
//---------------------------------------------------------------------------
 
int main(int argc, char* argv[]){
    int val, vec[NELEMENTS], repetidos[NELEMENTS][2], nElementos, nRepetidos, nreps_mayor;
 
	cout << "Ingrese el tamaño del vector: ";
	cin >> nElementos;
	crear(vec, nElementos);
	nreps_mayor=nRepsDelValorMasRepetido(vec, nElementos, repetidos, &nRepetidos);
	if (nreps_mayor > 0){
	    nElementos=eliminar(vec, nElementos, repetidos, nRepetidos, nreps_mayor);
	}else {
		cout << "No hay valores repetidos" << endl;
	}
    imprimir (vec, nElementos);
    clear();
    cin.get();
    return 0;
}
//---------------------------------------------------------------------------
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