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.