Vector Help in Programming Logic

Asked

Viewed 62 times

0

The idea is to print according to the condition, only that I am lost in logic, because there is no position of the vector to receive the two values.

#include <stdio.h>

int main (){

    int N,X[1000],i,band=0;
    scanf("%d",&N);
    for (i=0; i<N ;i++) {
        scanf("%d", &X[i]);
        if(X[i]== 3 && (X[i] == 5))
        {
            band++;
        }
    }

    if(band!=0)
        printf("Vetor certo");
    else
        printf("Vetor errado");

    return 0;

}

2 answers

1

The right condition would be or (represented by ||).

if (X[i]== 3 || X[i] == 5) {
    band++;
}

That is, if the value of X[i] be 3 or be 5, the condition will be true.

0

As far as I can tell, you just confused the logical operators "E" with the "OR" Just a few good practice tips, avoid creating variables starting with maiscúla letter, and there is a way to do this exercise without defining an exact value for the vector size, if you want to warn!

int main()
{
    int N;
    int X[1000];
    int i;
    int band=0;
    scanf("%d",&N);
    for (i=0; i<N ; i++)
    {
        scanf("%d", &X[i]);
        if(X[i]== 3 || (X[i] == 5))
        {
            band++;
        }
    }

    if(band!=0)
        printf("Vetor certo");
    else

        printf("Vetor errado");

    return 0;
}
  • The right is with the logical operator E msm, type If the vector has 3 and 5, it prints "Right vector"... Obg by the tip man, if you are thinking of dynamic allocation, n can use yet, but vlw:)

  • I’ve already decided to glor...'-'

Browser other questions tagged

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