/* This program is meant to demonstrate linear search in arrays.  
   The user has the ability to search for the first account with a 
   balance >= some limit */

#include <iostream.h>


#define SIZE 10

void Deposit(float[]);
void Initialize(float[]);
void Print(float[]);
float Average(float[]);
int SearchLimit(float[], float);  /* search for first account >= some limit */

main() {
   
   float balances[SIZE] ={0};  /* Array of 10 balances, all initialized to 0 */
   
   char option;    /* User input to select option */
   float limit;
   int account;

   /* Ask user what they want to do */

   cout << "d) Make deposit\np) Print balances\na) Print average\n"
        << "s) search balances for account >= some limit\nq) Quit\n"
        << "Enter option: ";
   cin >> option;
   
   /* Loop until the user wants to quit */

   while (option != 'q') {
    
      switch (option) {

         case 'd': 
            Deposit(balances);
            break;

         case 'p':
            Print(balances);
            break;

         case 'a':
            cout << "The average balance is " << Average(balances) << "\n";
            break;

         case 's':    /* user wants to search */
            cout << "Enter limit: ";
            cin >> limit;  /* get limit from user */

            /* Call function to find first balance >= that limit. This function
               returns the index number where that balance was found */

            account = SearchLimit(balances, limit);

            /* Make sure that such an account exists. If it does, print
               its location. */

            if (account != -1) {
              cout << "Account " << account+1 
                   << " has a balance of at least " << limit << "\n";
              }
            else {
              cout << "No account has a balance of " << limit << "\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\n"
          << "s) search balances for account >= some limit\nq) Quit\n"
          << "Enter option: ";
      cin >> option;

      }
   }


/* This function takes an array and a limit, and returns the index of the
   first element with a value >= that limit, or -1 if no such elements
   exist. */

int SearchLimit(float B[], float limit) {

   int account;  /* current element we are examining */

   /* This loop sequentially examines all elements of the array for one
      which is >= the limit. If one is found, we return that index (which
      exits both the loop and the function). */

   for (account = 0; account < SIZE; account++) {

      if (B[account] >= limit) {
        return account;
        }

      }

   /* If we reach this point in the function (which would happen only if we
      ran through the entire for loop without returning), it means that no 
      elements in the array meet the criteria. In that case, return -1
      to indicate that nothing was found. */

   return -1;

   }

/* 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;
    }
  }
   

