check sequence numbers visualg

Asked

Viewed 89 times

-1

I have a vector q will be filled by 6 random values and need to check if there are numbers in sequence in the vector. How would you do this ?

  • 1

    What do you mean in sequence? For example: 0 ,1 ,2 ,3 ...

  • 1

    That’s right 0,1,2,3...

  • If in sequence what is to be done?

1 answer

0


Good evening, you can after assigning the value Random, compare to see if the previous + 1 is equal

for(int i = 0; i<6; i++)
{
    //Nao lembro como usar o rand em c mas acho que e assim
    vetor[i] = rand();
    if((i>0) && ((vetor[i-1]+1) == vetor[i]))
    {
        printf("número sequencial");
    }
}
printf("nenhum numero sequencial");

This way you can get sequences like 14579(4 and 5 sequential) That check of i>0 is because if it is 0 it will turn a -1 in the other condition and would give error in the vector I was wrong to do it in c, but if you can understand the logic is the same. Good luck there, I hope I helped

  • For this to work you need to sort the vector before doing the check.

Browser other questions tagged

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