/* This program is meant to demonstrate function calls with arrays. Keep in mind 
   that all arrays are automatically passed by reference, so when an array is
   changed in a function, the array passed to it in the call is also changed. */

/* We also use this program to demonstrate how array indices may be 
   manipulated to appear to the user to start at 1 instead of 0. */

#include <iostream.h>

/* Use a defined constant to set the size of the array. This will make it easy to 
   change that size in the future. */

#define SIZE 10

void Deposit(float[]);
void Initialize(float[]);
void Print(float[]);
float Average(float[]);

main() {
   
   float balances[SIZE] ={0};  /* Array of 10 balances, all initialized to 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) {

         /* When we pass an array to a function, just use the name of the
            array in the call. */

         case 'd': 
            Deposit(balances);
            break;

         case 'p':
            Print(balances);
            break;

         case 'a':
            cout << "The average balance is " << Average(balances) << "\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;

      }
   }


/* This function is used to make a deposit to a particular account.
   It takes an array of float 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 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
   an array of floatss as 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 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 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 i;
  for (i = 0; i < SIZE; i++) {
    A[i] = 0;
    }
  }
   

