Question

Assume you are to write a ledger program that keeps track of each bank transaction as...

Assume you are to write a ledger program that keeps track of each bank transaction as a struct containing the transaction type (deposit or withdrawal), the transaction amount (double), and the balance after the transaction. Would you use an array or a linked list? Why? (in C++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

Thanks for posting an interesting question to decide between array or linked list to keep track of each bank transaction.

1. Answer in short. Linked List.

2. Why? Because to declare and initialize the array we must need to give the size of the array. The problem with the size of the array is explained with the below example:-

For example, Let's suppose I took the size of the array as 1000. After inserting 1000 transaction into the array when we will try to store 1001th transaction then we will get Segmentation fault for the 1001th transaction as the memory was not allocated for the 1001th transaction while creating the array. Hence, we will run into the problem if our no of transactions crossed the upper limit of 1000. So, to overcome this issue we generally use Linked List data strcuture because it is not fixed in size and is a example of dynamic storage of information.

3. In linked list we use node which is having one data part to store the information and the links to point the next node. So for the given question, I have created on structure with the given fields. Two methods are created in the below example. One method for inserting node and second method to display the information present in the linked list. Complete C++ program for linked list implementation to track transactions is mentioned below with proper comments:-

#include<iostream> using namespace std; //Declaring enum to store only two values either deposit or withdrawal enum TRANSACTI

//If head is null it means that this is the first node going to insert into the linked list //In other words it is a first tr

л | un cout<<\n; temp = temp->next; л o л и co //Main method Fint main() { Node *newNode; //Below statement will create fivnewNode->transactionType-Deposit; newNode->transactionAmount=500.00; newNode->balance=28000.00; newNode->next=NULL; insertNod

4. Complete program in text format with proper comments is mentioned below for ready copying:-

#include<iostream>

using namespace std;

//Declaring enum to store only two values either deposit or withdrawal
enum TRANSACTION_TYPE {Deposit, Withdrawal};

//Node type structure
struct Node{
   //Transaction type variable
   TRANSACTION_TYPE transactionType;
   //double transactionAmount
   double transactionAmount;
   //double balance
   double balance;
   //Node type variable to hold the next node
   Node *next;
};

//Node type variable head to hold the first node of the linked list
Node *head=NULL;

//Below method will insert the node newNode into the linked list
void insertNode(Node *newNode){
  
   //Node type temp variable
   Node *temp;
  
   //If head is null it means that this is the first node going to insert into the linked list
   //In other words it is a first transaction so head will point to the newNode
   if(head==NULL)
           head=newNode;
   else{//Otherwise store the head in temp
       temp=head;
       //Iterate till we find last node
       while (temp->next != NULL)
           temp = temp->next;
           //Store the next node to the newNode
           temp->next=newNode;
   }
}

//Below method will display the linked list till the end with each value of the node
void display(){
Node *temp;
temp=head;

while (temp!=NULL) {
   if(temp->transactionType==0){
       cout<<"Deposit";
   }else{
       cout<<"Withdrawal";
   }
cout<<" amount is: "<<temp->transactionAmount<<" and balance is: "<<temp->balance;
   cout<<"\n";
temp = temp->next;
}
}

//Main method
int main(){
  
   Node *newNode;
  
   //Below statement will create five nodes and its properties
   //then inserting all the nodes into the linked list
   newNode=new Node;
   newNode->transactionType=Deposit;
   newNode->transactionAmount=5000.00;
   newNode->balance=50000.50;
   newNode->next=NULL;
   insertNode(newNode);
  
   newNode=new Node;
   newNode->transactionType=Withdrawal;
   newNode->transactionAmount=2000.00;
   newNode->balance=20000.05;
   newNode->next=NULL;
   insertNode(newNode);
  
   newNode=new Node;
   newNode->transactionType=Deposit;
   newNode->transactionAmount=500.00;
   newNode->balance=28000.00;
   newNode->next=NULL;
   insertNode(newNode);
  
   newNode=new Node;
   newNode->transactionType=Deposit;
   newNode->transactionAmount=1600.00;
   newNode->balance=21458.00;
   newNode->next=NULL;
   insertNode(newNode);
  
   newNode=new Node;
   newNode->transactionType=Deposit;
   newNode->transactionAmount=2000.00;
   newNode->balance=70000;
   newNode->next=NULL;
   insertNode(newNode);  

  
   //Displaying the linked list
   display();
}      

5. Please find below output of the program after executing it. Please note that I have saved the file with transactionll at D:/.C. C:\Windows\system32\cmd.exe D:\>g++ transactionll.cpp D:\a.exe Deposit amount is: 5000 and balance is: 50000.5 Withdrawal

6. i) Please note that inside the main method you can call as many as insertNode method as you want to store the transaction information in the given linked list example.

