Wednesday 21 September 2011

How do i make this simple program in C?

i just started C programing and i was wondering how i could this program:



the program will ask the user how many coins he/she has and how much money is needed. then the program will calculate if it is possible to make that change with the amount of coins that the user has the program will print out how many coins of each type the user will need.



Note that the change doesn't always have to be made with all of the coins.
How do i make this simple program in C?
I don't know C, but here's the mechanics (given in C++) of how to do it



#include %26lt;iostream%26gt;



using namespace std;



int main()

{

label:

int p=0;

int n=0;

int d=0;

int q=0;

int hd=0;

int pennies;

int nickels;

int dimes;

int quarters;

int halfDollars;

int total;

cin%26gt;%26gt;pennies;

cin%26gt;%26gt;nickels;

cin%26gt;%26gt;dimes;

cin%26gt;%26gt;quarters;

cin%26gt;%26gt;halfDollars;

cin%26gt;%26gt;total;



for (int HD=0; HD%26lt;halfDollars %26amp;%26amp; (hd+1)*50%26lt;total;HD++){

hd=hd+1;

cout%26lt;%26lt;hd%26lt;%26lt;%26quot; half dollars \n%26quot;;

cout%26lt;%26lt;total-(hd*50+q*25+d*10+n*5+p)%26lt;%26lt;%26quot; remains\n%26quot;;

}

if ((hd*50+q*25+d*10+n*5+p)%26lt;total){

for (int Q=0; Q%26lt;quarters %26amp;%26amp; (hd)*50+(q+1)*25%26lt;total;Q++){

q=q+1;

cout%26lt;%26lt;q%26lt;%26lt;%26quot; quarters \n%26quot;;

cout%26lt;%26lt;total-(hd*50+q*25+d*10+n*5+p)%26lt;%26lt;%26quot; remains\n%26quot;;

}

}

if ((hd*50+q*25+d*10+n*5+p)%26lt;total){

for (int D=0; D%26lt;dimes %26amp;%26amp; (hd)*50+(q)*25+(d+1)*10%26lt;total;D++){

d=d+1;

cout%26lt;%26lt;d%26lt;%26lt;%26quot; dimes \n%26quot;;

cout%26lt;%26lt;total-(hd*50+q*25+d*10+n*5+p)%26lt;%26lt;%26quot; remains\n%26quot;;

}

}

if ((hd*50+q*25+d*10+n*5+p)%26lt;total){

for (int N=0; N%26lt;nickels %26amp;%26amp; (hd)*50+(q)*25+d*10+(n+1)*5+p%26lt;total;N++)鈥?br>
n=n+1;

cout%26lt;%26lt;n%26lt;%26lt;%26quot; nickels\n%26quot;;

cout%26lt;%26lt;total-(hd*50+q*25+d*10+n*5+p)%26lt;%26lt;%26quot; remains\n%26quot;;

}

}

if ((hd*50+q*25+d*10+n*5+p)%26lt;total){

for (int P=0; P%26lt;pennies %26amp;%26amp; (hd)*50+(q)*25+d*10+n*5+(p+1)%26lt;total;P++)鈥?br>
p=p+1;

cout%26lt;%26lt;p%26lt;%26lt;%26quot; pennies\n%26quot;;

cout%26lt;%26lt;total-(hd*50+q*25+d*10+n*5+p)%26lt;%26lt;%26quot; remains\n%26quot;;

}

}

if ((hd*50+q*25+d*10+n*5+p)==total){

cout%26lt;%26lt;p%26lt;%26lt;%26quot; pennies + %26quot;%26lt;%26lt;n%26lt;%26lt;%26quot; nickels + %26quot;%26lt;%26lt;d%26lt;%26lt;%26quot; dimes + %26quot;%26lt;%26lt;q%26lt;%26lt;%26quot; quarters + %26quot;%26lt;%26lt;hd%26lt;%26lt;%26quot; halfdollars gives %26quot;%26lt;%26lt;total;

}

else {

cout%26lt;%26lt;%26quot;can't be made%26quot;;

}



goto label;

}
How do i make this simple program in C?
First, you'll need to get the input for each of the coins you wish:

%26gt;How many Quarters? 2%26lt;

(repeat for dimes, nickels, and pennies.)



Second, you'll need to get the cost of the item



To do those two steps, look into printf and scanf. They're kind of tricky, but you're sure to find examples out there.



Next, you need to see if the cost is greater than a quarter. If it is, start subtracting quarters out of the total number of quarters.

total_cost -= 25;

number_quarters--;

Do that inside of a while loop that that checks if the total_cost is greater than 25 and if you have quarters.



Repeat that step for dimes (10) nickels (5) and pennies(1).



The idea is that you're trying to subtract the value of the coins from the amount of change required.



If you get to the end of the program (checking quarters, dimes, nickels, and pennies) and you still have a value in total_cost, then you don't have the correct change to make the cost.

----

There's the functional design. That should get you past worrying about how to write the program into the actual act of learning C.