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

Asked

Viewed 2,255 times

0

This is the question of the URI I thought of this idea more when I’m going to take the rest of float numbers I can’t...

#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 meores.

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 does the counting and separation of the notes.

My problem is when it comes to coins...

printf("MOEDAS:\n");

moeda = dinheiro;

Here is the problem, take the rest of 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 anyone has a tip I’d appreciate it...

  • Here’s an example of how to take the fractional part of the variable. #include <stdio.h>&#xA;&#xA;int main(){&#xA;&#xA; int i = 0;&#xA; float j = 3.55;&#xA; i = j; // recebe a parte inteira&#xA; j = j - i; //retira a parte inteira e sobra a parte fracionaria&#xA; printf("%d", i);&#xA; printf("\n%f", j);&#xA; return 0;&#xA;}

  • But the exercise is to separate the money into notes and coins ? Or is it to say what a quantity of money looked like, only in denominations of 100, only in denominations of 50, etc.. ?

  • separate the money...

2 answers

2

You want to separate an X amount of money with as few notes and coins as possible.

As well noted, my previous answer had problems since the float is inaccurate. I redid the code using only integer numbers 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 the left of the point (whole part) and what is the right (decimal part). Then I put it all together in the variable dinheiro.

#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;
}
  • That’s it, but your code gets fuzzy when it comes to the 0.01 cent coins. always cash one...

  • It is true @Wesleyalvescardoso, the floating point was generating some problems. I refiz everything without using float, see if it’s correct.

0

Could you elaborate on the question? I ran the code like this and it seems okay.

#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;
}
  • I understood the point of view, more according to which it separates the banknotes it cashes the separate value: 549.78 5 100 2 20 1 5 2 2 1 0.50 2 2 0.10 1 0.05 3 0.01 coins

Browser other questions tagged

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