Exercise in C, printing of change

Asked

Viewed 903 times

-1

Consider four coins, 25 cents, 10, 5 and 1. Build a C program that asks the user how much he wants to receive in change and then print out the amount of coins needed to pay the change, always delivering as few coins as possible. The program must have a loop that forces the user to put a positive value.

source:

#include <cc50.h>
#include <stdio.h>

int main (void)
{

int a = 25;
int b = 10;
int c = 5;
int d = 1;
float f = 0;

do
 printf("Quanto de troco deseja receber? ");
 f = GetFloat();
while ( f < 0 );

if ( f == 0 )
 printf("Obrigado volte sempre!\n");

if ( f > 0 )
 printf("Este troco exige %.2f moedas.\n", f);
}

2 answers

1


This problem is pretty classic college.

Basically what you have to do is get the change value, and go down by the value of the available currencies, from the highest value to the lowest value currency while counting the currencies.

follows a possible resolution to the problem

#include <stdio.h>

int main (void)
{

    float a = 0.25;
    float b = 0.10;
    float c = 0.05;
    float d = 0.01;
    float f = 0;

    int moedas;
    do{
        printf("Quanto de troco deseja receber? ");
        scanf("%f",&f);
    }while(f < 0);

    while(f >= a) {
        f -= a;
        moedas++;
    }

    while(f >= b) {
        f -= b;
        moedas++;
    }

    while(f >= c) {
        f -= c;
        moedas++;
    }

    while(f >= d) {
        f -= d;
        moedas++;
    }

    printf("Este troco exige %d moedas.\nObrigado volte sempre!\n", moedas);

}

One more question, in C, can’t you change the value of a variable? I tried using the while loop but did not compile, I used an error saying I could not change the value of the variable f,

In the case by the syntax, you are declaring again the same variable f, hence the error.

float f = GetFloat();

Also , like @Matheus I recommend using scanf instead of GetFloat(), the code snippet would be as follows:

scanf("%f",&f);
  • Thank you so much for the syntax tip, I didn’t know. About the function, I’m obliged to do with Getfloat(), it’s a course exercise.

  • No problems, I just couldn’t test because I don’t have the cc50.h file that should contain this function, so I used scanf which is available in C.

0

In this part of the code:

do
   printf("Quanto de troco deseja receber? ");
   float f = GetFloat();
while ( f < 0 );

You forgot your keys, so you’re making a mistake. Do:

do{
   printf("Quanto de troco deseja receber? ");
   float f = GetFloat();
}while ( f < 0 );
  • I made several modifications to the code, including this, and it doesn’t work, it’s as if the global value of f didn’t change with the keys in do while.

  • Take the f float inside the while and try again.

  • Xara, please use the scanf function instead of Getfloat(),another thing like f is already stated above Oce does not need to declare it again, in your case it would be f = Getfloat(); and not float f = Getfloat();

Browser other questions tagged

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