1
I am trying to make a code to show 3 random numbers in ascending order typed by the user however I do not understand why my code is not working
#include <stdio.h>
main(){
int numeros[3],a,b,c,maior=0,menor=0,medio=0;
printf("Digite 3 numeros:\n");
scanf("%d %d %d",&numeros[0],&numeros[1],&numeros[2]);
for(a=0;a<3;a++){
if(numeros[a]>numeros[a+1]){
numeros[a]=maior;
}
}
for(b=0;b<3;b++){
if(numeros[b]<numeros[b+1]){
numeros[b]=menor;
}
}
for(c=0;c<3;c++){
if(numeros[c]<maior && numeros[c]>menor){
numeros[c]=medio;
}
}
printf("%d %d %d",menor,medio,maior);
output appears only 0 in all 3 positions.
If your intention is just to put in the variables
maior
,medio
andmenor
, I believe you reversed it. You seem to be placing the value of the largest variable, which was not initialized, in the number line [a]=higher. Try switching to larger = numbers[a].– Artur Trapp
It still doesn’t work anyway
– Daniel Duplat
If you wish, I wrote an answer to order 8 numbers without using vectors or loops. I also give an idea of why it was done that way and some theoretical basis in the subject: https://answall.com/a/240531/64969
– Jefferson Quesado