Question

In: Computer Science

I am trying to make a new code that uses functions to make it. My functions...

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.

Solutions

Expert Solution

// 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.


Related Solutions

Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
I am trying to start saving for retirement. I am investing all my cash into the...
I am trying to start saving for retirement. I am investing all my cash into the S&P 500, which will assume consistently 9.8% interest, compounded annually. I initially put a lump sum of $100 into my account, and I will deposit $10 every second week. a) After 10 years, how much money will I have invested? b) After 10 years, if I sold all of my stocks, how much money will I have in my account? c) After 25 years,...
Professor, In trying to apply my knowledge in the real world, I am trying to create...
Professor, In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 22 and 68, in which I would deposit 25% of my income each year. The income starts at 80,000 with an annual growth rate...
Assembly Question: I am trying to annotate this code and am struggling to understand what it...
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments? .data .star: .string "*" .line: .string "\n" .input: .string "%d" .count: .space 8 .text .global main printstars: push %rsi push %rdi _printstars: push %rdi mov $.star, %rdi xor %rax, %rax call printf pop %rdi dec %rdi cmp $0, %rdi jg _printstars mov $.line, %rdi xor %rax, %rax call printf pop %rdi pop %rsi ret printstarpyramid: mov %rdi,...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT