It's a money changing program. For the first part, it gives me the best possible way for the change. It works, but not for $1,2,5,10,20,50,100?? It gives me the same amount of number back, where I want 2-$50 for the 100 or, 2-$20 and 1-$10 for $50 change.. how can I fix it?? the 2nd part of the program works where I have to find the number of possible ways for the change!!
the program is like this:
#include <iostream>
using namespace std;
int penNy(int x)
{
return 1;
}
int niCkel(int x)
{
if (x >= 0) return niCkel(x-5) + penNy(x);
return 0;
}
int diMe(int x)
{
if (x >= 0) return diMe(x-10) + niCkel(x);
return 0;
}
int quaRter(int x)
{
if (x >= 0) return quaRter(x-25) + diMe(x);
return 0;
}
int cent_50(int x)
{
if (x >= 0) return cent_50(x-50) + quaRter(x);
return 0;
}
int doLlar(int x)
{
if (x >= 0) return doLlar(x-100) + cent_50(x);
return 0;
}
int dollar_5(int x)
{
if (x >= 0) return dollar_5(x-500) + doLlar(x);
return 0;
}
int dollar_10(int x)
{
if (x >= 0) return dollar_10(x-1000) + dollar_5(x);
return 0;
}
int main ( )
{
float userNUmber;
int change,dollar100,dollar50,dollar20,dollar10,dollar5,dollar2,dollar1,
cent50,quarters, dimes, nickels, pennies; // declare variables
cout <<"Enter the amount of money: ";
cin >> userNUmber; // input the amount of change
change = userNUmber * 100;
dollar100=change/10000;
change=change%10000;
dollar50=change/5000;
change=change%5000;
dollar20=change/2000;
change=change%2000;
dollar10=change/1000;
change=change%1000;
dollar5=change/500;
change=change%500;
dollar2=change/200;
change=change%200;
dollar1=change/100;
change=change%100;
cent50=change/50;
change=change%50;
quarters = change / 25; // calculate the number of quarters
change = change % 25; // calculate remaining change needed
dimes = change / 10; // calculate the number of dimes
change = change % 10; // calculate remaining change needed
nickels = change / 5; // calculate the number of nickels
pennies = change % 5; // calculate pennies
Okay, let's break it down into a couple of pieces.
In your main function, you've got the variable y being initialized by the user.
The value stored in that variable is then multiplied by 100.
Next, you make a function call to same_thing().
This is where your problem begins -- inside the function same_thing(), you're creating an entirely NEW variable called "y" that isn't being initialized... this means that the variable "y" (only visible in the scope of same_thing()) will hold a completely random value, one that was previously held in that memory location.
See if you can figure out how to fix that first...