In: Computer Science
I am trying to make a new code that uses functions to make it. My functions are below the code.
<?php
*/
$input;
$TenBills = 1000;
$FiveBills = 500;
$OneBills = 100;
$Quarters = 25;
$Dimes = 10;
$Nickels = 5;
$Pennies = 1;
$YourChange = 0;
$input = readline("Hello, please enter your amount of cents:\n");
if(ctype_digit($input)) {
$dollars =(int)($input/100);
$cents = $input%100;
$input >= $TenBills;
$YourChange = (int)($input/$TenBills);
$input -= $TenBills * $YourChange;
print "Change for $dollars dollars and $cents cents:\n";
print " $YourChange ten dollar bills\n";
$YourChange = 0;
$input >= $FiveBills;
$YourChange = (int)($input/$FiveBills);
$input -= $FiveBills * $YourChange;
print " $YourChange five dollar bills\n";
$YourChange = 0;
$input >= $OneBills;
$YourChange = (int)($input/$OneBills);
$input -= $OneBills * $YourChange;
print " $YourChange one dollar bills\n";
$YourChange = 0;
$input >= $Quarters;
$YourChange = (int)($input/$Quarters);
$input -= $Quarters * $YourChange;
print " $YourChange quarters\n";
$YourChange = 0;
$input >= $Dimes;
$YourChange = (int)($input/$Dimes);
$input -= $Dimes * $YourChange;
print " $YourChange dimes\n";
$YourChange = 0;
$input >= $Nickels;
$YourChange = (int)($input/$Nickels);
$input -= $Nickels * $YourChange;
print " $YourChange nickels\n";
$YourChange = 0;
$input >= $Pennies;
$YourChange = (int)($input/$Pennies);
$input -= $Pennies * $YourChange;
print " $YourChange pennies\n";
$YourChange = 0;
}
else {
print "Please enter a positive integer (No negatives or letters)\n";
}
?>
<?php
function userinput() {
$input = readline("Hello, please enter amount of cents:\n");
if (ctype_digit($input)) {
$dollars =(int)($input/100);
print "$input\n";
} else {
print "Enter a positive integer (not negatives or letters)\n";
}
}
userinput();
function compute($cents = $input%100) {
$bills["Tens"] = (int)($input/1000);
$bills["Fives"] = (int)(($input % 1000) / 500);
$bills["Ones"] = (int)(($input % 1000 % 500) / 100);
$bills["Quarters"] = (int)(($input % 100) / 25);
$bills["Dimes"] = (int)(($input % 25) / 10);
$bills["Nickels"] = (int)(($input % 25 % 10) / 5);
$bills["Pennies"] = (int)(($input % 5) / 1);
return $bills;
}
compute();
function printoutput(compute) {
print "Change for $dollars dollars and $cents cents:\n";
print $bills["Tens"] . "dollar bills\n";
print $bills["Fives"] . "dollar bills\n";
print $bills["Ones"] . "dollar bills\n";
print $bills["Quarters"] . "dollar bills\n";
print $bills["Dimes"] . "dollar bills\n";
print $bills["Nickels"] . "dollar bills\n";
print $bills["Pennies"] . "dollar bills\n";
}
printoutput();
function main() {
userinput();
compute();
printoutput();
}
main();
?>
Function userinput() it should take no parameters and return an integer value for the number of cents. This function should validate the user input.
function compute() It should take the number of cents as a parameter and return an associative array that maps denominations to amounts.
Function printoutput() It should take an associative array mapping denominations to amounts as a parameter and print the output as in the previous program.
Function main() that takes no parameters and has no return value. It should call the previous functions correctly to execute the logic of the program.
// I have corrected your code. Please check it. It is working as you said.
<?php
function userinput() {
$input = readline("Hello, please enter amount of cents:\t");
if (ctype_digit($input)) {
return $input;
}
print "Enter a positive integer (not negatives or
letters)\n";
}
function compute($cents) {
$bills["Tens"] = (int)($cents/1000);
$bills["Fives"] = (int)(($cents % 1000) / 500);
$bills["Ones"] = (int)(($cents % 1000 % 500) / 100);
$bills["Quarters"] = (int)(($cents % 100) / 25);
$bills["Dimes"] = (int)(($cents % 25) / 10);
$bills["Nickels"] = (int)(($cents % 25 % 10) / 5);
$bills["Pennies"] = (int)(($cents % 5) / 1);
return $bills;
}
function printoutput($bills, $cents, $dollars) {
print "Change for $dollars dollars and $cents cents:\n";
print $bills["Tens"] . " ten dollar bills\n";
print $bills["Fives"] . " five dollar bills\n";
print $bills["Ones"] . " one dollar bills\n";
print $bills["Quarters"] . " quarters dollar bills\n";
print $bills["Dimes"] . " dimes dollar bills\n";
print $bills["Nickels"] . " nickels dollar bills\n";
print $bills["Pennies"] . " pennies dollar bills\n";
}
function main() {
$cents = userinput();
$dollersToPrint = (int)($cents/100);
$bills = compute($cents);
$cents = $cents%100;
printoutput($bills, $cents, $dollersToPrint);
// i have passed $cents & $dollersToPrint for the output
purpose. if you don't want it then define then globally &
done.
}
main();
?>
// If you need anything on this program please let me know.