This program is similar to programming assignment #4. That is, you will have all
ID: 3805627 • Letter: T
Question
This program is similar to programming assignment #4. That is, you will have all of the requirements as in Programming Assignment #4, including data error checking on all inputs. Your program is to behave in the exact same manner as assignment #4, however how you structure your code is a different story. Now we will use functions. Recall Programming Assignment #4: Write a C program that allows the user to make some simple banking transactions. The program should first prompt the user to enter the current balance of his/her bank account (in dollars and cents, not less than zero). The program should then prompt the user to enter the number of withdrawals to make, and then the number of deposits to make. For this assignment, let's set a maximum of 5 deposits and 5 withdrawals (etc.)
Here is the change for program #5: There should be at least 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. Hints: Your code from assignment #4 does not need to be modified too much to solve this problem. The algorithm is the same. Outputting the contents of the arrays in "record form" is pretty straightforward, and should be done in a loop. (This could be a good place for a function.) The most complicated of the above functions is the one which would display the bank record. If you decide that you want to attempt this one, I will give you a head start by providing the function header as shown here: void (display_bank_record (float start_balance, float deposits [ ], int num_deposits, float withdrawals [ ], int num_withdrawals, float end_balance)
Here is what I have so far
/* This program completes simple banking transactions per user */
#include <stdio.h>
int main (void)
{
/* declaring variables */
/*---------------------*/
int x ;
int number_of_deposits;
int number_of_withdrawals;
float deposits[5];
float withdrawals[5];
float current_balance;
float balance =0;
float total_deposits = 0;
float total_withdrawals = 0;
/* display greeting to user */
/*---------------------*/
printf("Welcome to the Computer Banking System ");
do
{
/* Enter Current Balance */
/*------------------------*/
printf ("Enter your current balance in dollars and cents: ");
scanf ("%f",¤t_balance);
fflush(stdin);
/* Error if current balance entered is less than zero dollars */
/*------------------------------------------------------------*/
if (current_balance < 0)
printf ("*** Beginning balance must be at least zero, please re-enter. ");
}
/* End Loop */
/*--------------*/
while (current_balance < 0);
do
{
/* Enter the number of deposits that will be made */
/*--------------------------------------*/
printf (" Enter the number of deposits (0 - 5): ");
scanf ("%i",&number_of_deposits);
fflush (stdin);
/* Limit amount of deposits */
/*--------------------------*/
if (number_of_deposits < 0 || number_of_deposits > 5)
printf ("*** Invalid number of deposits, please re-enter. ");
}
/* End Loop */
/*----------*/
while (number_of_deposits < 0 || number_of_deposits > 5);
do
{
/* Enter the numbers of withdrawals that will be made */
/*------------------------------------------*/
printf (" Enter the number of withdrawals (0 - 5): ");
scanf ("%i",&number_of_withdrawals);
fflush (stdin);
printf(" ");
/* Limit amount of withdrawals */
/*-----------------------------*/
if ( number_of_withdrawals < 0 || number_of_withdrawals > 5)
printf ("*** Invalid number of withdrawals, please re-enter. ");
}
/* Loop stops if value is between 0 and 5 */
/*----------------------------------------*/
while (number_of_withdrawals < 0 || number_of_withdrawals > 5);
/* Add the deposits entered */
/*--------------------------*/
for (x = 1; x <= number_of_deposits; x++)
{
do
{
/* Enter dollar value of deposits */
/*--------------------------------*/
printf ("Enter the amount of deposit #%i: ", x);
scanf ("%f",&deposits[x]);
fflush (stdin);
/* loop until the amount is zero or greater */
/*------------------------------------------*/
if (deposits[x] <= 0)
printf ("*** Deposit amount must be greater than zero. Please re-enter. ");
}
while (deposits[x] <= 0);
/* Total deposit will be created by adding the deposits value */
/*------------------------------------------------------------*/
total_deposits = total_deposits + deposits[x];
/*End Loop*/
/*--------*/
}
printf(" ");
/* Start loop and the amount in the deposits will be positive */
/*------------------------------------------------------------*/
for (x = 1; x <= number_of_withdrawals; x++)
{
do
{
/* Enter dollar value of withdrawals */
/*-----------------------------------*/
printf ("Enter the amount of withdrawal #%i: ", x);
scanf ("%f",&withdrawals[x]);
fflush (stdin);
/* Prompt if the withdrawal amount is greater than the balance */
/*-------------------------------------------------------------*/
if (withdrawals[x] > current_balance)
printf ("*** Withdrawal amount exceeds current balance. ");
else
/* Prompt if the amount is a negative amount */
/*-------------------------------------------*/
if (withdrawals[x] <= 0)
printf ("*** Withdrawal amount must be greater than zero. Please re-enter. ");
}
/* Lnd Loop */
/*----------*/
while (withdrawals[x] > current_balance || withdrawals[x] <= 0);
/* Lotal withdrawal by adding the amount entered */
/*-----------------------------------------------*/
total_withdrawals = total_withdrawals + withdrawals[x];
/* Calculate the balance & validate withdrawal is less than the balance */
/*----------------------------------------------------------------------*/
balance = current_balance - total_withdrawals + total_deposits;
if (balance == 0)
{
/* If balance is zero then display with "no more withdrawals" */
/*-----------------------------------------------------------*/
printf (" *** Balance is now zero. No more withdrawals can be made at this time. *** ");
number_of_withdrawals = x;
break;
}
/* End Loop */
/*----------*/
}
/* Calculate and display the closing balance */
/*-------------------------------------------*/
printf (" *** The closing balance is $%.2f *** ", 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! *** ");
/* Display the bank statement */
/*----------------------------*/
printf (" *** Bank Record *** ");
printf (" Starting Balance:$ %6.2f ", current_balance);
for (x = 1; x <= number_of_deposits; x++)
{
printf ("Deposit #%i: %13.2f ", x, deposits[x]);
}
for ( x = 1; x<= number_of_withdrawals; x++)
{
printf (" Withdrawal #%i: %10.2f", x, withdrawals[x]);
}
printf (" Ending Balance:$%9.2f ", balance);
//return 0;
system("pause");
/* End Main */
}
Explanation / Answer
A small banking application has been developed which first prompts the user to enter the current balance of his/her bank account (in dollars and cents) and then prompts the user to enter the number of withdrawals to make, and then the number of deposits to make.
The most complicated of the above expected functions is the one which would display the bank record.
void (display_bank_record (float start_balance, float deposits [ ], int num_deposits, float withdrawals [ ], int num_withdrawals, float end_balance)
The same can be implemented using structures in C.
The following structure definition can be incorporated above the main function and an optional structure account can be created using typedef to add more details into the existing account variables.
/*Structure declartion*/
struct display_bank_record
{
float start_balance;
float deposits [5 ],
int num_deposits;
float withdrawals [5 ];
int num_withdrawals;
float end_balance;
};
struct display_bank_record account[20];
printf("The above structure can be declared using
typedef like below");
typedef struct account
{
char bank_name[20];
char bank_branch[20];
char acc_holder_name[30];
int acc_number;
char acc_holder_address[100];
float available_balance;
}Acc_detail;
Acc_detail account[20];
int num_acc;
void display(struct display_bank_record display_record); // write this outside main function
struct display_bank_record display_record; // initialize structure inside main
The void display_bankrecord() function can be created inside the main function and can be instantiated to display the bank record values of the user(i.e the structure is called from the function display)
void display(struct display_bank_record display_record)
{
// Displaying bank record informations
register int num_acc = 0;
while(balance>0)
{
printf(" Start balance : %f " ,
account[num_acc].start_balance);
printf("Deposits : %f " ,
account[num_acc].deposits);
printf("Number of deposits : %d " ,
account[num_acc].num_deposits);
printf("Withdrawals : %f " ,
account[num_acc].withdrawals);
printf("Number of withdrawals : %d " ,
account[num_acc].num_withdrawals);
printf("End balance : %f " ,
account[num_acc].end_balance);
num_acc++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.