Show the output of a custom vector

Asked

Viewed 84 times

-1

I have a vector with 16 elements. I want to show the elements of this vector using the repetition loop for. Output must be equal to this:

A
1
2
3
4
B
A
5
6
7
8
B
A
9
10
11
12
B
A
13
14
15
16
B

As the tie ended, there is no need to show the "To" just below the last B. One remark: I only gave the value 16 as an example, but it can be any value, for example: 4, 5, 17, 25, 30, 100, etc.
I tried this code:

for($k = 0, $i = 0; $k <= 16 ; $k++, $i++){
  if($k % 4)==0){
      echo "A";
  }
  //Mostrar os números aqui
   if($k % 5)==0){
      echo "B";
  }
}

https://repl.it/@TaffarelXavier/ForDemo

but without any success.

  • Wouldn’t just change the $k <= 16 for $k <= 15?!

  • No, since 16 is just a number as an example, but it could be any number, as I showed in the question. Could be 4, 5, 17, 26, 40, 100,1000, etc.

  • Why did you define $i in the loop if you didn’t even use it? If you need to also display the number, why not put echo $k? Why do you start showing off A and does not display B, since 0 % 5 will be 0?

  • I fixed it, minus the $i, but I’m actually not getting the logic. I’d really like to show the output of the mentioned form.

  • 1

    The problem is that it is not clear what is the logic to define the "mentioned form".

  • I did. But thank you so much for trying.

Show 1 more comment

4 answers

3

I’m not sure I understand your question, but I updated your code, as the expected output you posted.

initially imagined that it would be printed BA only for multiples of 4 and 5, but saw that in fact the logic is to print every 5 iterations.

<?php
     include 'professores.php';

     $total = count($Plugins['professores']['plugins']);
     $count = 0;
     for($k = 0; $k <= $total ; $k++){  
         if ($k == 0){
            echo "A";
            echo "\n" ;
         } else if($count == 5){      
               echo "B";
               echo "\n";
               echo "A";
               echo "\n";
               echo $k;
               echo "\n";
               $count = 0;
          } else {    
               echo $k;
              echo "\n";
          }
          $count++;
     }
     echo "B";
?>

3

I also did but without using counter, only conditions, stay here as another example.

for($k = 0; $k <= 16; $k++){

  if(((($k-1) % 4) == 0 && ($k-1) != 0) || $k == 0){
      echo "A";
      echo PHP_EOL;
  }

    echo $k;
    echo PHP_EOL;

  if($k % 4 == 0 && $k != 0){
    echo "B";
    echo PHP_EOL;
  }

}

3


I believe I could simply do an array with the numbers (range) and divide it (array_chunk):

<?php

$divisor = 4;
$maximo = 16;

foreach(array_chunk(range(1, $maximo), $divisor) as $k) {
  echo "A" . PHP_EOL;
  echo implode($k, PHP_EOL) . PHP_EOL;
  echo "B" . PHP_EOL;
}

You can test this here.

A
1
2
3
4
B
A
5
6
7
8
B
A
9
10
11
12
B
A
13
14
15
16
B
  • It worked too. Thank you, my friend.

1

I made a simpler example, take a look and see if it’s what you’re looking for..

include 'professores.php';

$count = count($Plugins['professores']['plugins']);

//Contador de 4 em 4 adiciona um AB
$contador = 0;

//Tamanho do for a percorrer
$size = 16;
for($k = 1; $k <= $size ; $k++){

  //Se for o primeiro adiciona A antes
  if($k == 1)
    echo "A\n";

  // Se Contador == 4 e $k diferente do tamanho do vetor ele printa AB e zera o contador
  if($contador==4 && $k != $size ){
    echo "A\n";
    echo "B\n";
    $contador = 0;
  }

  //Imprime  $k
  echo "$k".PHP_EOL;

  // Se $k == Size printa B
  if($k == $size){
    echo "B\n";
  }

  //Incrementa contador
  $contador++;
}
?>
  • 1

    Great, great! Thank you very much. It worked.

Browser other questions tagged

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