This code is not calculating things right, any ideas why? [duplicate] - c

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Well, suppose that a cashier owes a customer some change and in that cashier’s drawer are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). The problem to be solved is to decide which coins and how many of each to hand to the customer
if some customer is owed 41¢, the biggest first bite that can be taken is 25¢
41 - 25 = 16
another 25¢ bite would be too big
cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem
At that point the cashier calls for one 5¢ bite followed by one 1¢ bite
#include <cs50.h>
#include <stdio.h>
int main(void)
{
float f = get_float("Enter Cash: ");
int q;
int d;
int n;
int p;
float quarter = 0.25;
float dimes = 0.10;
float nickels = 0.05;
float pennies = 0.01;
while ( f != 0)
{
if (f >= quarter){
f = f - quarter;
q = q + 1;
} else if (f >= dimes && f < quarter) {
f = f - dimes;
d = d + 1;
} else if (f >= nickels && f < dimes) {
f = f - nickels;
n = n + 1;
} else {
f = f - pennies;
p = p + 1;
}
printf ("Quarter: %d \n Dimes %d \n Nickels %d \n Pennies %d \n", q,
d, n, p);
}
}
$ ./cash
Enter Cash: 6
Quarter: 32768
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32769
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32770
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32771
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32772

you need to initialize your variables
int q = 0;
int d = 0;
int n = 0;
int p = 0;
otherwise they have unpredictable values

Related

Attempting to calculate how much change/money someone has given user input, returning as (dollar bills, quarters, etc.)

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);
}

Why does value change when converting floating point value to int [duplicate]

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

Bug in While-Loop Keeps Counting Beyond Condition

For school I am to write a C program that takes some amount of cash and returns the smallest number of coins it would take to reach that amount. I don't know what I am doing wrong. I've been tweaking and trying all sorts of different things but I cannot seem to totally debug the program.
The program gives correct answers to some inputs but overstates the amount of coins needed for many inputs.
Here is what I have so far.
#include <stdio.h>
int main()
{
float cash;
int n;
int counter=0;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
for (;;)
{
printf("Enter change amount: ");
scanf("%f",&cash);
if (cash > 0)
{
break;
}
}
n = cash * 100;
counter = 0;
while (n > 0)
{
while (n >= 25)
{
counter ++;
n = n - 25;
quarters ++;
printf("%i\n",n);
}
while (n >= 10 && n < 25)
{
counter ++;
n = n - 10;
dimes ++;
printf("%i\n",n);
}
while (n >= 5 && n < 10)
{
counter ++;
n = n - 1;
nickels++;
printf("%i\n",n);
}
while (n > 0 && n < 5)
{
counter ++;
n = n - 1;
pennies ++;
printf("%i\n",n);
}
}
printf("%d\n",counter + n);
printf("%i quarters, %i dimes, %i nickels, %i pennies\n",
quarters, dimes, nickels, pennies);
return 0;
}
I'm a bit surprised they're wanting you to use break to exit a loop, as you usually want loops to conclude "naturally" (and you usually save breaks for switch statements). Something like this should work, using integer division and the modulus operator (edit note: I'm using two ints instead of a single float because of inaccuracy with the latter. If someone more knowledgeable wants to show how to do it with float, would be interesting.):
#include <stdio.h>
int main() {
int dollar, cent;
int q = 0;
int d = 0;
int n = 0;
int p = 0;
int re;
printf("Enter amount: ");
scanf(" %d.%d", &dollar, &cent);
q = dollar * 4;
re = cent;
q = q + (re / 25);
re = re % 25;
d = re / 10;
re = re % 10;
n = re / 5;
re = re % 5;
p = re;
printf("q %d d %d n %d p %d\n", q, d, n, p);
return 0;
}
This approach also works if, for example, you're given the seconds and want to find the min:sec from that. If you're given 65 seconds, you do 65 / 60 for the minutes portion (which is 1), and the seconds portion is just the remainder after you divide by 60, or 65 % 60 (which is 5).
Here is a more complete answer that fits with the code that you gave us to start with. I changed the loops to subtraction/multiplication and fixed the bug where you were treating nickels as pennies. You don't need the counter variable anymore, but I left it in.
#include <stdio.h>
int main()
{
float cash;
int n;
int counter=0;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
for (;;)
{
printf("Enter change amount: ");
scanf("%f",&cash);
if (cash > 0)
{
break;
}
}
n = cash * 100;
counter = 0;
if (n > 0)
{
quarters = (int)floor(n / 25);
n -= quarters*25;
printf( "%i\n", n );
dimes = (int)floor(n / 10);
n -= dimes*10;
printf("%i\n",n);
nickels = (int)floor(n / 5);
n -= nickels*5;
printf("%i\n",n);
pennies = n;
printf("%i\n",n);
}
printf("%i quarters, %i dimes, %i nickels, %i pennies\n",
quarters, dimes, nickels, pennies);
return 0;
}
First of all, try start with abstracting out how to handle one type of coin:
int coinsNeeded( int amount, int coinAmount )
{
return (int) floor( amount / coinAmount );
}
Then, handle each coin separately:
quarters = coinsNeeded( cash, 25 );
cash -= (quarters * 25);
Just repeat that for each type of coins you want to consider, and then print out the information at the end. There is some disagreement about whether you want to use floating points or not. Floating points do have rounding errors that you want to avoid when using money. what you actually need is a fixed point data type, but I digress. You can get close enough by doing it the way you're doing it (multiplying by 100 and just dealing with pennies).

C program to calculate change

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.

Change Calculator // Struggling with C and Functions

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 );
}

Resources