Problem with if and &&

1

Good morning!

I'm doing an exercise that I found:

Write a program in C ++, that determines the energy efficiency of a Thermoelectric, considering its consumption in general in 10000 kW. If your intake is between 80% and 100% kW, display " MEDIUM CONSUMPTION ", otherwise it is above 100%, display " HIGH ENERGY CONSUMPTION ".

I also wanted to add a message if the consumption is less than 80 but the program is not working well for me.

If I put 80, instead of printing " AVERAGE CONSUMPTION " it prints " LOW CONSUMPTION " and I can not find how to correct this problem. I hope you can help me, please.

#include <iostream>

using namespace std;

int main() {
  float consumoGeneral = 0;
  float consumo = 0;

  cout << "\t\tEficiencia Energetica" << endl;
  cout << endl << endl;
  cout << "Para determinar la eficiencia energetica \ningrese el Consumo "
          "General (en kW): ";
  cin >> consumoGeneral;
  cout << endl;
  cout << "Ahora ingrese el consumo a comparar: ";
  cin >> consumo;
  cout << endl;

  if ((consumo >= 0.8 * consumoGeneral) && (consumo <= consumoGeneral)) {
    cout << "CONSUMO MEDIO";
  } else if (consumo < 0.8 * consumoGeneral) {
    cout << "CONSUMO BAJO" << endl;
  } else {
    cout << "ALTO CONSUMO DE ENERGIA";
  }

  cin.get();
  return 0;
}

Thanks in advance!

I closed the program and I reopened it and it keeps giving me the same: (

    
asked by Germán Diego Guisasola Plejo 25.06.2017 в 08:00
source

2 answers

1

or I did not understand well or your program does what it asks, maybe I should check that it is running a current build and not an old one I leave the output I get in an online ideo and I think it does what it asks.

  • General Consumption - > 100
  • consumption - > 80
        Eficiencia Energetica


Para determinar la eficiencia energetica 
ingrese el Consumo General (en kW): 100

Ahora ingrese el consumo a comparar: 80

CONSUMO MEDIO 
  • General Consumption - > 100
  • consumption - > 78
    Eficiencia Energetica


Para determinar la eficiencia energetica 
ingrese el Consumo General (en kW): 100

Ahora ingrese el consumo a comparar: 78

CONSUMO BAJO

update, I just set to use 80 for the tests, because it is one of the latest data cited in your question, and I get the doubt that maybe referred to 80% even though the result is similar because I simplify the zeros, I leave something that I think fits more to your question in terms of data:

  • General Consumption - > 10000
  • consumption - > 8000
        Eficiencia Energetica


Para determinar la eficiencia energetica 
ingrese el Consumo General (en kW): 10000

Ahora ingrese el consumo a comparar: 8000

CONSUMO MEDIO 

I leave this capture of the Internet, maybe it is easier to understand what I said:

    
answered by 25.06.2017 в 08:17
1

The problem lies in the type of variable used. People are used to make comparisons to use:

int valor;
std::cin >> valor;
if( valor < 10 )
  // ...

But with floating-point numbers you have to be more careful and the reason is that both float and double do not have full precision, but have a series of trash digits. In the case of float , only the first 6 digits are representative, while in the case of double we can trust the first 12.

That there are garbage digits implies that there are several binary representations that give us, effectively, the same number. Thus, for a type float , it would be understood that both numbers should be the same:

6 digitos | basura
   1,00000 01
   1,00000 09

And yet the comparison operator would tell us that the values are different. That is, when comparing variables in floating point we have to make relative comparisons. That is, we must establish a margin of error, so we understand that two variables are equal if that margin of error is not exceeded.

For example, to compare if the value of a float equals 10 we should do something like this:

float valor;
std::cin >> valor;
if( std::abs(valor - 10) < 1e-6 )
//                         ^^^^ Margen de error
{
  // ...
}

This feature of floating point coding also affects operators less than , greater than , less than or equal , greater or equal and different . Special attention must be paid to this aspect, since if the stored value is close enough (they differ only in junk digits) from the reference value, the result of the comparison may not be satisfactory. Think, for example, of the values that I have put before:

6 digitos | basura
   1,00000 01
   1,00000 09

An operation of greater or equal should result in true and, nevertheless, it will return false since in first digit it is, really, smaller than the second one. Likewise, the operator different != will return true .

All this said, the exercise should look like this

#include <iostream>

using namespace std;

int main() {
  float consumoGeneral = 0;
  float consumo = 0;
  float const error = 1e-5;

  cout << "\t\tEficiencia Energetica" << endl;
  cout << endl << endl;
  cout << "Para determinar la eficiencia energetica \ningrese el Consumo "
          "General (en kW): ";
  cin >> consumoGeneral;
  cout << endl;
  cout << "Ahora ingrese el consumo a comparar: ";
  cin >> consumo;
  cout << endl;

  if( consumoGeneral + error < consumo )
    std:cout << "ALTO CONSUMO DE ENERGIA";
  else if( consumoGeneral * 0.8 - error > consumo )
    std::cout << "CONSUMO MEDIO";
  else
    cout << "CONSUMO BAJO";

  cin.get();
  return 0;
}

In the first comparison, to consumoGeneral we add a margin of error. What we do is assume that consumo==consumoGeneral while the difference between both is less than error , to avoid uncertainty, we add the margin of error to consumption.

In the comparison of the average consumption something similar happens ... only now the margin of error remains. Now it is subtracted because the operator we replace is greater or equal , and it is precisely that equal that forces us to subtract the margin of error. What we do is include the (consumoGeneral-error,consumoGeneral) range in the valid set of values.

    
answered by 22.08.2018 в 08:03