I have used this page to address questions that people have e-mailed me.
One person asked me about how to figure out someone's change.
As with all programs you are going to need to plan your actions in advance. Once the amount of change has been determined, you will need to do an integer divide to determine how many coins.
Than do the same equation as a mod to determine how much is left over.
$1.00
- .23 purchase
_____
.77
77 / 25 = 3
77 % 25 = 2 (mod)
2 / 10 = 0
2 % 10 = 0
2 / 5 = 0
2 % 5 = 0
2 / 1 = 2
2 % 1 = 0
Now it is time to write the actual code.
#include
void main()
{
int remainder;
int result;
int totChange;
totChange = 100 - 23;
result = totChange / 25;
remainder = totChange % 25;
printf("Give %i Quarters", result);
result = totChange / 10;
remainder = totChange % 10;
printf("Give %i Dimes", result);
result = totChange / 05;
remainder = totChange % 05;
printf("Give %i Nickles", result);
result = totChange / 1;
remainder = totChange % 1;
printf("Give %i Pennies", result);
return 0;
}
//---------------------------------------------------
// Description: Simulates rolling a die
// Programmer: Dwayne Stewart
// Hardware: 80286 or greater
// Software: MSDOS 3.11
// Input: Keyboard
// Output: Screen
// Parameters: Void
// Returns: Void
// History:
//----------------------------------------------------
#include
#include
#include
#include
#include
void main (void)
{
int r;
char ans;
clrscr();
srand (time (NULL));
ans = getans();
do
{
r = rand() % 6;
switch (r + 1)
{
case 1:
showOne();
break;
case 2:
showTwo();
break;
case 3:
showThree();
break;
case 4:
showFour();
break;
case 5:
showFive();
break;
case 6:
showSix();
break;
default:
printf("ERROR");
}
ans = getans();
}
while(ans == 'y' || ans == 'Y');
return0;
}
//==============
char getans()
{
char ans;
printf("Throw y/n ?");
ans = 'x';
while (ans != 'y' && ans != 'Y' && ans != 'n' && ans != 'N')
ans = getchar();
return ans;
}
//==============
void showOne()
{
printf(" -----\n"
"| |\n"
"| * |\n"
"| |\n"
" -----\n");
}
//==============
void showTwo()
{
printf(" -----\n"
"| * |\n"
"| |\n"
"| * |\n"
" -----\n");
}
//==============
void showThree()
{
printf(" -----\n"
"| * |\n"
"| * |\n"
"| * |\n"
" -----\n");
}
//==============
void showFour()
{
printf(" -----\n"
"| * * |\n"
"| |\n"
"| * * |\n"
" -----\n");
}
//==============
void showFive()
{
printf(" -----\n"
"|* * |\n"
"| * |\n"
"|* * |\n"
" -----\n");
}
//==============
void showSix()
{
int i;
printf(" -----\n");
for(i=0 ; i < 3 ; i++)
printf("| * * |\n");
printf(" -----\n");
}
For more informationa and source code check out Jamsa's Bible. This is a great book for beginners and veterans alike! Click on the picture if you want to find out more.