3
I made some modifications to my code, it is in modifications at the end of the post I will modify it when I find it necessary.
hello, I am doing a college exercise, it is very simple, however, I am having some difficulty in it, the exercise asks to write two vectors of 5 possible and print a resulting vector ordering the elements in descending order (elements are of the 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 greatest difficulty is in for line 28(or the 3rd for), I can’t think of a better algorithm, so I posted an impression that aligns the vet2 first and then the vet1. I was thinking of making it smaller and bigger but when I modify the algorithm it always loses the data saved in vector 1 and 2 and then appears other random numbers. if anyone can help me thank you... my program has no errors and neither Warning tested in code::Locks and 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 print first the vet1 and then the vet2.
You want the rvet, get ordered downward?
– Diego Zanardo
that’s right diego
– Leonardo V. De Gasperin