Problems with union of 2 vectors

2

I have a problem to join two sets of integers into a vector, without repeating numbers that are in Set A and Set B. The following error is displayed in line 13:

  

error: unknown type name 'bool'; did you mean '_Bool'? |

Changing bool to Bool the code is not compiled, is it some syntax error?

Code:

#include <stdio.h>
#include <stdlib.h>

void PrintVetor(int Conjunto[], int Tamanho){
    int i;
    for(i = 0; i < Tamanho; i++)
    {
        printf(" %i ", Conjunto[i]);
    } 
}


bool Existe(int x; int Uniao[], int k){ //Onde o erro se encontra
    int i;
    for(i=0; i < k; i++)
    {
        if(Uniao[i] == x)
            return true;
    }
    return false; 
}


int main() {
    void PrintVetor(int Conjunto[], int Tamanho);
    bool Existe(int x; int Uniao[], int k);
    int Tam, i;
    int k=0; // Variavel que controla o tamanho do Vetor Uniao a cada vez que um novo termo é adicionado

    //Definindo o tamanho do conjunto
    printf("Qual o tamanho dos conjuntos?\n");
    scanf("%i", &Tam);
    int ConjA[Tam];
    int ConjB[Tam];
    int TamUniao = (Tam*2);
    int Uniao[TamUniao];

    //Alimentando o Conjunto A
    //Adicionando Conjunto A aoConjunto Uniao
    printf("Preencha o Conjunto A\n");
    for (i = 0; i < Tam; i++)
    {
        scanf("%i", &ConjA[i]);
        Uniao[i] = ConjA[i];
    }
    system("clear"); //Caso Seja Linux

    //Alimentando o Conjunto B
    printf("Preencha o Conjunto B\n");
    for (i = 0; i < Tam; i++)
    {
        scanf("%i", &ConjB[i]);
    }
    system("clear"); //Caso Seja Linux

    //#########################
    //Print dos Conjuntos
    printf("Conjunto A:[");
    PrintVetor(ConjA, Tam);
    printf("] \n\n");
    printf("Conjunto B:[");
    PrintVetor(ConjB, Tam);
    printf("] \n\n");

    //UNiao
    for(i=0; i<Tam; i++)
    {
        Uniao[i] = ConjA[i];
        k++;
    }

    for(i=0; i<Tam; i++)
    {
        if(!Existe(ConjB[i], Uniao, k))
        {
            Uniao[k++] = ConjB[i];
        }
    }

    printf("Conjunto Uniao:[");
    for(i=0; i < k; i++)
    {
        printf("%i ", Uniao[i]);
    }
    printf("] \n\n");


    return 0; 

 }
    
asked by anonymous 16.11.2018 / 07:19

1 answer

3

The main problem is missing the header that allows you to use type bool :

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

void PrintVetor(int conjunto[], int tamanho) {
    printf("[");
    for (int i = 0; i < tamanho; i++) printf(" %d", conjunto[i]);
    printf(" ]\n");
}

bool Existe(int x, int uniao[], int tamanho) {
    for (int i = 0; i < tamanho; i++) if (uniao[i] == x) return true;
    return false; 
}

int main() {
    int tamanho;
    printf("Qual o tamanho dos conjuntos?\n");
    scanf("%i", &tamanho);
    int conjA[tamanho];
    int conjB[tamanho];
    int uniao[tamanho * 2];
    printf("Preencha o Conjunto A\n");
    for (int i = 0; i < tamanho; i++) {
        scanf("%d", &conjA[i]);
        uniao[i] = conjA[i];
    }
    printf("Preencha o Conjunto B\n");
    for (int i = 0; i < tamanho; i++) scanf("%d", &conjB[i]);
    printf("Conjunto A: ");
    PrintVetor(conjA, tamanho);
    printf("Conjunto B: ");
    PrintVetor(conjB, tamanho);
    for (int i = 0, k = tamanho; i < tamanho; i++, k++) if (!Existe(conjB[i], uniao, k)) uniao[k] = conjB[i];
    printf("Conjunto Uniao: ");
    PrintVetor(uniao, tamanho * 2);
 }

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

Some additional things I did:

  • I removed the header that I was not using
  • I have standardized variable names
  • I declare the variables near where they are used and in the smallest possible scope
  • I have matched unnecessary variable
  • I killed unnecessary function declaration
  • I killed a repeated logic
  • I better organized the code to shorten giving more readability (increasing where necessary)
  • And I arranged to put the responsibility of each thing in the right place
  • I have read comments, if they are needed, it is because the code is badly written
  • Improved aesthetic presentation
  • I have changed the formatting of data to %d which is most suitable for almost all cases.

What I did not do, but should have done:

  • Validate data entry
  • Separate parts into functions up to maintain consistency, or kill auxiliary functions since other parts that are clearly separate actions, but are in main()
  • Larger optimizations that go against what appears to be the purpose of the exercise
  • Other little details that will change little

And I did not change the type to _Bool that would work as well.

    
16.11.2018 / 08:05