Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will write a C program for working with bank accounts. Your program will use pointers, structs, arrays, and null - terminated

In this assignment, you will write a C program for working with bank accounts. Your program will use pointers, structs, arrays, and null-terminated strings. You should have experience working with these things from CS 2370.
You might need to review some of these topics, and look some things up, but you should write this program without help from the teacher, tutors, IAs, other students, or anyone else. If you are not able to write this program on your own, without getting help from anyone, you are not ready to take this class and you should not stay in the class this term.
As with every programming assignment in this class, make sure that your program compiles and runs on the class server. You won't get any credit for programs that don't run on the server.
Part 1
In a file named account.c implement all of the functions declared in the account.h file. It's important that you understand everything in account.h, so if there is anything in it that you don't understand, look it up. Make sure that your account.c file works with the following file (main1.c) and produces the output shown below.
All amounts and balances should be positive. Amounts for withdrawals should not be greater than the account balance, and amounts for transfers should not be greater than the balance of the source account. If any of these constraints are violated then the transaction should not be executed and the function should return a negative integer as its return value.
The only function that should print anything is account_print.
Use a command line like this to compile your program:
gcc -o main1 main1.c account.c
That command line tells the compiler to compile main1.c and account.c and to make an executable file named main1.
account.h
#include
#define MAX_NAME_LEN (100)
typedef struct {
int id;
int balance;
char owner[MAX_NAME_LEN]; // USED FOR A STRING
} Account;
int account_init(Account *acct_p, int id, int balance, char *owner);
int account_deposit(Account *acct_p, int amount);
int account_withdraw(Account *acct_p, int amount);
int account_transfer(Account *from_p, Account *to_p, int amount);
int account_print(Account *acct_p);
Link to download file: account.hDownload account.h
main1.c
#include
#include "account.h"
int main(int argc, char *argv[]){
Account a1, a2;
account_init(&a1,1,1000, "Hermione Granger");
account_init(&a2,2,500, "Jack Swallow");
account_deposit(&a1,500);
account_withdraw(&a2,50);
account_withdraw(&a1,100);
account_transfer(&a2, &a1,25);
account_print(&a1);
account_print(&a2);
}
Link to download file: main1.cDownload main1.c
Output
In the output below, the dollar sign represents the shell prompt and the name of the executable file is main1
$ ./main1
1 Hermione Granger $1425
2 Jack Swallow $425
Part 2
Write a program that reads account transactions from the standard input stream and then executes those transactions. The main source file (that includes a function named main) for this program should be main2.c and your Makefile for this program should produce an executable named main2
Make an array of Account structs to keep track of the accounts. Define a constant (using #define or the const keyword) named MAX_ACCOUNTS that specifies the maximum number of accounts that can be created.
Format of input lines
Each line of input will have a transaction or a command as the first thing on the line. Items on each input line will be separated by one or more spaces or tabs. Here is a list of account transactions and commands:
new
For a new transaction, your program should create (or initialize) an account. If the maximum number of accounts has already been created then your program should print an error message and not create an account. To create an account, initialize an Account struct in the array, using the account_init function. Your program should assign an account number to the account and pass that number to account_init.
Input lines for new transactions will have the word new, followed by the initial balance of the new account, followed by the first and last names of the account owner. The initial balance will always be an integer. There will always be exactly two names for the account owner.
Here is an example of an input line for a new account:
new 1000 Hermione Granger
The initial balance for the account is $1000. The first name is Hermione and the last name is Granger. Those two names should be combined into one null-terminated string which is passed to account_init.
deposit
For a deposit transaction, your program should get the account number and the deposit amount from the input line, use the account number to get the address of the correct Account struct, and then call account_deposit.
Example:
deposit 1400
withdraw
For a withdraw transaction, your program should get the account number and the withdrawal amou

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions