problem in c++

hello everyone, firstly happy holidays

now to the problem:hello everyone could you help fix my code it keeps repeating the same question and doesn't print the necessary results.

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 #include <iostream>
#include <math.h>
#include <locale.h>
#define l 200
#include <algorithm>
using namespace std;

///P inteiros, sendo o i-ésimo número a indicação da ordem de chegada do piloto
///i na corrida (o primeiro número indica a ordem de chegada do piloto 1 naquela
///corrida, o segundo número indica a ordem de chegada do piloto 2 na corrida, e
///assim por diante).




  struct Piloto {
    int identificador;
    int pontuacao = 0;
};

void limpaMatriz(int matriz[l][l]) {
    for(int  i = 0; i < l; i++) {
        for(int j = 0; j < l; j++) {
            matriz[i][j] = 0;
        }
    }
}

bool ordenaPilotos(Piloto a, Piloto b) {
    return a.pontuacao > b.pontuacao;
}

bool eOrdenaPorIdentificador(Piloto a, Piloto b) {
    return a.identificador < b.identificador;
}

int main()
{
    setlocale(LC_ALL, "Portuguese");
    while(1) {
        int corridas, pilotos;
        cout << "digite numero de corridas e de pilotos( caso queira sair digite 0 e 0)"<<endl;
        cin >> corridas >> pilotos;

        if(pilotos <=0 and corridas <= 0) {
            break;
        }

        int posicoes_por_corrida[corridas][pilotos];

        for(int i = 0; i < corridas; i++) {
            for(int j = 0; j < pilotos; j++) {
                cout<< "posiçoes por corridas"<<endl;
                cin >> posicoes_por_corrida[i][j];
            }
        }

        int sistemas;
        cout <<" sistema até 10 \n"<<endl;
        cin >> sistemas;

        int descricao_sistemas[l][l];

        limpaMatriz(descricao_sistemas);
        int posicao_final[sistemas];

        for(int i = 0; i < sistemas; i++) {
            cout <<"posição final" <<endl;
            cin >> posicao_final[i];

            for(int j = 0 ; j < posicao_final[i]; j++) {
                cout <<"Descrição do sistema(até 100)\n";
                cin >> descricao_sistemas[i][j];
            }
        }


        Piloto piloto[pilotos];

        for(int i = 0; i < pilotos; i++) {
            piloto[i].identificador = i + 1;
        }

        for(int h = 0; h < sistemas; h++) {

            for(int i = 0; i < corridas; i++) {
                for(int j = 0; j < pilotos; j++) {
                    int indice = posicoes_por_corrida[i][j] - 1;

                    if(indice < posicao_final[h]) {
                        piloto[j].pontuacao += descricao_sistemas[h][indice];
                    }
                }
            }

            sort(piloto, piloto + pilotos, ordenaPilotos);

            int maior = piloto[0].pontuacao;
            int repeticao = 0;

            for(int i = 0; i < pilotos; i++) {
                if(maior == piloto[i].pontuacao) {
                    repeticao++;
                }
            }

            if(repeticao == 1) {
                cout << piloto[0].identificador;
            }

            else if(repeticao > 1) {
                sort(piloto, piloto + repeticao, eOrdenaPorIdentificador);
                for(int i = 0; i < repeticao; i++) {
                    cout << piloto[i].identificador << " ";
                }
            }

            cout << "\n";

            for(int i = 0; i < pilotos; i++) {
                piloto[i].pontuacao = 0;
            }

            for(int i = 0; i < pilotos; i++) {
                piloto[i].identificador = i + 1;
            }

            for(int i = 0; i < pilotos; i++) {
                piloto[i].pontuacao = 0;
            }
        }

    }
    return 0;
}
when i ran it it just exited after getting a value.
what I see:
your first loop gets 2 numbers.
if both are negative, it exits the loop. if not, it goes again.
however they are used (illegally, since array sizes CANNOT be variables) to craft an array where the size can't be negative.
it looks like it should say
do
cout << "prompt stuff\n";
cin >> corridas >> pilotos;
while(corridas <1 || pilotos <1); //must be 1 or more NOTE THE OR, AND is incorrect here!

also note that crridas & pilotos are LOCAL TO THE LOOP and should not continue to exist outside the loop. so the values you read in are discarded! Declare them outside the loop at main's scope.

{
int x;
}//x is destroyed here, it only exists in the scope (set of braces) where it was created.


note: see how I cleaned up the if/break + infinite loop just by changing the loop style and its condition? See how that is cleaner? Your idea is OK (your logic is the problem, not the idea) but it is clunky.
Last edited on
could you explain me better?
what do you mean illegal?
the code has a bug, because when placing 2 variables with espõ between them, the error
Last edited on
Line 49,65,78: In standard C++, array bounds must be constant values. Your array bounds are not constant values. That's not legal in standard C++.
If the size of the array is not a fixed value that is known beforehand, i.e. at compile-time, and if you are programming in C++ (not just C), then it's probably the best to use a std::vector.

1
2
3
4
5
6
7
8
void foo(const size_t n)
{
    std::vector<int> vec(n);
    for (size_t i = 0; i < n; ++i)
    {
        vec[n] = /*whatever*/;
    }
}
Last edited on
Topic archived. No new replies allowed.