How to Count Integers Repeated Every 5 Values in an OR Vector to Each Row of a Table (Two-Dimensional Matrix, Example: matrix[3][5] ) C++

Asked

Viewed 52 times

0

I have a vector that only has integers 1 and 2 randomly allocated. Example:

int vetor[15] = {1,1,1,2,2,1,1,2,2,2,1,1,1,2,1};

How the vector would be printed:

    1 1 1 2 2 1 1 2 2 2 1 1 1 2 1

I have this same vector in table form (two-dimensional matrix):

int tabela[3][5] = {{1,1,1,2,2},{1,1,2,2,2},{1,1,1,2,1}};

How the table would be printed:

    [,0][,1][,2][,3][,4]
[0,]   1   1   1   2   2
[1,]   1   1   2   2   2
[2,]   1   1   1   2   1

Note that there are (in the example) 3 rows and 5 columns, but this may vary.

Would it be possible count the quantity of number 1 every 5 values (in the case of the one-dimensional vector) and store the responses in a one-dimensional vector? That is, each index of the vector is the count response of a line.

Response vector:

    int resposta[];

OR count the number 1 quantity on each line (which is the number of columns in the table) and store the responses in a one-dimensional vector (or two-dimensional, providing line and response to the side)?

int     respostaTabela[][2];

Anyway the values stored in the response vector would be:

resposta[0] = 3

resposta[1] = 2

resposta[2] = 4

The vector print would be:

3 2 4

The assignment of values to the response table would be the same as (first column to enumerate the row and second column to display the count):

int   respostaTabela[][2] = {{1, 3}, {2, 2}, {3, 4}};

And the impression would be:

[0,]   1   3
[1,]   2   2
[2,]   3   4

Thank you for your attention, a hug!

1 answer

0

I found myself the answer to three possibilities:

1º (from the vector):

  int contador5 = 0;

  int k = 5;

  int l = 0;

  for (int i = 0; i <= 10; i += 5)
     {
      for (int j = i; j < k; j++)
        {
           if(vetor [j] == 1)
              {
                contador5++;
              }
        }
     respostaVetor[l] = contador5;
     
     contador5 = 0;
     
     k+=5;
     
     l++;
     }

Printing of the first code:

    3 2 4

2nd (from the table storing the answers in a vector):

int   numeroLinhas = 3;

int   numeroColunas = 5;

int   contador5_tabela = 0;

for (int linha = 0; linha < numeroLinhas; linha++)
 {
    for (int coluna = 0; coluna < numeroColunas; coluna++)
    {
       if(tabela[linha][coluna] == 1)
         {
          contador5_tabela++;
         }
   }
   respostaTabela[linha] = contador5_tabela;
   
   contador5_tabela = 0;
 } 

Printing of the 2nd code:

    3 2 4

3rd (from the table storing the answers in another table, using the 1st column as a reference index to the row of the original table):

   int   respostaTabela_indice[3][2];
   
   for (int linha = 0; linha < numeroLinhas; linha++)
   {
      for (int coluna = 0; coluna < numeroColunas; coluna++)
      {
         if(tabela[linha][coluna] == 1)
           {
            contador5_tabela++;
           }
      }
      
    respostaTabela_indice[linha][0] = linha;
      
    respostaTabela_indice[linha][1] = contador5_tabela;
      
    contador5_tabela = 0;
    } 

Printing of the 3rd code:

    [,0][,1]
[0,]   0   3
[1,]   1   2
[2,]   2   4

I thank God and all my colleagues who have helped me in other places or given attention to the message. I hope it helps those who have difficulties in the same area A hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.