C ++ Style when writing code

-3

Hi, I'm trying to learn C ++, but I notice that everyone has a different style when writing the code

Could someone explain to me what this piece of code does?

{
  for(
   c = 0 ;c<4 ; c++) 
  {
    desglose[a][c]=0;
  }
} 

I know it's a for cycle, but what do you mean by

desglose[a][c]=0;

Is there another way to interpret it?

    
asked by DOOM 18.09.2018 в 05:14
source

2 answers

0

I leave this reference link for you to understand that is a matrix .

In that link you will find more information on the subject, anyway, if you are right, it is a FOR cycle that runs through all the columns from the 'to' which we do not know what number it is, and assigns them 0 .

Example: Assuming your Matrix is 3 * 3 , it is completely filled with '1' and your a = 1 , the drawing of your matrix would look like this:

I hope it serves you! Greetings.

    
answered by 18.09.2018 / 05:33
source
0

desglose[][] is referencing a two-dimensional array (a matrix). Which its value in position [a][c] is being assigned to 0 .

For example: If you have a 3X3 matrix, it would look something like this:

   j j j   
i [][][]
i [][][]
i [][][]

If you mix two for cycles (as it seems to be your case) to fill the matrix with zeros you should do something like this:

for(int i = 0; i < 3; ++i){
    for(int j = 0; j < 3; ++j){
        desglose[i][j] = 0;
    }
}

Where the first cycle for corresponds to the value of your rows and that of the second cycle corresponds to your columns.

We can see it in the following way. In the first for cycle, which is repeated 3 times, a for cycle is created, which is repeated 3 times. This means that in the first iteration of the first cycle for the variable i will have a value of 0 . This value will continue in 0 for the 3 turns of the second for cycle. Where the variable j will increase.

desglose[0][0] = 0; // [variable i][variable j]
desglose[0][1] = 0; // [variable i][variable j]
desglose[0][2] = 0; // [variable i][variable j]

After fulfilling 3 iterations within the variable cycle j , the value of variable i of the first cycle is increased by 1. Whereas the variable j returns to a value of 0 and is increased again 3 times.

desglose[1][0] = 0; // [variable i][variable j]
desglose[1][1] = 0; // [variable i][variable j]
desglose[1][2] = 0; // [variable i][variable j]

This means that if the first for cycle has n iterations and the second cycle has m iterations a total of n*m iterations will be made to fill the matrix. In this case it is a matrix of 3*3 .

    
answered by 18.09.2018 в 05:47