Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Construct a C program which reads the details of a monthly checking account and

ID: 672427 • Letter: C

Question

Construct a C program which reads the details of a monthly checking account and outputs a bank statement summarizing these transactions. An input file, account.txt, contains a list of transactions for the checking account for one month. Each line of the input file consists of a one character transaction code along with a double containing the amount of the transaction. Valid transaction codes are as follows:

• I – initial balance brought forth from previous month

• D - deposit

• C - check cashed

As each transaction is entered, you should output on a single line the type of transaction, the amount of the transaction (separate columns for deposits and checks cashed), and the balance after the transaction has been processed:

Transaction          Deposit        Check Cashed            Balance

-----------              ------            ------------                  -------

Initial Balance                                                            478.83

Deposit                 127.45                                           606.28

Deposit                 619.84                                           1226.12

Check                                                 945.12              281.00

Check                                                   4.76                     276.24

...

The banks service charges (which should be deducted from the balance at the end of the month) are as follows:

• $3.00 per month to maintain the account.

• $0.06 for each check cashed.

• $0.03 for each deposit made.

• $5.00 overdraft whenever a check cashed brings the balance below $0.00. (Note: Do not assess the service charge when the transaction is a deposit into an overdrawn account that does not bring the balance above zero.

The bank statement should also include the following summary information:

Total number deposits:

Total amount deposits:

Total number checks:

Total amount checks:

Total service charge:

Opening balance:

Closing balance:

Shown below is the main () function that you should use for your program:

int main(void) {

char code;

double amount, service, balance;

double amtCheck, amtDeposit, openBalance, closeBalance;

int numCheck, numDeposit;

if ( ! (fp = fopen("account.txt", "r") ) ) {

printf("account.txt could not be opened for input. ");

exit(1); }

if ( ! ( csis = fopen("csis.txt", "w") ) ) {

printf("csis.txt could not be opened for output. ");

exit(1); }

amount = 0.0;

service = 0.0;

balance = 0.0;

amtCheck = 0.0;

amtDeposit = 0.0;

openBalance = 0.0;

closeBalance = 0.0;

numCheck = 0.0;

numDeposit;

outputHeaders();

while ( ! feof(fp) ) {

fscanf (fp, " %c %lf ", &code, &amount);

if ( code == 'I' ) {

initialBalance ( amount, &balance, &service, &openBalance); }

else if ( code == 'D' ) {

deposit ( amount, &balance, &service, &numDeposit, &amtDeposit); }

else {

check ( amount, &balance, &service, &numCheck, &amtCheck); }

}

closeBalance = balance - service;

outputSummary(numDeposit, amtDeposit, numCheck, amtCheck, openBalance, service, closeBalance);

fclose(csis);

fclose(fp);

return 0; }

Write 5 functions and their prototypes for your program:

outputHeaders()

initialBalance()

deposit()

check()

outputSummary()

These functions will use counters and summations to keep track of the different components of the checking account as well as output specific information relating to the bank statement. Note that some of the function parameters are passed as input parameters and some are passed as input-output parameters. Be sure to use the indirection ( * ) correctly in your functions.

The contents of account.txt are shown below:

I 478.83

D 127.45

D 619.84

C 945.12

C 4.76

D 32.81

C 1.06

D 184.32

You should not be using any global variables, other that declarations for the file pointers, in your program other than file pointers. A global variable is a variable declared outside of main() . Please do not modify the main() function tha is provided above.

Output from program should be sent to the terminal window as well as the requested csis.txt output file.

Please use code that is simple or for begginers in c programming. No arrays. Thank you.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
const char *label[] =
{
"Initial Balance",
"Deposit",
"Cheque cashed",
"Service Fee",
};
void read_transactions(const char *szFilename)
{
FILE *fp = fopen(szFilename, "rt");
char code = 0;
float amount = 0.0f;
float balance = 0.0f;
int iArray[3] = {0};
float serviceCharge = 3.0f;
int i = 0;
if (!fp) {
printf("Couldn't open file [%s]", szFilename);
return;
}
fprintf(stdout, "%-16s Amount ", "Transaction");
fprintf(stdout, "---------------- ---------- ");
while(!feof(fp))
{
fscanf(fp, "%c %f ", &code, &amount);
if (code == 'I')
{
balance = amount;
fprintf(stdout, "%-16s %10.2lf ", label[0], amount);
++iArray[0];
}
else if (code == 'D')
{
balance += amount;
fprintf(stdout, "%-16s %10.2lf ", label[1], balance);
++iArray[1];
}
else if (code == 'C')
{
balance -= amount;
if (balance < 0.0f)
serviceCharge += 5.0f;
fprintf(stdout, "%-16s %10.2lf ", label[2], balance);
++iArray[2];
}
}
fprintf(stdout, "---------------- ---------- ");
fprintf(stdout, "%-16s % 10d = %8.2f ", "Deposits", iArray[1], iArray[1] * 0.03);
fprintf(stdout, "%-16s % 10d = %8.2f ", "Checks Cashed", iArray[2], iArray[2] * 0.06);
serviceCharge += iArray[1] * 0.03;
serviceCharge += iArray[2] * 0.06;
fprintf(stdout, "%-16s %10.2f ", "Service Charge", serviceCharge);
fprintf(stdout, "%-16s %10.2f ", "Opening Balance", amount);
fprintf(stdout, "%-16s %10.2f ", "Closing Balance", balance - serviceCharge);
fclose(fp);
}
int main()
{
read_transactions("Account.txt");
return 0;
}