#include <iostream.h>

/* This program illustrates the use of dynamic allocation in C++ for
   arrays. It is a modification of the bank program, but now asks the
   user how many accounts to create. */

/* Note that all the functions now take a pointer to a float instead of
   an array of float, as well as the array size. */

void Deposit(float*, int);
void Initialize(float*, int);
void Print(float*, int);
float Average(float*, int);

main() {

   int SIZE, account;

   /* Prompt user for size of array */

   cout << "How many accounts: ";
   cin >> SIZE;
   
   /* Use dynamic allocation (with the new command to allocate space for that
      many elements. balances is the address of the first element. */

   float *balances = new float[SIZE];

   /* Initialize all balances to 0. Note that we can treat the pointer like
      an array */

   for (account = 0; account < SIZE; account++) {
     balances[account] = 0;
     }
   
   char option;    /* User input to select option */

   /* Ask user what they want to do */

   cout << "d) Make deposit\np) Print balances\na) Print average\nq) Quit\n"
        << "Enter option: ";
   cin >> option;
   
   /* Loop until the user wants to quit */

   while (option != 'q') {
    
      switch (option) {

         case 'd': 
            Deposit(balances, SIZE);
            break;

         case 'p':
            Print(balances, SIZE);
            break;

         case 'a':
            cout << "The average balance is " 
                 << Average(balances, SIZE) << "\n";
            break;

         default:
            cout << "That is not an option!\n";
         }

      /* Ask user for next option */
      cout << "d) Make deposit\np) Print balances\na) Print average\nq) Quit\n"
           << "Enter option: ";
      cin >> option;

      }

   /* Before exiting, deallocate the memory for the array. */

   delete balances;
   }


/* This function is used to make a deposit to a particular account.
   It takes a float pointer as a parameter, and alters the element of
   the array chosen by the user. Before actually making the deposit, we 
   make sure the index which is actually in the array. */

void Deposit(float *A, int SIZE) {

   int account;   /* User input for random access */
   float deposit;     /* Amount the user wishes to deposit */

   cout << "Enter the account number: ";
   cin >> account;      

   account = account - 1; /* Since the user entered a number between 1 and 10
                             translate it to an index between 0 and 9 */

   if (account >= 0 && account < SIZE) {

      cout << "How much to deposit: ";
      cin >> deposit;

      /* Modify the element of the array with that index */
 
      A[account] = A[account] + deposit;

      }

   else {cout << "No such account!\n"; }

   return;
   }


/* This function uses sequential access to print all balances. It takes
   a float pointer a parameter (which it does not change).
   The loop counter is used as the array index, which means that the
   loop will eventually process all of the elements in the array. Note 
   that we also print the array index to make the output easier to read. */

void Print(float *A, int SIZE) {

   int i;         /* Loop counter for sequential access */
            
   for (i = 0; i < SIZE; i++) {
      cout << "Balance for account " << i+1  /* We print i+1 instead of i */
           << ": " << A[i] << "\n";        /* so the user sees 1-10 */
      }                                      /* instead of 0-9 */
   return;
   }


/* This function uses sequential access to compute the average of elements
   in an array. It takes an array of float as its parameter, and returns the
   average. It keeps a running total of each array element. */

float Average(float *A, int SIZE) {

   int i;         /* Loop counter for sequential access */         
   float total = 0;   /* Running total to compute average */
            
   for (i = 0; i < SIZE; i++) {
      total = total + A[i];
    }
   return total/SIZE;
   }

           
/* This function initializes all elements of the array to zero. */

void Initialize(float *A, int SIZE) {
  int i;
  for (i = 0; i < SIZE; i++) {
    A[i] = 0;
    }
  }
   

