C programming Problem In this assignment you will simulate bank activity by runn
ID: 3758252 • Letter: C
Question
C programming
Problem In this assignment you will simulate bank activity by running basic banking services on a given number of bank accounts. The services involved are represented by letters: B to access the account balance; W to process a withdrawal; D to make a deposit and U to update an account by adding interest to the balance. The interest rate for your bank is fixed at 1.85%. When run, your program should read data from an input file called bankfile.txt to create your bank environment. The input file will have the following format: The first line will be an integer n to indicate the number of bank accounts included in the file The next n lines will be pairs of an integer and a float. These are an account number and the original balance in the account The subsequent lines of data each begin with a character representing the service to be performed. This is followed by an account number (an integer), indicating the account on which the service is to be applied. If the service is W or D, an amount will follow the account number. When the service is W, this amount represents the withdrawal amount. When the service is D, the amount represents the amount of money that should be deposited into the account. You may assume that the account numbers and amounts are valid numbers. However, your program should check that the requested service is legitimate. If it is not, your code should print out an error message. Your program should process each of the requested services one line at a time- as it is read. The data terminates when a line of data is read, in which the service character is Z. When this Z is encountered, your program prints out a summary of the balances in each of the n accounts as shown in the sample output. The implementation of your solution to this assignment should include the following functions: 1. withdraw: This function takes 2 floats. The first represents the current balance of the account, and the second is the amount of money to be withdrawn from the account. The function returns the updated balance after the withdrawal. Note that if the account does not have enough to make the withdrawal, then the balance remains unchanged and an error message is printed 2. deposit: This function takes 2 floats. The first represents the current balance of the account, and the second is the amount of money to be deposited into the account. The function returns the updated balance after the deposit. 3. update: This function takes a single float – the balance in the account. It computes the interest for the account, and updates the account balance by adding the interest to the balance. The function returns the updated balance. 4. getBalance: This function takes an integer representing the account number , an integer array storing account numbers and an array of floats which stores account balance. The function returns the balance in that account. 5. processServices: This function takes three parameters: a file pointer representing the file from which the data is being read, an integer array storing account numbers and an array of floats which stores account balances. The function reads each line requesting service and calls the appropriate function, to get the service rendered to the appropriate account. Objectives: . The purpose of this assignment, is for students to demonstrate their ability to use C syntax and semantics to create user defined functions C syntax to read file input C syntax to format data output C variable declarations to create appropriate variable types to store program data C syntax and semantics to create a program that compiles and runs correctly Comments to annotate code Error handling techniques to build robust code by using correct C syntax and semantics Hint: Parallel arrays may be useful in designing your solution. Submission of Assignment Your assignment should be submitted via Webcourses by the deadline. No assignments will be accepted via email. Your submission should be a single .c file called bank.c . Note that you do not have to submit the input file. Your program will be graded using DevC++, so be sure that your submitted code runs correctly in this IDE. If you do not have DevC++ on your personal machine, you may use a computer in any lab to test your code. Restrictions: Your program should be written to read an input file named bankfile.txt and write output to the screen Remember that the sample input file and screen output are just samples of possible data that your program can be tested on. The actual input file used to run your code will always match the format described in the problem statement, but may be different in content from the sample input file. For example: o There may be more than 3 accounts o different monetary amounts will be used o the order and number of service requests for the accounts can vary Your program MUST include the 5 functions described above. You may include other functions if you wish. Sample input/output Your program should run on any input file that matches the format of the sample bankfile.txt below
3
123 20000.00
467 10000.00
499 2500.00
W 467 2000.00
D 123 50.00
B 123
U 467
W 499 125.00
Z
Explanation / Answer
Sample output:
Code to copy:
#include <stdlib.h> /* atof */
#include<stdio.h> /*printf,fscanf*/
int num_accounts=0;
float withdraw(float,float);
float deposit(float,float);
float update(float);
float getBalance(int ,int[],float[]);
void processServices(FILE *,int[],float[]);
int main(int argc, char *argv[])
{
FILE *fp;
int accounts[100];
float balances[100];
int i;
//open the account file
fp=fopen("bank.txt", "r");
if(fp==NULL)
{
printf(" Unable to open bank.txt");
system("pause");
exit(0);
}
printf("***************Bank Statement****************** ");
//read number of accounts
fscanf(fp, "%d ", &num_accounts);
//read each account and its initial balance
for( i=0;i<num_accounts;i++)
{
fscanf(fp, "%d%f ", &accounts[i], &balances[i]);
}
processServices(fp,accounts,balances);
return 0;
}//end of main
void processServices(FILE * fpointer,int accounts[],float balances[])
{
char serv_code;
int account;
float amount;
//read until end of the file
while (!feof(fpointer))
{
fscanf(fpointer, "%c",&serv_code);
//if the transaction is withdrawal
if(serv_code=='W')
{
fscanf(fpointer, "%d%f ", &account, &amount);
int i=0;
for(i=0;i<num_accounts;i++)
{
if(account==accounts[i])
break;
}
if(balances[i]-amount >20)
balances[i]=withdraw(balances[i],amount);
else
printf(" Error: insufficient funds ");
}
//if the transaction is deposit
else if(serv_code=='D')
{
fscanf(fpointer, "%d%f ", &account, &amount);
int i=0;
for(i=0;i<num_accounts;i++)
{
if(account==accounts[i])
break;
}
balances[i]=deposit(balances[i],amount);
}
//if the transaction is update
else if (serv_code=='U')
{
fscanf(fpointer, "%d ", &account);
int i=0;
for(i=0;i<num_accounts;i++)
{
if(account==accounts[i])
break;
}
balances[i]=update(balances[i]);
}
//if the transaction is balance
else if (serv_code=='B')
{
fscanf(fpointer, "%d ", &account);
printf("The current balance in the account %d is %f",account,getBalance(account,accounts,balances));
}
else if(serv_code=='Z')
{
printf(" ***Accounts summary*** ");
int i;
printf(" A/c.No balance ");
printf(" --------------- ");
for(i=0;i<num_accounts;i++)
{
printf(" %d %f ",accounts[i],balances[i]);
}
break;
}
}
}//end of processServices();
float withdraw(float cur_balance,float amount)
{
return cur_balance-amount;
}
float deposit(float cur_balance,float amount)
{
return cur_balance+amount;
}
float update(float cur_balance)
{
return (float)cur_balance+((cur_balance*1.85)/100);
}
float getBalance(int account,int accounts[100],float balances[100])
{
int i=0;
for(i=0;i<num_accounts;i++)
{
if(account==accounts[i])
break;
}
return balances[i];
}
Note: don't forget to copy bank.txt to your project's folder.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.