Resolve operation with Set

-2
  

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

     
  • Create three integer vectors of size 12: vector0 [12], vector1 [12] and vector2 [12].
  •   
  • Store in the vector0 the elements pertaining to X that leave rest 0 when divided by 3.
  •   
  • Store in the vector1 the elements pertaining to X that leave rest 1 when divided by 3.
  •   
  • Store in the vector2 the elements pertaining to X that leave rest 2 when divided by 3.
  •   
  • Print the elements contained in vector0.
  •   
  • Print the elements contained in vector1.
  •   
  • Print the elements contained in vector2.
  •   

    Can anyone help me with this code?

    This afternoon a friend asked me for help in resolving an issue in college.  I like to program, but I have already finished high school for many years and I do not remember how to solve sets anymore. The only thing I remember is for my teacher to write vectors on the blackboard or draw polka dots to see if there was an intersection between two or more sets.

        
    asked by anonymous 24.05.2018 / 23:50

    1 answer

    0
      

    Create three integer vectors of size 12: vector0 [12],   vector1 [12] and vector2 [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]);
    }
    
        
    25.05.2018 / 00:07