Problems with 2 vectors joining

Asked

Viewed 109 times

2

I have a problem joining two sets of integer numbers into one vector, without repeating numbers that are in Set A and Set B. The following error is shown in row 13:

error: Unknown type name ?bool'; Did you Mean ?_Bool'?|*

Exchanging bool for Bool the code is not compiled, it is 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; 

 }

1 answer

3


The main problem is that the header that lets you use the 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);
 }

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Some additional things I’ve done:

  • I removed the header I wasn’t using
  • I standardized variable names
  • I declared the variables near where they are used and in the smallest possible scope
  • I killed unnecessary variable
  • I killed unnecessary function statement
  • I killed a repeated logic
  • I better organized the code to shorten it giving more readability (increasing where needed)
  • And I organized to put the responsibility of everything in the right place
  • I killed comments, if they are necessary it is because the code is misspelled
  • I improved the aesthetic presentation
  • I changed the data formatting to %d which is more suitable for almost all cases.

Which I didn’t, but I should have:

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

And I didn’t change the guy to _Bool that would also work.

Browser other questions tagged

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