How to intersperse the values of two rows in C, in a third row?

Asked

Viewed 359 times

0

I am trying to intermediate the values of two rows in C, F1 and F2, in a third row, inter, as function below (n would be the amount of F1 and F2 elements):

Fila *intercala_fila (Fila *f1, Fila *f2){
   int Tam, i;
   Fila *Inter = fila_cria();

   Tam = f1->n + f2->n;

   for(i=0;i<Tam;i++){
      fila_insere(Inter, f1->vet[i]);
      fila_insere(Inter, f2->vet[i]);
   }

   return Inter; }

I am successfully interlink, however, I am getting memory junk in the result, as below:

10 //valor de f1
3 //valor de f2
5 //valor de f1
8 //valor de f2
2 //valor de f1
//abaixo são printados os lixos de memória
1297903728
1275095411
1464299113
1279345487
1650209846

How can I get a result where I can intermediate the values of both queues without getting memory values, or else without omitting F1 or F2 values?

Thank you!

  • But what is the definition of your Fila? At first it does not seem correct to add up the amount of elements of the two rows and try to access such quantity in each of them.

  • Your for loop goes to Tam (which is the sum of the size of the two rows), but the F1 and F2 rows are smaller than Tam

2 answers

1

Tam shall not be the sum of f1->n and f2->n, but equal to the number of elements in the largest row (maximo(f1->n, f2->n)). (Then you’d better change the name of this variable Tam since it does not represent the final length of the intercalated list.)

And you should only enter if you still have items on the list to be entered, so you have to check that the counter i is less than the number of items on the list before inserting that item in the intercalated list.

Fila *intercala_fila (Fila *f1, Fila *f2){
    int Tam, i;
    Fila *Inter = fila_cria();

    //Tam eh igual ao numero de elementos da maior fila:
    Tam = f1->n > f2->n ? f1->n : f2->n;

    for(i=0;i<Tam;i++){
        //Deve inserir somente se ainda tem items na fila:
        if(i < f1->n) fila_insere(Inter, f1->vet[i]);
        if(i < f2->n) fila_insere(Inter, f2->vet[i]);
    }

    return Inter;
}

0

I believe these memory dumps in line inter come from the queues f1 and f2. When doing some tests, I arrived at a solution half bad, but at first it generates a different result. What was done was to modify the value n queued inter for the final value of the meter i, with this, when calling a function to show the queue inter, she wouldn’t pick up and show these rubbish, but remembering that they still exist there.

Fila *intercala_fila (Fila *f1, Fila *f2){
    Fila *inter=fila_cria();//cria fila inter
    int tamanho=f1->n+f2->n;//soma o tamanho das filas f1 e f2 a partir do valor de seu fim
    int i;
    for(i=0;i<=tamanho;i++){//for que roda até o tamanho da soma de f1 e f2
        fila_insere(inter, f1->vet[i]);//insere valores de f1 e f2 intercaladamente
        fila_insere(inter, f2->vet[i]);
    }
    inter->n=i-1;//determina que o fim da fila inter é igual ao valor final do contador
                 //para que assim não sejam impressos lixos de memória presentes na  fila inter
    return inter;//retorna fila inter
}

Browser other questions tagged

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