0
I need to make two comparisons as the person enters data:
- Higher height; OK
- Shorter height; (turned bad)
I cannot use vector in this case, because it is a requirement of exercise. In this case I was able to make the comparisons but it was very 'pig' because I had to start the variable with an absurd value, someone would have another way to solve?
Using Beginner Things etc.
/**
* 3) Faça um programa que leia dez conjuntos de dois valores:
* o primeiro representa o número do aluno e, o segundo, a sua altura em centímetros.
* Encontre o aluno mais alto e o mais baixo. Mostre o número do aluno mais alto e o
* número do mais baixo, junto com suas alturas.
* */
int n_aluno, id=1, n_guarda_maior = 0, n_guarda_menor = 0;
double altura_cm, maior = 0, menor = 999999999999999;
string aux;
while(id <= 4)
{
Console.WriteLine("Número do aluno: ");
aux = Console.ReadLine();
n_aluno = Convert.ToInt16(aux);
Console.WriteLine("Altura em cm: ");
aux = Console.ReadLine();
altura_cm = Convert.ToDouble(aux);
if(altura_cm > maior)
{
maior = altura_cm; //Armazena o maior
n_guarda_maior = n_aluno; //Guarda o número do maior
}
if(altura_cm < menor)
{
menor = altura_cm; //Armazena o menor
n_guarda_menor = n_aluno; //Guarda o número do menor
}
id++; //Proximo aluno
}
Console.WriteLine("ID do maior aluno: " + n_guarda_maior + " Altura: " + maior);
Console.WriteLine("ID do menor aluno: " + n_guarda_menor + " Altura: " + menor);
Console.ReadKey();
But then you’re not comparing the height of the students the ID only serves to control how many students I will register
– WSS
My problem is there: minor = 999999999999 (strange this)
– WSS
@WSS How so, young man? It’s obvious you’re comparing the heights yet. I know that the
id
only serves to control the quantity. Note that it is only a comparison (well logic by the way). If it is the first loop, there need not be any height comparison because it is impossible to have a value smaller than what was typed at this time.– Jéf Bueno
Ahh I understood what you did, put the id == 1 because if I would never give to compare the altura_cm < lower (in case 0).
– WSS