crash in accumulator of a vector?

0

I'm just starting programming and I have to do a dice game, the problem is that when I want to build an accumulator to know how many times each number is repeated, it creshes or reports false numbers the" dad "is a vector of 5 with random numbers and the "acum" is another vector of 5 zeros filled

I know it must be something very small in what I'm confusing, but I've been trying for 2 days to get the accumulator right, if anyone can help I'd appreciate it a lot

     int unjugador(char nombre[420]) {
     int dad[5] , i , j , temp , vectorpospunt[9] , punt=0 , nr=1 , puntp=0 
     , acum[5] , dec , posj ;

     while (punt<10000){


     for (i=0; i<6; i++){
     acum[i]=0;
     }
     puntp=0;
     posj=99;


     cout << "RONDA NRO :" <<nr << endl;

     tirardados(dad);
     ///truchardados(dad);
     mostrardados(dad);

    for(int i=0;i<5;i++){
     for(int j=i+1;j<=5;j++){
        if(dad[i]>dad[j]){cout << "acumulador de 1 es : " << acum[0] << endl 
    ;
         temp=dad[j];
         dad[j]=dad[i];
         dad[i]=temp;
        }
     }
     }

     ///acumulardados(acum, dad);

    /**for(i=0;i<6;i++){

    acum[i]=contarnumeros(dad, i+1);

    }**/

    acumulardadosdos(dad , acum );

    cout << "acumulador de 1 es : " << acum[0] << endl ;
    cout << "acumulador de 2 es : " << acum[1] << endl ;
    cout << "acumulador de 3 es : " << acum[2] << endl ;
    cout << "acumulador de 4 es : " << acum[3] << endl ;
    cout << "acumulador de 5 es : " << acum[4] << endl ;
    cout << "acumulador de 6 es : " << acum[5] << endl ;
    }

and the functions that probe for the accumulators are:

void acumulardados(int acum[] , int dad[]){
int i , j;
for (i=0;i<6;i++){
for(j=0;j<6;j++){
    if(dad[j]==i+1){
        acum[i]++;
    }
}
} 
}

int contarnumeros(int v[], int valorAbuscar){
int i, cantidad=0;
for (i=0;i<6 ;i++ )
{
if (v[i]==valorAbuscar)cantidad++;
}
return cantidad;
}


void acumulardadosdos(int dad[] , int acum[]){
int i ;
for (i=0;i<6;i++){
if (dad[i]==1){
        acum[0]++;
}else if (dad[i]==2){
acum[1]++;
}else if (dad[i]==3){
acum[2]++;
}else if (dad[i]==4){
acum[3]++;
}else if (dad[i]==5){
acum[4]++;
}else {
acum[5]++;
}
}
}
    
asked by RockAndBool 01.12.2017 в 22:10
source

1 answer

0

The problem.

Your problem seems to be the ignorance of the operation of the C ++ fixes and the loops for of C ++. You declare an arrangement of 5 elements:

//  vvvvvv <--- 5 elementos: dad[0], dad[1], dad[2], dad[3] y dad[4].
int dad[5] , i , j , temp , vectorpospunt[9] , punt=0 , nr=1 , puntp=0 
 , acum[5] , dec , posj ;

And you go through the first 6 elements:

//       vvv <--- 6 elementos: i = 0, i = 1, i = 2, i = 3, i = 4 e i = 5.
for (i=0;i<6;i++){
    // ...
}

The error.

Accessing memory pointers outside the scope of an array is a undefined behavior , this means anything could happen , from a runtime failure to your nostrils ejecting demons .

When your loop reaches the index 5 , you are accessing the sixth position of your array dad and this is a position beyond the size of that array, therefore you get the indefinite behavior.

Proposal.

Except for the use of std::cout , there is nothing in your code that suggests that you are working in C ++, my proposal is that you use the C ++ tools to improve your code:

  • Use std::string , not char[240] , it will give you less problems.

    int unjugador(const std::string &nombre) {
    //            ^^^^^^^^^^^^^^^^^^^^^^^^^ <--- Mas facil de usar.
    
  • Automatically initialize your fixes, not manually.

    // Anyadiendo {} al final de un arreglo este se inicializa a 0 automaticamente
    int dad[5]{} , i , j , temp , vectorpospunt[9]{}, punt=0 , nr=1 , puntp=0, acum[5]{}, dec , posj ;
    //        ~~                                  ~~                                  ~~
    
  • If you are going to pass fixes as a parameter, use references to fix or references to std::array (remember to mark them as constants if they are not going to be modified):

    //                 vvvvvvvvvvvvvv, vvvvvvvvvvvvv <--- Referencia a arreglo de 5 int
    void acumulardados(int (&acum)[5], int (&dad)[5]){
        // ...
    }
    
    //                vvvvvvvvvvv <--- Referencia a arreglo de 5 int
    int contarnumeros(int (&v)[5], int valorAbuscar){
        // ...
    }
    
    //                    vvvvvvvvvvvvv, vvvvvvvvvvvvvv <--- Referencia a arreglo de 5 int
    void acumulardadosdos(int (&dad)[5], int (&acum)[5]){
        // ...
    }
    
    using int5 = std::array<int, 5>;
    //                 vvvvvvvvvv, vvvvvvvv <--- Referencia a array de 5 int
    void acumulardados(int5 &acum, int5 dad){
        // ...
    }
    
    //                vvvvvvv <--- Referencia a array de 5 int
    int contarnumeros(int5 &v, int valorAbuscar){
        // ...
    }
    
    //                    vvvvvvvvv, vvvvvvvvvv <--- Referencia a array de 5 int
    void acumulardadosdos(int5 &dad, int5 &acum){
        // ...
    }
    
  • Complying with the previous point, your loops will not have index problems again if you use the for of rank:

    for (auto &d : dad){
        if (d==1){
            // ...
        }
     }
    
  • Use a map to count accumulations:

    using int5 = std::array<int, 5>;
    using acumulaciones = std::map<int, int>;
    
    void acumulardadosdos(const int5 &dad , acumulaciones &acum){
        for (const auto &d : dad){
            auto [iterador, insertado] = acum.insert({d, 0});
            ++iterador->second;
        }
    }
    
answered by 04.12.2017 в 08:53