In-Class Exercise

Dynamic Memory


This exercise introduces dynamic memory allocation for a user-defined number of elements.  There is no limit on the number that the user selects, except the maximum that the operating system provides. 


Given Information

The following code is a solution to the Handout on Input and Output.

Client Module

h3.h

 // Input and Output
 // h3.h

 #define NO_TRANSACTIONS 3

h3.cpp

 // Input and Output
 // h3.cpp

 #include <iostream>
 using namespace std;
 #include "h3.h"
 #include "Transaction.h"

 int main() {
     Transaction tr[NO_TRANSACTIONS];

     cout << "Enter " << NO_TRANSACTIONS << " Transactions" << endl; 
     for (int i = 0; i < NO_TRANSACTIONS; i++)
         tr[i].enter();
     cout << endl;

     cout << "   Account Description             Credit     Debit" << endl; 
     for (int i = 0; i < NO_TRANSACTIONS; i++) {
         tr[i].display();
         cout << endl;
     }
 }

Transaction Module

Transaction.h

 // Input and Output
 // Transaction.h

 struct Transaction {
   private:
     int acct;
     char type;
     char desc[21];
     double amount;
   public:
     void enter();
     void display() const;
 };

Transaction.cpp

 // Input and Output
 // Transaction.cpp

 #include <iostream>
 #include <iomanip>
 using namespace std;
 #include "Transaction.h"

 void Transaction::enter() {

     cout << "Enter the account number : ";
     cin  >> acct;
     cin.ignore();
     cout << "Enter the desription : ";
     cin.getline(desc, 21);
     cout << "Enter the account type (d for debit, c for credit) : "; 
     cin  >> type;
     cout << "Enter the account amount : ";
     cin  >> amount;
 }

 void Transaction::display() const {

     cout << setw(10) << acct;
     cout << ' ' << setw(20) << left << desc << right;
     cout << setprecision(2) << fixed;
     if (type == 'd')
         cout << setw(20) << amount;
     else
         cout << setw(10) << amount;
 }

Your Task

Client Module

Upgrade the client module to accept a user-specified number of Transactions, to accept the data for each object in turn, and finally to display the data for each object in tabular form: 

  1. include the header file for dynamic memory allocation errors
  2. allocate space for a pointer to a single Transaction
  3. allocate dynamic memory for an array of Transactions that contains the specified number of elements
  4. deallocate the dynamic memory before returning control to the operating system
 // Dynamic Memory
 // h4.cpp


 #include <iostream>
 using namespace std;
 #include "Transaction.h"

 int main() {
     int n; // user-specified number of Transactions
     Transaction       ;

     cout << "Enter the number of Transactions : "; 
     cin  >> n;


     cout << "Enter " <<     << " Transactions" << endl; 
     for (int i = 0; i < n; i++)
         tr[i].enter();
     cout << endl;

     cout << "   Account Description             Credit     Debit" << endl; 
     for (int i = 0; i < n; i++) {
         tr[i].display();
         cout << endl;
     }


 }






  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License