C/Visual C - Problema con funcion OrdenRegistros

 
Vista:
sin imagen de perfil

Problema con funcion OrdenRegistros

Publicado por Jesusito (12 intervenciones) el 13/12/2017 20:22:36
Hola a todos. Estoy haciendo un programa en C con dos tipos de struct. El caso es que uno contiene un vector de structs del otro, y me piden que ordene dicho vector segun N. Para ello tengo una función que se llama OrdenRegistros, que debería ordenar de forma creciente. No lo hace y por mas que lo intente no encuentro el fallo, por lo que dejare aquí el código por si alguien sabe en que me equivoco.

Aquí viene el código:
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
 
struct Registro {
    int N;
    char letra[5];
};
 
 
struct Lista_Reg{
    int N_Reg;
    struct Registro Clave[100];
};
 
 
 
struct Lista_Reg GenLista_Reg(){
 
    int i, j, k, HayIgual;
    int v[100];
    struct Registro Reg;
    struct Lista_Reg List;
    HayIgual = 0;
 
    srand(time(NULL));
    List.N_Reg = (rand()%99)+2;
 
    for (i = 0; i < List.N_Reg; i++){
        for (j = 0; j < 5; j++){
            Reg.letra[j] = rand();
        }
 
        Reg.N = rand();
        k = 0;
 
        while (k < i){
            if (Reg.N == v[k]){
                Reg.N = rand();
                k = -1;
            }
 
            k++;
        }
 
        List.Clave[i] = Reg;
    }
 
    return List;
}
 
 
struct Lista_Reg OrdenRegistros(struct Lista_Reg List){
 
    int i, j;
    struct Registro Reg1, Reg2, aux;
 
    for (i = 0; i < List.N_Reg; i++){
        Reg1 = List.Clave[i];
 
        for (j = i+1; j < List.N_Reg; j++){
            Reg2 = List.Clave[j];
 
            if (Reg1.N > Reg2.N){
                aux = List.Clave[i];
                List.Clave[i] = List.Clave[j];
                List.Clave[j] = aux;
            }
        }
    }
 
    return List;
}
 
 
 
 
 
/*******************************************************************************************************************************/
/*      main        */
/*******************************************************************************************************************************/
int main()
{
 
    int i;
    struct Lista_Reg Lista, ListaOrdenada;
    struct Registro R;
 
    Lista = GenLista_Reg();
    ListaOrdenada = OrdenRegistros(Lista);
 
 
    for (i = 0; i < Lista.N_Reg; i++){
        R = ListaOrdenada.Clave[i];
        printf("%i ", R.N);
    }
 
    printf("\n\n%i\n\n\n", ListaOrdenada.N_Reg);
 
 
}

Muchas gracias por vuestro tiempo
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: 28
Ha aumentado su posición en 2 puestos en C/Visual C (en relación al último mes)
Gráfica de C/Visual C

Problema con funcion OrdenRegistros

Publicado por Andrés (9 intervenciones) el 14/12/2017 07:20:02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Lista_Reg OrdenRegistros(struct Lista_Reg List){
 
    register int i, j;
    struct Registro  aux;
 
    for (i = 0; i < List.N_Reg-1; i++) {
 
        for (j = 0 ; j < List.N_Reg-1-i; j++) {
 
            if (List.Clave[j].N > List.Clave[j+1].N) {
 
                aux = List.Clave[j];
                List.Clave[j] = List.Clave[j+1];
                List.Clave[j+1] = aux;
 
            }
 
        }
 
    }
 
    return List;
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar
sin imagen de perfil

Problema con funcion OrdenRegistros

Publicado por Jesusito (12 intervenciones) el 14/12/2017 19:51:50
muchas gracias
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