5% URI error (in C language)

Asked

Viewed 70 times

-1

The code gives error in cases such as 576.73 or 576.43, in the coin field of 0.01 the other field answer correctly. I don’t know exactly the reason to return 2 coins of 0.01 in the above cases, but when the input is 0.03, 0.04 or 0.06 among other attempts taken from the site uDebug the result is correct.

The description of the problem is:

Read a floating point value to two decimal places. This value represents a monetary value. Then calculate the fewest possible banknotes and coins in which the value can be broken down. The banknotes considered are 100, 50, 20, 10, 5, 2. The possible currencies are 1, 0.50, 0.25, 0.10, 0.05 and 0.01. The following shows the list of necessary notes. Entree

The input file contains a floating point value N (0 N 1000000.00). Exit

Print the minimum amount of banknotes and coins needed to exchange the initial amount, as per example provided.

Note: Use dot (.) to separate the decimal part.

Expected exit to entry 576.73

NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01

My code in C:

#include <stdio.h>
#include <stdlib.h>

int main(){
float valor;
int div;

scanf("%f", &valor);
///// NOTAS
printf("NOTAS:\n");
div = (valor/100);
valor = valor - (div*100);
printf("%d nota(s) de R$ 100.00\n", div);
div = (valor/50);
valor = valor - (div*50);
printf("%d nota(s) de R$ 50.00\n", div);
div = (valor/20);
valor = valor - (div*20);
printf("%d nota(s) de R$ 20.00\n", div);
div = (valor/10);
valor = valor - (div*10);
printf("%d nota(s) de R$ 10.00\n", div);
div = (valor/5);
valor = valor - (div*5);
printf("%d nota(s) de R$ 5.00\n", div);
div = (valor/2);
valor = valor - (div*2);
printf("%d nota(s) de R$ 2.00\n", div);
///// MOEDAS
printf("MOEDAS:\n");
valor *= 100;
div = (valor/100);
valor = valor - (div*100);
printf("%d moeda(s) de R$ 1.00\n", div);
div = (valor/50);
valor = valor - (div*50);
printf("%d moeda(s) de R$ 0.50\n", div);
div = (valor/25);
valor = valor - (div*25);
printf("%d moeda(s) de R$ 0.25\n", div);
div = (valor/10);
valor = valor - (div*10);
printf("%d moeda(s) de R$ 0.10\n", div);
div = (valor/5);
valor = valor - (div*5);
printf("%d moeda(s) de R$ 0.05\n", div);
div = valor / 1;
printf("%d moeda(s) de R$ 0.01\n", div);

return 0;
}

My output (for input: 576.73)

NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
2 moeda(s) de R$ 0.01

Thank you in advance.

  • Try to use the value double instead of float.

  • It worked, even Danizavtz.

  • Dude, I don’t know the search terms, but it is related to the representation of binary numbers in floating point (standard IEEE754 for floating point arithmetic), periodic decimation in binaries and accumulation of errors in multiplication and division, when using floating point.

  • To avoid the lack of precision in the float/double representations you can multiply the values by 100 and work with integers and use the rest operator of the division (%).

1 answer

0


The limit for money in the statement is one million so it fits in one int. More importantly, a hundred million fits into one int. Then multiply all values by 100 and use int and everything is right.

Do not use float or even double to treat money or goes earlier --- type now --- or later have rounding errors because the representation of float is not exact.

For that reason COBOL has always been the language of money and financial reporting: money arithmetic without rounding errors, using BCD, two decimal digits per byte.

Browser other questions tagged

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