I am trying to write a program to calculate change, but it doesn't seem to work.
I think that the problem is the owed 1/ paid 1; when I tried to print there values I got nothing (0).
Any help ?
#include <stdio.h>
int main()
{
double owed, paid;
int dollars, quarters, dimes, nickels, cents, remainder, owed1, paid1;
printf("how much did the customer have to pay ?\n");
scanf("%f",&owed);
printf("how much did the customer pay ?\n");
scanf("%f",&paid);
owed1 = owed * 100;
paid1 = paid * 100;
int change = paid1 - owed1;
dollars = change / 100;
remainder = change % 100;
quarters = remainder / 25;
remainder = remainder % 25;
dimes = remainder / 10;
remainder = remainder % 10;
nickels = remainder / 5;
remainder = remainder % 5;
cents = remainder;
printf("%d",dollars);
printf("Dollars:%d, Quarters:%d, Dimes:%d, Nickels:%d, Cents:%d", dollars , quarters , dimes , nickels , cents );
return 0;
}
You're using %f in your scanf, which is the format specifier for a float, but your variables are doubles. You should use %lf instead:
scanf("%lf",&owed);
Same thing for paid. You should be getting warning from your compiler about that.
Related
I am not quite sure how to fix the program to make it display the change someone has correctly. I will list the expected outputs and the actual outputs below, as well as the code that I have written thus far. Any help is greatly appreciated, as I am very unsure about how to go about completing this project.
Expected output for input "4.67":
Dollar Bills: 4
Quarters: 2
Dime: 1
Nickel: 1
Pennies: 2
Actual output:
Dollar Bills: 223187040
Cents Leftover
Quarters: 0
Dimes: 0
Nickels: 0
Pennies: 0
#include <stdio.h>
double user_change(double);
void user_cents(int cents);
int main() {
double user_amt, cents;
printf("Hello User!\n\nHow much money would you like change for?\nEnter any amount: $");
scanf("%lf", &user_amt);
user_change(user_amt);
user_cents(cents);
return 0;
}
double user_change (double user_amt)
{
int updated_amt = (int) user_amt;
int hundreds, fifties, tens, fives, dollar_bills;
double cents = user_amt - updated_amt;
hundreds = updated_amt / 100;
updated_amt %= 100;
fifties = updated_amt / 50;
updated_amt %= 50;
tens = updated_amt / 10;
updated_amt %= 10;
fives = updated_amt / 5;
updated_amt %= 5;
dollar_bills = updated_amt / 100;
updated_amt %= 100;
printf("\nDollar Bills: %d\n", dollar_bills);
}
void user_cents(int cents)
{
int balance = cents * 10;
int quarters, dimes, nickels, pennies;
quarters = balance / 25;
balance %= 25;
dimes = balance / 10;
balance %= 10;
nickels = balance / 5;
balance %= 5;
pennies = balance / 1;
balance %= 1;
printf("\nCents Leftover\nQuarters: %d\nDimes: %d\nNickels: %d\nPennies: %d\n", quarters, dimes, nickels, pennies);
}
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I am declaring the amount as a float. When I multiply the value by 100 and plug it into an "int" variable the value changes to 419... why is this happening?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
float amount;
int cents, coins, quarters, dimes, nickels, pennies, Q, D, N, P;
int main(void){
do
{
amount = get_float("How much we talkin?\n");
}
while (amount < 0);
printf("cents = %.55f\n", amount);
cents = amount * 100;
printf("cents = %i\n", cents);
quarters = cents % 25;
Q = cents / 25;
printf("quarters = %i\n", quarters);
dimes = quarters % 10;
D = quarters / 10;
printf("dimes = %i\n", dimes);
nickels = dimes % 5;
N = dimes / 5;
printf("nickels = %i\n", nickels);
pennies = nickels % 1;
P = nickels / 1;
printf("pennies = %i\n", pennies);
coins = Q+D+N+P;
printf("%i\n", coins);
}
~/workspace/pset1/cash/ $ ./cash
How much we talkin?
4.2
cents = 4.1999998092651367187500000000000000000000000000000000000
cents = 419
quarters = 19
dimes = 9
nickels = 4
pennies = 0
22
... that link helped. Thank you!
Thank you, I resolved this issue by taking the input from the user as a float, multiplying that value by 100, then rounding to the nearest integer. The code below works, not the cleanest solution in the world.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
float amount;
int cents, coins, quarters, dimes, nickels, pennies, Q, D, N, P;
int main(void){
do
{
amount = get_float("How much we talkin?\n");
}
while (amount < 0);
// Print float input to see value is not precise
printf("amount = %.55f\n", amount);
// Multiply amount by 100, then round to nearest
cents = roundf(amount * 100);
printf("cents = %i\n", cents);
// *Quarters*
quarters = cents % 25;
Q = cents / 25;
printf("quarters = %i\n", Q);
// *Dimes*
dimes = quarters % 10;
D = quarters / 10;
printf("dimes = %i\n", D);
// *Nickels*
nickels = dimes % 5;
N = dimes / 5;
printf("nickels = %i\n", N);
// *Pennies*
pennies = nickels % 1;
P = nickels / 1;
printf("pennies = %i\n", P);
// Add up all the coins
coins = Q+D+N+P;
printf("%i\n", coins);
}
~/workspace/pset1/cash/ $ ./cash
How much we talkin?
4.2
amount = 4.1999998092651367187500000000000000000000000000000000000
cents = 420
quarters = 16
dimes = 2
nickels = 0
pennies = 0
18
To understand this issue, you need to understand how floating point numbers are stored in memory, in their binary form.
You also need to understand that C's type conversion TRUNCATES a float when converting to an integer.
The number 4.2 is stored as:
1 x 4
0 x 2
0 x 1
0 x 0.5
0 x 0.25
1 x 0.125
1 x 0.0625
0 x 0.03125
0 x 0.015625
1 x 0.0078125
1 x 0.00390625
etc
Which sums to 4.199 (and eventually approximates to 4.199999 - but NEVER 4.2)
Multiplying by 100 gives you 419.999(etc) - and truncating this gives you 419 not 420.
The solution (as you have found) is to make sure you ROUND rather than TRUNC
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
Hi so I'm struggling to figure out what's wrong with my code. I'm really new at this so bear with me. Comments might be a little messy atm.
#include <stdio.h>
int main(void)
{
double cost, gstCost, newCost; // initial cost, cost of gst by itself, new cost after gst
int loonies, quarters, dimes, nickels, pennies; // used for quantity of coins
float loonreq, quartreq, dimereq, nickreq, penreq; // cost required after deduction of the corresponding coin
printf("Please enter the amount to be paid: $");
scanf("%lf", &cost); // scanf allows to input an amount as a double ("%lf")
gstCost = cost * .13 + .005; // .13 for gst and add 0.005 to round up or down
printf("GST: %.2lf\n", gstCost);
newCost = cost + gstCost;
printf("Balance owing: $%.2lf\n", newCost); // original cost + gst cost
loonies = newCost; // loonies will output as integer when cost has decimals
printf("Loonies required: %d", loonies);
if (loonies == 0) // == used to compare equality
loonies = 1; // if loonies = 0, loonreq will be infinite causing code to crash and stop
loonreq = (float)((int)(100 * newCost) % (100 * loonies)) / 100; // casting int and * 100 on float values for modulo since it can only calculate for integer value
printf(", balance owing $%.2f\n", loonreq); // %.2f shows a float with 2 decimal places
quarters = 100 * loonreq / 25; // 100*loonreq allows the code to find how many quarters by dividing by 25
printf("Quarters required: %d", quarters);
if (quarters == 0)
quarters = 1;
quartreq = (float)((int)(100 * loonreq) % (int)(100 * (quarters * .25))) / 100;
printf(", balance owing $%.2f\n", quartreq);
dimes = 100 * quartreq / 10;
printf("Dimes required: %d", dimes);
if (dimes == 0)
dimes = 1;
dimereq = (float)((int)(100 * quartreq) % (int)(100 * (dimes * .10))) / 100;
printf(", balance owing $%.2f\n", dimereq);
nickels = 100 * dimereq / 5;
printf("Nickels required: %d", nickels);
if (nickels == 0)
nickels = 1;
nickreq = (float)((int)(100 * dimereq) % (int)(100 * (nickels * .05))) / 100;
printf(", balance owing $%.2f\n", nickreq);
pennies = 100 * nickreq / 1;
printf("Pennies required: %d", pennies);
if (pennies == 0)
pennies = 1;
penreq = (float)((int)(100 * nickreq) % (int)(100 * (pennies * .01))) / 100;
printf(", balance owing $%.2f\n", penreq);
return 0;
}
When I run this code on Visual Studio, I get the correct outputs for certain inputs like 8.68 but when I submit this file through PuTTY for my professor, it tests it for me and I get an incorrect balance (a penny off) after dimes deducted. When I put in a value like 76.54, I'm a penny off after the deduction of loonies. Inputting a value like 76.76, I get the correct output. I don't exactly know what's happening. My professor looked at my code and said it's because of type changes? and recommended that I use this method instead, example
dimes = (balance / 100) / 0.1;
balance = (int)(balance) % 10;
I'm not sure how to use this method though.
Edit: I need to use the modulus operator and casting for this assignment. gcc is used when I submit on PuTTy.
The rounding logic is not giving the correct answer. Say we have an input 123. The GST should be 15.99. However your code will give 16.00. To fix this try replacing gstCost = cost * .13 + .005; with gstCost = (round(cost * .13 * 100) / 100);
Don't use float/double at all for money. Computers are binary and use base 2 math. Not all decimal values (base 10) can be represented precisely in base 2 floating point, causing rounding errors. Do your math with int (or long long) representing pennies and it will be accurate up to the max value of a 32-bit signed int (millions) or 64-bit signed long long (quadrillions).
(float)((int)(100 * quartreq) % (int)(100 * (dimes * .10))) / 100;
I'm sure that changing types like float to int and vise versa can lose a some data throught the parsing part.
example: you have a double variable set as 1.2 when you parse it to int it become 1 and not 1.2 and it won't become 1.2 when you parse it back to double.
Do your calculations with the float type and if you want to change data type only parse it in the final result.
I ran into a peculiar problem when trying to write a program for counting out change for a user. The user would enter in an amount -- in dollars and cents -- and the program would compute how many bills and coins the user would receive.
I have issues with counting out the coins portion, as I am not calculating the amount I expect to be calculating.
For example, the user would input 123.45 so I would pull the 123 out and compute the dollars portion, and then pull the .45 and compute the coins portion.
I feel like there is an issue with casting a floating-point number to an integer, but I'm not sure. If I enter .45 and then try and print it, the program would output .449997 instead.
What's going on here?
#include <stdio.h>
void giveChange(float);
void countCents(float);
int main()
{
float amount;
printf("Enter in an amount (in dollars and cents): ");
scanf("%f", &amount);
giveChange(amount);
getchar();
return 0;
}
void giveChange(float amount)
{
int newAmount = (int)amount;
int hundreds, fifties, tens, fives, ones;
float cents = amount - newAmount;
hundreds = newAmount / 100;
newAmount %= 100;
fifties = newAmount / 50;
newAmount %= 50;
tens = newAmount / 10;
newAmount %= 10;
fives = newAmount / 5;
newAmount %= 5;
printf("\nHundreds: %i\nFifties: %i\nTens: %i\nFives: %i\nOnes: %i\n", hundreds, fifties, tens, fives, newAmount);
countCents(cents);
return;
}
void countCents(float cents)
{
int newCents = cents * 10;
int quarters, dimes, nickels, pennies;
quarters = newCents / 25;
newCents %= 25;
dimes = newCents / 10;
newCents %= 10;
nickels = newCents / 5;
newCents %= 5;
printf("\nQuarters: %i\nDimes: %i\nNickels: %i\nPennies: %i\n", quarters, nickels, dimes, newCents);
return;
}
You are using floating-point data types to represent fixed-point calculations. Floating-point arithmetic is subject to a number of rounding errors such as those you've seen.
Instead of using float or double to represent dollars and fractions, use a plain int representing the total amount of cents. For example, instead of using float value = 4.25; for $4.25, use int value = 425; (425 cents) instead. Then, value/100 (== 4) is the number of dollars (note: integer division is truncated), and value%100 (== 25) is the number of pennies.
(This is called 'fixed-point' since the decimal point position is fixed in the representation (in this case, it is always after exactly two decimal digits). Fixed-point arithmetic is basically integer arithmetic with post-calculation corrections.)
You can't represent change with a float. You need to treat money as an integral number of cents (or tenths of a cent, hundredths of a cent, &ct depending on application).
Try something like this.
#include <stdio.h>
int main (void)
{
int dollars;
int cents;
scanf ("%d.%d", &dollars, ¢s);
printf ("%d dollars, %d cents\n", dollars, cents);
return 0;
}
Long story short floats do not work like ints and thus you cannot express precise numbers with them.
See here.
Try multiplying the input by 100 and then using %100!
I've just started learning C so bear with me. I'm trying to figure out how to program a change calculator using functions. I just need help on learning how to structure it. I figured out the formulas I need to use below:
int dollars = float money / 1
float quarters = money – dollars
int quartercount = quarters / .25
float dimes = quarters - (quartercount*.25)
int dimescount = dimes / .10
float nickels = dimes – (dimescount*.10)
int nickelcount = nickels / .05
float pennies = nickels – (nickelcount*.05)
int pennycount = pennies / .01
EDIT: I should've made that more clear sorry. we would be inputing the change amount and it would display what dollars and coins it would take to make that change.
Any amount of help would be greatly appreciated!
The basic structure is like this. But it depends if you need to return the values, or just print them (like this example).
void changeCalculator(float money) {
int dollars = money / 1;
printf("Dollars = %d",dollars);
float quarters = money – dollars;
int quartercount = quarters / .25;
printf("Quarters = %d",quartercount );
float dimes = quarters - (quartercount*.25);
int dimescount = dimes / .10;
printf("Dimes= %d",dimescount);
float nickels = dimes – (dimescount*.10);
int nickelcount = nickels / .05;
printf("Nickels= %d",nickelcount );
float pennies = nickels – (nickelcount*.05);
int pennycount = pennies / .01;
printf("Pennies= %d",pennycount );
}