How to get the rest of a float number in C?

0

This question is the one of the URI I thought of this idea more when I will take the rest of float numbers I can not ...

#include <stdio.h>

int main(int dinheiro)
{
    int res, res2;

Here I have an array of integers that I think should be float to put the values higher.

int notas[] = {100, 50, 20, 10, 5 ,2};
float moeda, moedas[] = {1.0, 0.50, 0.25, 0.10, 0.05, 0.01};
scanf("%i", &dinheiro);
printf("NOTAS:\n");
for(int i=0;i<=5; i++)
{
    res = dinheiro / notas[i];
    printf("%d nota(s) de R$ %d.00\n", res, notas[i]);
    dinheiro %= notas[i];
}

So far the program runs and counts and separates notes.

My problem is when it comes to the coins ...

printf("MOEDAS:\n");

moeda = dinheiro;

Here's the problem, get the rest of the float numbers, both here and in the notes part.

    for(int j=0;j<=6; j++)
    {
        res2 = moeda / moedas[j];
        printf("%i moedas(s) de R$ %.2f\n", res2, moedas[j]);
        moeda = moeda/2;
    }
    return 0;
}

If someone has a tip, thank you ...

    
asked by anonymous 26.11.2017 / 03:57

2 answers

2

You want to separate an X amount of money with the fewest possible banknotes and coins.

As noted, my previous response had problems since float is inaccurate . I have refiled the code using only integers, to avoid these problems.

As everything is done using integers only I represent all the notes and coins in cents instead of using real ones. The process of reading the money I separate in two, what is left of the point (whole part) and what is to the right (decimal part). Then put everything together in the dinheiro variable.

#include <stdio.h>

int main(){
        int notas[6]={10000,5000,2000,1000,500,200};
        int moedas[6]={100,50,25,10,5,1};
        int inteiro;
        int decimal;
        int dinheiro;

        int n;

        int i;

        scanf("%d.%d", &inteiro, &decimal);

        if(decimal>99){
                printf("Erro, decimal com mais de 2 digitos.\n");
                return -1;
        }

        dinheiro=inteiro*100+decimal;

        printf("Notas:\n");
        for(i=0;i<6;i++){
                n=dinheiro/notas[i];
                dinheiro-=n*notas[i];
                printf("%d nota(s) de R$ %.2f\n", n,notas[i]/100.);
        }

        printf("Moedas:\n");
        for(i=0;i<6;i++){
                n=dinheiro/moedas[i];
                dinheiro-=n*moedas[i];
                printf("%d moeda(s) de R$ %.2f\n", n,moedas[i]/100.);
        }
        return 0;
}
    
26.11.2017 / 20:01
0

Could you please explain the question to me? I ran the code like this and it seems ok.

#include <stdio.h>

int main(int dinheiro)
{
    int res, res2;
    int notas[] = {100, 50, 20, 10, 5 ,2};
    float moeda, moedas[] = {1.0, 0.50, 0.25, 0.10, 0.05, 0.01};
    scanf("%i", &dinheiro); 
    printf("\n\n\nNOTAS:\n");
    for(int i=0;i<=5; i++)
    {
        res = dinheiro / notas[i];
        printf("%d nota(s) de R$ %d.00\n", res, notas[i]); 

    }
     printf("MOEDAS:\n");


    moeda = dinheiro;


     for(int j=0;j<=5; j++)
    {

       res2 = moeda / moedas[j];

      printf("%i moedas(s) de R$ %.2f\n", res2, moedas[j]);

    }
    return 0;
}
    
26.11.2017 / 05:21