ii) The above program is just a demo to explain the motive which is good array or linked list. Actual requirement may differ from the above example. Important part of the code I marked in bold.

7. Though I put comments to each of the line of the code and tried to explain easily. Still, if you have any query please feel free to reach me through comments section. I will be more than happy to help you further.

8. Please upvote if you liked my answer.

Thanks!!

Happy Learning!!

Add a comment
Know the answer?
Add Answer to:
Assume you are to write a ledger program that keeps track of each bank transaction as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) Write a Java program that creates a double linked list called Log that keeps track...

    1) Write a Java program that creates a double linked list called Log that keeps track of server access: Date and time File accessed Number of hits 2) Write a method that inserts a new element in the list at the end of the list 3) Write a method that return the page that has been access the most (highest number of hits)

  • You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account i...

    You are to write a banking application in c# that keeps track of bank accounts. It will consist of three classes, Account, Bank and BankTest. The Account class is used to represent a savings account in a bank. The class must have the following instance variables: a String called accountID                       a String called accountName a two-dimensional integer array called deposits (each row represents deposits made in a week). At this point it should not be given any initial value. Each...

  • Write a complete Java program called "AtmSimDoLoop" (without the quotation marks) according to the following specifications....

    Write a complete Java program called "AtmSimDoLoop" (without the quotation marks) according to the following specifications. It should use a do-while loop to prompt the user with the following starting prompt (without the horizontal lines) until the user enters 4 to quit. The program should start with an initial account balance, which you can set to any legitimate double value. Prompt the user with the following prompt (without the horizontal lines). Enter the number of your desired transaction type. Deposit...

  • You are to write a Java program using the following instructions to build a bank-ATM server...

    You are to write a Java program using the following instructions to build a bank-ATM server system with sockets. A bank holds accounts that can have deposits, withdrawals, or retrievals of balance. The bank server provides the account for transactions when a client uses and ATM machine. The ATM asks for the type of transaction and the amount (if needed), then creates a socket connection to the bank to send the transaction for processing. The bank performs the transaction on...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • QUESTION 3 The ledger (T-accounts) keeps track of the balance in each account. True False QUESTION...

    QUESTION 3 The ledger (T-accounts) keeps track of the balance in each account. True False QUESTION 4 The process of putting transactions into the books begins with the ledger (T-accounts). True False QUESTION 5 The income statement is a product of accounting and has a timeframe of over a period of time. True False Click Save and Submit to save and submit. Click Save All Answers to save all answers. QUESTION 1 Credit sales are a transaction. True False QUESTION...

  • ****USING PYTHON***** Write a program to simulate a bank transaction. There are two bank accounts: checking...

    ****USING PYTHON***** Write a program to simulate a bank transaction. There are two bank accounts: checking and savings. First, ask for the initial balances of the bank accounts; reject negative balances. Then ask for the transactions; options are deposit, withdrawal, and transfer. Then ask for the account; options are checking and savings. Reject transactions that overdraw an account. At the end, print the balances of both accounts

  • 11.9: Speakers Bureau Write a program that keeps track of a speaker's bureau. The program should...

    11.9: Speakers Bureau Write a program that keeps track of a speaker's bureau. The program should use a structure to store the following data about a speaker: Name Telephone Number Speaking Topic Fee Required The program should use an array of at least 10 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven interface. Input Validation: When...

  • Design a bank account class named Account that has the following private member variables:

    Need help please!.. C++ programDesign a bank account class named Account that has the following private member variables: accountNumber of type int ownerName of type string balance of type double transactionHistory of type pointer to Transaction structure (structure is defined below) numberTransactions of type int totalNetDeposits of type static double.The totalNetDeposits is the cumulative sum of all deposits (at account creation and at deposits) minus the cumulative sum of all withdrawals. numberAccounts of type static intand the following public member...

  • Description For this assignment, you will maintain an array of bank accounts. You will declare a...

    Description For this assignment, you will maintain an array of bank accounts. You will declare a struct type called bankAcct and have an array of bankAcct with maximum size of 20, the members of the struct are shown below struct bankAcct { string first , last ; double amount ; long acctNo ; short pin ; }; Each member is described below • string first stores the user’s first name • string last stores the user’s last name • double...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT