Resolve Operation with Set

Asked

Viewed 71 times

-2

Question 1 - Consider the set X = {x Z : 1 x 32}. Implement a program in C that performs the following instructions.

  1. Create three integer-type vectors of size 12: vector 0[12], vector 1[12] and vector 2[12].
  2. Store the elements belonging to X in the vector that leave 0 when divided by 3.
  3. Store in vector 1 the elements belonging to X that leave rest 1 when divided by 3.
  4. Store in vector 2 the elements belonging to X that leave rest 2 when divided by 3.
  5. Print the elements contained in vector 0.
  6. Print the elements contained in vector 1.
  7. Print the elements contained in vector 2.

Could someone help me with this code?

This afternoon a friend asked me to help resolve an issue in college.
I love programming, however it is many years since I finished high school and I can’t remember how to solve sets anymore. The only thing I remember is my teacher writing vectors on the blackboard or drawing polka dots to see if there was an intersection between two or more sets.

  • The set X represents the integers between 1 and 32 inclusive, so just declare and initialize each vector with the respective values. The exercise does not ask you to do this dynamically, so you can analyze it at hand (there are no joint operations in the exercise). Then just make three repeat loops and display the values of the vectors.

  • @Andersoncarloswoss This looks like a failure on the part of the teacher. That maybe I don’t give a damn if someone comes up with an answer like the one you suggest.

  • William, however, here on the site this type of question is considered very broad. Code requests from an enunciation only tend to be poorly viewed. I suggest you edit the question and include an attempt to solve it yourself.

  • Thanks @bfavaretto, I really found the question a little imprudent. Because my problem is that I do not know how to solve the mathematical problem. But thank you very much for the tip, and for the solution of the problem. Thank you.

  • Understood that the key is the module operator %? But note that the answer they gave you has a problem, it considers a set of 33 items (0 to 32) instead of 32 (1 to 32).

  • Understand perfectly, had already used the operator before. And very well observed in fact the is starts at 0.

Show 1 more comment

1 answer

0


Create three integer type vectors of size 12: vector 0[12], vector 1[12] and vector 2[12].

int i, vet0 = 0, vet1 = 0, vet2 = 0;
int vetor0[12], vetor1[12], vetor2[12];

for(i = 1; i <= 32; i++){
    // Para calcular o resto, usamos %.
        if((i % 3) == 0){
            vetor0[vet0] = i;
            vet0++;
        }
        if((i % 3) == 1){
            vetor1[vet1] = i;
            vet1++;
        }
        if((i % 3) == 2){
            vetor2[vet2] = i;
            vet2++;
        }
    }
}

To print everything, use:

int n;
for(n = 0; n < 12; n++){
    printf("%d ", vetor0[n]);
}
printf("\n");
for(n = 0; n < 12; n++){
    printf("%d ", vetor1[n]);
}
printf("\n");
for(n = 0; n < 12; n++){
    printf("%d ", vetor2[n]);
}
  • Sorry, there was an error in the code, try again now.

  • Now it works, the only thing that was missing was adding a conditional to the case of having fewer elements than the size of the vector, or more elements. But the structure itself is that, sorry for the mistake.

  • 1

    There is nothing wrong with making a mistake; it exists so that we can learn. The important thing is that you corrected the error. Another thing is that you can start the vectors with {0}, making vetor1[12] = {0}, to avoid memory junk. And it would be interesting to explain in words the solution, because what may be trivial for us may not be for all users.

Browser other questions tagged

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