Problem with Iterator Map C++

Asked

Viewed 141 times

0

the program for a URI exercise hangs (as in infinite loop) when the input (double) is different from an integer or decimal number that is not something with a middle (e.g.: 55.5). I have tried to modify the code in several ways, but apparently the problem is in the second iteration (it2), because without this piece of code it works with any decimal. The strange thing is that with the code the way it is, it hangs exactly after reading the double, at the beginning of the code.

The purpose of the exercise is to pass the amount reported on entry to the smallest possible number of banknotes and coins.

code:

#include <iostream>
#include <map>
#include <iomanip>

using namespace std;

int main(){

double val=0.0;
map<int,int> nota,moeda;
map<int,int>::reverse_iterator it,it2;

cin >> val;

if(val<0.00 || val>1000000.00)
    return 1;

nota[100]=0;
nota[50]=0;
nota[20]=0;
nota[10]=0;
nota[5]=0;
nota[2]=0;

moeda[100]=0;
moeda[50]=0;
moeda[25]=0;
moeda[10]=0;
moeda[5]=0;
moeda[1]=0;

it=nota.rbegin();

while(val>=2){
    while(val-it->first>=0){
        val-=it->first;
        nota[it->first]++;
    }           
    it++;
}   

it2=moeda.rbegin();

while(val>0){
    while(val-it2->first/100.00>=0){
        val-=it2->first/100.00;
        moeda[it2->first]++;
    }           
    it2++;
}

cout << "NOTAS:" << endl;
for(it=nota.rbegin();it!=nota.rend();it++)
    cout << it->second << " nota(s) de R$ " << fixed << setprecision(2) << it->first/1.0 << endl;
cout << "MOEDAS:" << endl;
for(it2=moeda.rbegin();it2!=moeda.rend();it2++)
    cout << it2->second << " moeda(s) de R$ " << fixed << setprecision(2) << it2->first/100.00 << endl;

return 0;
}
  • The problem arises from using float (binary floating point) to store the amount. I suggest reading http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html and either storing the amount in two variables separated by the comma or using a precision library (e.g., GMP).

  • Reading part of the material (because it is very extensive), I agree that it is very likely that the problem is the type of variable. Being a simple problem and just to exercise logical reasoning, I will try to implement using the standard library, perhaps separating the decimal part to integer. I had an idea of how the float was stored in memory, but had never thought or seen about the accuracy. Very good and detailed the material. Thank you

1 answer

0

You have not implemented a correct stop rule in the two while Ops. Try to include a rule that uses iterator’s, as these are the most important to be tested, since if they are not pointing to a valid position, an undetermined error situation may occur.
Include this test after each iterator increment, for each case:

if (it == nota.rend()) break;
if (it2 == moeda.rend()) break;
  • The stopping condition should be sufficient, in this case, because of logic, not causing the Iterators to leave for an invalid position. Thus, the break loop stop would not complete the entire task (counting banknotes and coins).

Browser other questions tagged

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