How to get the highest and lowest number among 15 typed numbers? C++

Asked

Viewed 744 times

3

An algorithm that reads 15 real numbers, and writes the largest and the smallest among the numbers read. I’m not getting to do the part of writing the smallest and largest of the numbers typed.

Code:

#include <iostream>
using namespace std;
int main(){
    float numero;
    int cont;
    cont=1;
    while(cont<=15) {
       cout << ("Digite um numero real:");
       cin >> numero;
       cont++;
    }
}

How to get the highest and lowest number among the typed numbers?

  • 2

    Define the variables maior and menor, after reading a do: if (cont == 1) menor = maior = num; else {if (num > maior) maior = num; if (num < menor) menor = num;}. After the loop print the variables maior and menor.

  • @anonymity would not be better to use the std::set or a vector with the algorithm?

  • 1

    I was following the line that diogoBRRN was following in the development and he was not using such Features and there is possible solution without using them..

4 answers

2

You can enter the input data into a std::set, which is an ordered container, and then just take the first element (smaller) and the last element (larger).

#include <iostream>
#include <set>

int main( [[maybe_unused]] int argc, [[maybe_unused]] char ** argv )
{
  // a inicialização é para simular a entrada de dados
  std::set<float> numbers = { 100.1, 23.3, 84.3, -1.4, 1234.5, /* ... */ 40.22 };

  std::cout << "Min:" << *numbers.begin() << std::endl;
  std::cout << "Max:" << *numbers.rbegin() << std::endl;

  return 0;
}
  • Short and simple way, I’ll follow that answer :)

1

Define two control variables of type float, being them maior and menor. After reading the attribute numero make the following validation:

//Verifica se é o primeiro número digitado e define o valor das variáveis maior e menor igual ao número.
if (cont == 1) {
    maior = numero;
    menor = maior;
}
else if (numero > maior) //Verifica se o número digitado é maior do que o maior dos números digitados.
    maior = numero;
else if (numero < menor) //Verifica se o número digitado é menor do que o menor dos números digitados.
    menor = numero;

Modified code:

#include <iostream>
using namespace std;
int main() {
    float numero, maior, menor;
    int cont = 1;
    while (cont <= 15) {
       cout << ("Digite um numero real:");
       cin >> numero;
       if (cont == 1) {
           maior = numero;
           menor = maior;
       }
       else if (numero > maior) {
           maior = numero;
       }
       else if (numero < menor) {
           menor = numero;
       }
       cont++;
    }
    cout << "Menor numero: " << menor << "\nMaior numero: " << maior << endl;
}

1


One way I consider very good is to use vector and the algorithm, they already have a function to see the lowest value and the highest, so it will not be necessary to use any if.

Code:

#include <iostream>
#include <vector>
#include<algorithm> 

using namespace std;
int main(){
    float numero;
    vector<float> num;
    int cont, maximo, minimo;
    cont=1;
    while(cont<=15) {
       cout << ("Digite um numero real:");
       cin >> numero;
       num.push_back(numero);
       cont++;
    }

    cout << "Maior valor digitado: " << *max_element(num.begin(), num.end()) << endl;
    cout << "Menor valor digitado: " << *min_element(num.begin(), num.end()) << endl;

}

See working on Repl.it

0

Read a number, consider this number to be both the largest and the smallest so far.

Read the other 14 and compare with the largest and smallest so far.

#include <iostream>
using namespace std;
int main(){
   float numero, maior, menor;
   int cont;
   cont=1;
   cout << ("Digite um numero real:");
   cin >> numero;
   maior = menor = numero
   while(cont< 15) {
       // aqui você coloca o código pra atualizar as variáveis maior e menor
       cont++;
}

}

  • But it’s just this comparison that he doesn’t know

Browser other questions tagged

You are not signed in. Login or sign up in order to post.