How to apply the same condition/operation for each element of a vector in C

Asked

Viewed 47 times

0

Hi, guys!

I need to perform the same operation for all elements of a given vector. How to do this?

More specifically, I need to check if an element any "x" has, in its two adjacent positions larger than x, a number that is equal to x+1 or x+2. And, in this case, I need to verify that this same condition is valid for the other elements at positions greater than vector[x] (i.e., I need to apply this to "x" and for the n values at positions greater than "x").

To facilitate understanding, I will illustrate. Suppose we have the following entry:

1 2 4 5 6 8 9 12

For this case, suppose x=4.

My code needs to do something kind of like this:

5 = 4+1 or 4+2? Yes.

6 = 4+1 or 4+2? Yes.

6 = 5+1 or 5+2? Yes.

8 = 5+1 or 5+2? No.

8 = 6+1 or 6+2? Yes.

9 = 6+1 or 6+2? No.

9 = 8+1 or 8+2? Yes.

12 = 8+1 or 8+2? No.

12 = 9+1 or 9+2? No.

And then the program stops after having done all these checks for the elements equal or greater than x. And then the program should show on screen all cases "yes".

I managed to do this for x, but I have no idea how to do for the other cases I need. I wish I had at least some sense of what I can try...

I thought of doing what I want in a vector and passing the selected elements to another vector, and then showing on the screen. But I don’t know how to do this... I think I would first need to manipulate the elements larger than "x", but how?

For now, I tried it in C:

#include <stdio.h>
#include <stdbool.h>

int verifica_proximas_posicoes(int i, int x) {
    if((i == x + 1) || (i == x + 2)){
        return true;
    }
    return false;
}

int main(){

    int i, numero, vetor[8], vetor1[8];

    printf("Escreva todos os elementos do vetor:\n");
    for(i=0; i<8; i++){
        scanf("%d", &vetor[i]);
    }
    printf("Escreva o número que deseja:\n");
    scanf("%d", &numero);

    for(i=numero; i<=8; i++){
        if(verifica_proximas_posicoes(i+1, numero)){
            vetor1[i] = vetor[i];
            printf("%d ", vetor1[i]);
        }
    }

    return(0);
}

  • I believe this is his deduction: "And, in this case, I need to verify that this same condition is valid for the other elements in positions greater than vector[x] (that is, I need to apply this to "x" and for the n values in positions greater than that of "x")." is wrong. It seems to me that you should look for "x" in the vector and, if found, check whether the two adjacent positions meet the condition. Suppose the value "x" exists at position "i", in this case you have to check whether vector[i+1] or vector[i+2] meet the condition.

  • It would be of great value if you put the original problem

1 answer

0

I believe this code can help you better understand how you can get to the solution to your problem.

#include <stdio.h>

int main(void) {
  // Estas variáveis foram declaradas estaticamente com a finalidade de
  // explicar melhor o funcionamento do programa por si só, mas acredito
  // que você pode ler essas variáveis dinamicamente na sua implementação
  int index_of_x = 2, x = 4, vetor_inicial[] = {1, 2, 4, 5, 6, 8, 9, 12},
      tamanho_do_vetor = 8;

  for (int i = index_of_x + 1; i < tamanho_do_vetor; i++, x++) {
    if (vetor_inicial[i] == x + 1 || vetor_inicial[i] == x + 2) {
      printf("%d = %d+1 ou %d+2? Sim.\n", vetor_inicial[i], x, x);
    } else
      printf("%d = %d+1 ou %d+2? Não.\n", vetor_inicial[i], x, x);

    // Para evitar que o programa leia dados inválidos.
    if (i + 1 == tamanho_do_vetor) break;

    if (vetor_inicial[i + 1] == x + 1 || vetor_inicial[i + 1] == x + 2) {
      printf("%d = %d+1 ou %d+2? Sim.\n", vetor_inicial[i + 1], x, x);
    } else
      printf("%d = %d+1 ou %d+2? Não.\n", vetor_inicial[i + 1], x, x);
  }

  return 0;
}

This code produced the following result:

5 = 4+1 ou 4+2? Sim.
6 = 4+1 ou 4+2? Sim.
6 = 5+1 ou 5+2? Sim.
8 = 5+1 ou 5+2? Não.
8 = 6+1 ou 6+2? Sim.
9 = 6+1 ou 6+2? Não.
9 = 7+1 ou 7+2? Sim.
12 = 7+1 ou 7+2? Não.
12 = 8+1 ou 8+2? Não.

Browser other questions tagged

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