In: Computer Science
You should read this question as if the change giver is an artificial intelligence-based bank teller who has to give amounts of money to customers by automatically working out the amounts. You are asked to write a modular solution (algorithm and C program) that will accept an integer value in the range of 5-95 inclusive. Your solution should also ensure that input values are in multiples of 5. Based on valid input, your solution should calculate how many coins of each denomination should be returned, and display this to the user. Coin values are 50, 20, 10 and 5. The solution should aim to give as much of the higher valued coins as possible. For example, a poor solution for an input of 30 cents would give six 5 cent coins. A correct solution would give a 20 cent coin and a 10 cent coin. After each output, the user should be asked whether they wish to continue or exit/terminate the program. Your solution (algorithm and program) should be designed using a modular approach. This requires the submission of a structure chart, a high-level algorithm, and subsequent decompositions of each step (i.e. low-level algorithms). Hint 1- multple of 5 condition. 2- do while loop in switch to terminate the question. 3- no of input is divisible of 5
Hey,
Please comment if you have any doubt or you need to know it detail....Hope this help you!!!!!....Do like ...
Have a happy day buddy.....
C PROGRAM
#include <stdio.h>
int getData()
{
int totalChange;
printf ("Please insert amount of change required:\n");
scanf ("%d%*c", &totalChange);
return(totalChange);
}
int Check(int totalChange)
{
const int MAXCHANGE = 95;
const int MINCHANGE = 5;
int valid;
if (totalChange > MAXCHANGE || totalChange < MINCHANGE)
{
printf ("Please insert a value greater then 5 and less then 95\n");
valid = 1;
}
else if(totalChange%5!=0)
{
printf ("Please insert a value of multiple of 5\n");
valid=1;
}
return(valid);
}
int Change(int totalChange, int cent)
{
int t, counter = 0;
while (totalChange >= cent)
{
t= (totalChange%cent);
if(t==0)
{
counter=(totalChange/cent);
printf ("\n%d = %d\n", cent, counter);
return(t);
}
counter=(totalChange/cent);
totalChange=t;
printf ("\n%d = %d\n", cent, counter);
}
return (totalChange);
}
int main()
{
int totalChange, fiftyC, twentyC, tenC, fiveC, valid,ch;
const int FIFTYCENT = 50;
const int TWENTYCENT = 20;
const int TENCENT = 10;
const int FIVECENT = 5;
do
{
totalChange = getData ();
valid = Check (totalChange);
if (valid != 1)
{
fiftyC= Change (totalChange, FIFTYCENT);
twentyC= Change ( fiftyC, TWENTYCENT);
tenC= Change (twentyC, TENCENT);
fiveC= Change (tenC, FIVECENT);
}
printf ("Do you want to continue(1/0)::");
scanf("%d",&ch);
}while(ch==1);
return (0);
}
output::