major / minor in vectors

3

I made some modifications to my code, it presents itself in modifications at the end of the post and I will modify it when I feel it necessary.

Hello, I'm doing a college exercise, it's very simple, however, I'm having some difficulty in it, the exercise asks to write two vectors of 5 possibilities and print a resulting vector by ordering the elements in descending order (elements are from type int ), below the code:

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

int main()
{
    int vet1[5], vet2[5], rvet[10];
    int i, j, k, n1=0, n2=0;


    for(i=0; i<=4; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%i", &vet1[i]);
    }

    printf("Obrigado, agora:\n");

    for(j=0; j<=4; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet2:\n", n2++);
        scanf("%d", &vet2[j]);

    }

    i=0;
    j=0;
    for(k=0;k<=9;k++)//Ordena os vetores
    {
        rvet[k]=i>j ? vet1[i++] : vet2[j++];

            printf("vet[%d]:%d\n", k, rvet[k]);
    }

    return 0;
}

My biggest difficulty is in the for line 28 (or the 3rd for), I can not think of a better algorithm, so I posted an impression that aligns first vet2 and then vet1. I was thinking of doing smaller and bigger but when I modify the algorithm it always loses the data saved in vector 1 and 2 and there are other random numbers. if anyone can help me thanks ... my program has no errors and no warning I tested it in code :: blocks and no dev C ++

MODIFICATIONS:

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

int main()
{
    int vet1[5], vet2[5], rvet[10];
    int i, j, k, x, y, n1=0, n2=0, aux=0;


    for(i=0; i<=4; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%i", &vet1[i]);
    }


    fflush(stdin);
    system("cls");
    printf("Obrigado, agora:\n");

    for(j=0; j<=4; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet2:\n", n2++);
        scanf("%d", &vet2[j]);

    }

    fflush(stdin);
    system("cls");


    for(k=0; k<=4; k++)//preenche as 5 primeiras possições com vet1;
    {
            rvet[k]=vet1[i++];
    }

    for(k=5; k<=9; k++)
    {
        rvet[k]=vet2[j++];
    }   


    for(y=0; y<=9; y++)//coloca em ordem decrescente
    {
        for(k=x+1; k<=9; k++)
        {
            if(rvet[k]<rvet[x])
            {
                aux=rvet[x];
                rvet[x]=rvet[y];
                rvet[y]=aux;
            }
        }
    }

    for(k=0; k<=9; k++)
    {
        printf("%d->%d\n", k, rvet[k]);
    }

    return 0;
}

Here they first print vet1 and then vet2.

    
asked by anonymous 06.08.2014 / 19:07

2 answers

2

For this, I think the most efficient way would be to create a third vector with size 10, insert the values of the 2 previous vectors in the third, and then perform the ordering.

For this:

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

int main()
{
int vet1[5], vet2[5], rvet[10], n1=0, n2=0, aux=0;

for(int i=0; i<=4; i++) //Lê o vet1
{

    printf("Digite para posicao %d do vet1:\n", n1++);
    scanf("%i", &vet1[i]);
}

printf("Obrigado, agora:\n");

for(int j=0; j<=4; j++)//Lê vet2
{
    printf("Digite para posicao %d do vet2:\n", n2++);
    scanf("%d", &vet2[j]);

}

//intercalando os vetores aqui
for(int k=0; k<=4; k++)
{
rvet[k] = vet1[k]; //preenche as primeiras 5 posições
}

for(int k=5; k<=9; k++)
{
rvet[k] = vet2[k-5]; //preenche as próximas 5.
}

//Ordenação bubble sort
 for(int x = 0; x <= 9; x++ )
 {
   for(int y = x + 1; y <= 9; y++ ) // sempre 1 elemento à frente
    {
     // se o x for menor que a próxima posição do vetor, o x passa pra frente (ordem decrescente)
     if (rvet[x] < rvet[y] )
     {
      aux = rvet[x];
      rvet[x] = rvet[y];
      rvet[y] = aux;
     }
    }
  } // fim da ordenação
printf("*** VETOR ORDENADO ***\n");

for(int i=0; i<=9; i++)
{
  printf("%d\n", rvet[i]);
}

return 0;
}

Tip: When you use a variable just to go through for , declare it this way for(int var=0; var<=x; var++) . So, when the loop ends, the variable ceases to exist, that is, you do not need to assign 0 to it.

@ LeonardoV.DeGasperin in the last changes made I noticed the following errors in your code:

1 - The vectors vet1 and vet2 were filled in indexes: 0, 1, 2, 3 and 4 (5 indexes, remembering that 0 counts as well). You zeroed the variables iej, and then started to traverse the vector like this: vet1[i++] , remembering that if i starts with 0, i ++ will automatically be 1, ignoring index 0. In this way, you go through the indexes: 1, 2, 3 , 4 and 5, and in index 5 there is nothing. Look at the code I posted above in the part that intersects the vectors and you'll notice the difference.

2 - After you fill in the rvet[] vector do not just do an if to sort it. From here, forget the vectors vet1[] and vet2[] . To sort and display in descending order, it is necessary to go through the entire vector rvet[] and organize the numbers that are inside it, and something that assists in this task is an algorithm called sort algorithm . Without the use of this algorithm, it is difficult to sort. What I used in my code (and I believe it to be the simplest to understand) is the bubble sort , but then I recommend you to search for others too, it's interesting.

Here you can see what I used to do this ordering .

3 - Finally, to print the entire ordered vector at one time, it is necessary to create another for , as shown in the above code.

Any questions, do not hesitate to ask. Hugs!

    
06.08.2014 / 19:24
1

You did nothing with i and j on your last for and notice that even if you were what I thought you were thinking of doing, you'd be wrong:

v1 : {2,5,20}  v2: {44,34,23}
v1[i]>v2[j]  => vres = [v2[j++]] 

The problem is that as vectors v1 and v2 are not sorted, they will get unexpected results especially if the memory garbage value is still less than one of the values used.

Code hints:

Use #define so you do not have to write the values everywhere (it's much easier to change later).

Do the ordering after concatenating the two vectors.

#include <stdio.h>
#include <stdlib.h>
#define MAXVETOR 5
#define MAXRESULTADO MAXVETOR*2



void ordena(int* vetor , int tamanho){
int c,d,swap;
for (c = 0 ; c < ( tamanho - 1 ); c++)
  {
    for (d = c ; d < tamanho - c - 1; d++)
    {
      if (vetor[d] > vetor[d+1]) //Troque de > para < para alterar a forma de ordenação.
      {
        swap       = vetor[d];
        vetor[d]   = vetor[d+1];
        vetor[d+1] = swap;
      }
    }
  }
}
void imprime_vetor(int *vetor,int tamanho){
    int i=0;
    printf("[");
    for(;i<tamanho;i++){
        if(i==tamanho-1){
            printf("%d]",vetor[i]);
        }
        else{
            printf("%d,",vetor[i]);
        }
    }
}

int main()
{
    int vet1[MAXVETOR] = {1,4,28,34,2}, vet2[MAXVETOR]={99,23,6,3,7}, rvet[MAXRESULTADO];
    int i, j, k, n1=0, n2=0;

/*
    for(i=0; i<MAXVETOR; i++)//Lê o vet1
    {

        printf("Digite para posicao %d do vet1:\n", n1++);
        scanf("%i", &vet1[i]);
    }

    printf("Obrigado, agora:\n");

    for(j=0; j<MAXVETOR; j++)//Lê vet2
    {
        printf("Digite para posicao %d do vet2:\n", n2++);
        scanf("%d", &vet2[j]);

    }
*/
    i=0;
    j=0;
    for(k=0;k<MAXRESULTADO;k++)//Intercala os vetores
    {
        rvet[k] = vet1[k];
    }
    for(k=MAXVETOR;k<MAXRESULTADO;k++)//Intercala os vetores
    {
        rvet[k] = vet2[k-MAXVETOR];
    }
    ordena(rvet,MAXRESULTADO);
    imprime_vetor(rvet,MAXRESULTADO);
    return 0;
}
    
06.08.2014 / 19:34