Can you help me incorporate two non-trivial functions in the program? (Non-trivi
ID: 3758524 • Letter: C
Question
Can you help me incorporate two non-trivial functions in the program? (Non-trivial means that the function does more than simply print a line of text.)
Here are some examples of what you might want to use for your functions:
Input functions:
1. Obtain opening (current) balance.
2. Obtain number the number of deposits.
3. Obtain number of withdrawals.
4. Obtain deposit amounts.
5. Obtain withdrawal amounts.
Output functions:
1. Display closing balance and associated message.
2. Display message based on closing balance.
3. Display bank record.
#include
int main(void)
{
/* Variable Declarations */
/* --------------------- */
int x, number_of_deposits, number_of_withdrawals;
float deposits[5], withdrawals[5];
float current_balance, balance;
float total_deposits = 0, total_withdrawals = 0;
/* Display Bank Greeting */
/* --------------------- */
printf("Welcome to the Computer Banking System ");
/* This trap is so you can enter your current balance without it being less than zero */
/* ---------------------------------------------------------------------------------- */
do
{
/* Prompts user to enter current balance */
/* ------------------------------------- */
printf ("Enter your current balance in dollars and cents: ");
scanf ("%f",¤t_balance);
fflush(stdin);
/* It will display an error if the current balance is less than 0 dollars */
/* ---------------------------------------------------------------------- */
if (current_balance < 0)
printf ("***Beginning balance must be at least zero, please re-enter. ");
}
/* The loop will end if current balance is less than 0 */
/* --------------------------------------------------- */
while (current_balance < 0);
do
{
/* This prompts the user to enter the number of deposits */
/* ----------------------------------------------------- */
printf (" Enter the number of deposits (0-5): ");
scanf ("%i",&number_of_deposits);
fflush (stdin);
/* This loops limits the amount of deposits so it's between 0 and 5. Displays error otherwise */
/* ------------------------------------------------------------------------------------------ */
if (number_of_deposits < 0 || number_of_deposits > 5)
printf ("***Invalid number of deposits, please re-enter.");
}
/* If it's between 0 and 5 the loop ends */
/* ------------------------------------- */
while (number_of_deposits < 0 || number_of_deposits > 5);
do
{
/* This prompts the user to enter the number of withdrawals */
/* -------------------------------------------------------- */
printf (" Enter the number of withdrawals (0-5): ");
scanf ("%i",&number_of_withdrawals);
fflush (stdin);
/* This loops limits the amount of deposits so it's between 0 and 5. Displays error otherwise */
/* ------------------------------------------------------------------------------------------ */
if ( number_of_withdrawals < 0 || number_of_withdrawals > 5)
printf ("***Invalid number of withdrawals, please re-enter.");
}
/* If it's between 0 and 5 the loop ends */
/* ------------------------------------- */
while (number_of_withdrawals < 0 || number_of_withdrawals > 5);
/* Gives a space to make it easier to read */
/* --------------------------------------- */
printf(" ");
/* This 'for loop' will take down deposit amounts entered */
/* ------------------------------------------------------ */
for (x = 1; x <= number_of_deposits; x++)
{
do
{
/* Prompts user to enter amount of deposits */
/* ---------------------------------------- */
printf ("Enter the amount of deposit #%i: ", x);
scanf ("%f",&deposits[x]);
fflush (stdin);
/* The loop will keep going until the amounts are 0 or greater. Displays error otherwise */
/* ------------------------------------------------------------------------------------- */
if (deposits[x] <= 0)
printf ("***Deposit amount must be greater than zero, please re-enter ");
}
while (deposits[x] <= 0);
/* This will create a total deposit amount by adding the deposits entered together */
/* ------------------------------------------------------------------------------- */
total_deposits = total_deposits + deposits[x];
/* end for loop */
/* ------------ */
}
/* Gives a space to make it easier to read */
/* --------------------------------------- */
printf(" ");
/* This 'for loop' will take down withdrawal amounts entered */
/* ------------------------------------------------------ */
for (x = 1; x <= number_of_withdrawals; x++)
{
do
{
/* Prompts user to enter amount of withdrawals */
/* ------------------------------------------- */
printf ("Enter the amount of withdrawal #%i: ", x);
scanf ("%f",&withdrawals[x]);
fflush (stdin);
/* This loop will display an error if the withdrawal amount is greater than the balance */
/* ------------------------------------------------------------------------------------ */
if (withdrawals[x] > current_balance)
printf ("***Withdrawal amount exceeds current balance, please re-enter. ");
else
/* The loop will keep going until the amounts are 0 or greater. Displays error otherwise */
/* ------------------------------------------------------------------------------------- */
if (withdrawals[x] <= 0)
printf ("*** Withdrawal amount must be greater than zero, please re-enter. ");
}
/* Loop ends when all amounts are 0 or greater */
/* ------------------------------------------- */
while (withdrawals[x] > current_balance || withdrawals[x] <= 0);
/* This will create a total withdrawal amount by adding the withdrawals entered together */
/* ------------------------------------------------------------------------------------- */
total_withdrawals = total_withdrawals + withdrawals[x];
/* This ensures that the withdrawal is less than the balance */
/* --------------------------------------------------------- */
balance = current_balance - total_withdrawals + total_deposits;
/* End the loop */
/* ------------ */
}
/* Calculate and display the closing balance */
/* ----------------------------------------- */
printf (" *** The closing balance is $%7.2f *** ", balance);
/* A corresponding message will display depending on how much is in your end balance */
/* --------------------------------------------------------------------------------- */
if (balance >= 50000.00)
printf ("*** It is time to invest some money!*** ");
else if (balance >= 15000.00 && balance <= 49999.99)
printf ("*** Maybe you should consider a CD.*** ");
else if (balance >= 1000.00 && balance <= 14999.99)
printf ("*** Keep up the good work!*** ");
else
printf ("*** Your balance is getting low!*** ");
/* This will display your bank statement */
/* ------------------------------------- */
printf (" *** Bank Statement *** ");
printf (" Starting Balance: $ %6.2f ", current_balance);
for (x = 1; x <= number_of_deposits; x++)
{
printf ("Deposit #%i: %14.2f ", x, deposits[x]);
}
for ( x = 1; x<= number_of_withdrawals; x++)
{
printf (" Withdrawal #%i: %11.2f", x, withdrawals[x]);
}
printf (" Ending Balance: $ %8.2f ", balance);
{
return 0;
}
getchar();
} /* End main */
Explanation / Answer
Hope this will help you.
#include <stdio.h>
float readCurrentBalance()
{
float x;
scanf ("%f",&x);
return x;
}
int readNumberOfDeposits()
{
int x;
scanf("%i",&x);
return x;
}
int readNumberOfWithdrawals()
{
int x;
scanf("%i",&x);
return x;
}
float readDepositAmounts()
{
float x;
scanf ("%f",&x);
return x;
}
float readWithdrawalAmounts()
{
float x;
scanf ("%f",&x);
return x;
}
void displayClosingBalance(float balance)
{
printf (" *** The closing balance is $%7.2f *** ", balance);
/* A corresponding message will display depending on how much is in your end balance */
/* --------------------------------------------------------------------------------- */
if (balance >= 50000.00)
printf ("*** It is time to invest some money!*** ");
else if (balance >= 15000.00 && balance <= 49999.99)
printf ("*** Maybe you should consider a CD.*** ");
else if (balance >= 1000.00 && balance <= 14999.99)
printf ("*** Keep up the good work!*** ");
else
printf ("*** Your balance is getting low!*** ");
}
int main(void)
{
/* Variable Declarations */
/* --------------------- */
int x, number_of_deposits, number_of_withdrawals;
float deposits[5], withdrawals[5];
float current_balance, balance;
float total_deposits = 0, total_withdrawals = 0;
/* Display Bank Greeting */
/* --------------------- */
printf("Welcome to the Computer Banking System ");
/* This trap is so you can enter your current balance without it being less than zero */
/* ---------------------------------------------------------------------------------- */
do
{
/* Prompts user to enter current balance */
/* ------------------------------------- */
printf ("Enter your current balance in dollars and cents: ");
current_balance = readCurrentBalance();
fflush(stdin);
/* It will display an error if the current balance is less than 0 dollars */
/* ---------------------------------------------------------------------- */
if (current_balance < 0)
printf ("***Beginning balance must be at least zero, please re-enter. ");
}
/* The loop will end if current balance is less than 0 */
/* --------------------------------------------------- */
while (current_balance < 0);
do
{
/* This prompts the user to enter the number of deposits */
/* ----------------------------------------------------- */
printf (" Enter the number of deposits (0-5): ");
number_of_deposits = readNumberOfDeposits();
fflush (stdin);
/* This loops limits the amount of deposits so it's between 0 and 5. Displays error otherwise */
/* ------------------------------------------------------------------------------------------ */
if (number_of_deposits < 0 || number_of_deposits > 5)
printf ("***Invalid number of deposits, please re-enter.");
}
/* If it's between 0 and 5 the loop ends */
/* ------------------------------------- */
while (number_of_deposits < 0 || number_of_deposits > 5);
do
{
/* This prompts the user to enter the number of withdrawals */
/* -------------------------------------------------------- */
printf (" Enter the number of withdrawals (0-5): ");
number_of_withdrawals = readNumberOfWithdrawals();
fflush (stdin);
/* This loops limits the amount of deposits so it's between 0 and 5. Displays error otherwise */
/* ------------------------------------------------------------------------------------------ */
if ( number_of_withdrawals < 0 || number_of_withdrawals > 5)
printf ("***Invalid number of withdrawals, please re-enter.");
}
/* If it's between 0 and 5 the loop ends */
/* ------------------------------------- */
while (number_of_withdrawals < 0 || number_of_withdrawals > 5);
/* Gives a space to make it easier to read */
/* --------------------------------------- */
printf(" ");
/* This 'for loop' will take down deposit amounts entered */
/* ------------------------------------------------------ */
for (x = 1; x <= number_of_deposits; x++)
{
do
{
/* Prompts user to enter amount of deposits */
/* ---------------------------------------- */
printf ("Enter the amount of deposit #%i: ", x);
deposits[x] = readDepositAmounts();
fflush (stdin);
/* The loop will keep going until the amounts are 0 or greater. Displays error otherwise */
/* ------------------------------------------------------------------------------------- */
if (deposits[x] <= 0)
printf ("***Deposit amount must be greater than zero, please re-enter ");
}
while (deposits[x] <= 0);
/* This will create a total deposit amount by adding the deposits entered together */
/* ------------------------------------------------------------------------------- */
total_deposits = total_deposits + deposits[x];
/* end for loop */
/* ------------ */
}
/* Gives a space to make it easier to read */
/* --------------------------------------- */
printf(" ");
/* This 'for loop' will take down withdrawal amounts entered */
/* ------------------------------------------------------ */
for (x = 1; x <= number_of_withdrawals; x++)
{
do
{
/* Prompts user to enter amount of withdrawals */
/* ------------------------------------------- */
printf ("Enter the amount of withdrawal #%i: ", x);
withdrawals[x] = readWithdrawalAmounts();
fflush (stdin);
/* This loop will display an error if the withdrawal amount is greater than the balance */
/* ------------------------------------------------------------------------------------ */
if (withdrawals[x] > current_balance)
printf ("***Withdrawal amount exceeds current balance, please re-enter. ");
else
/* The loop will keep going until the amounts are 0 or greater. Displays error otherwise */
/* ------------------------------------------------------------------------------------- */
if (withdrawals[x] <= 0)
printf ("*** Withdrawal amount must be greater than zero, please re-enter. ");
}
/* Loop ends when all amounts are 0 or greater */
/* ------------------------------------------- */
while (withdrawals[x] > current_balance || withdrawals[x] <= 0);
/* This will create a total withdrawal amount by adding the withdrawals entered together */
/* ------------------------------------------------------------------------------------- */
total_withdrawals = total_withdrawals + withdrawals[x];
/* This ensures that the withdrawal is less than the balance */
/* --------------------------------------------------------- */
balance = current_balance - total_withdrawals + total_deposits;
/* End the loop */
/* ------------ */
}
/* Calculate and display the closing balance */
/* ----------------------------------------- */
displayClosingBalance(balance);
/* This will display your bank statement */
/* ------------------------------------- */
printf (" *** Bank Statement *** ");
printf (" Starting Balance: $ %6.2f ", current_balance);
for (x = 1; x <= number_of_deposits; x++)
{
printf ("Deposit #%i: %14.2f ", x, deposits[x]);
}
for ( x = 1; x<= number_of_withdrawals; x++)
{
printf (" Withdrawal #%i: %11.2f", x, withdrawals[x]);
}
printf (" Ending Balance: $ %8.2f ", balance);
{
return 0;
}
getchar();
} /* End main */