/* This program demonstrates search an parallel arrays. It keeps one array
   of "ID numbers", and another which a balance for each account.
   It prompts the user for an ID number, and searches for the first index
   in the ID array which stores a value equivalent to that number. That
   index is then used to access the corresponding element of the balances
   array (hence, the term "parallel arrays"). */

#include <iostream.h>
#define SIZE 5

void Deposit(int[], float[]);  /* make deposit to an account */
void Print(int[], float[]);  /* print all accounts */
int Search(int[], int); /* search for index of a particular account */

main() {

   /* Declare the arrays. */
   
   float balances[SIZE];
   int IDs[SIZE];

   int account;
   char option;

   /* Initialize the IDs */
   IDs[0] = 8472;
   IDs[1] = 1234;
   IDs[2] = 2525;
   IDs[3] = 7777;
   IDs[4] = 9876;

   /* Initialize all balances to 0 */
   
   for (account = 0; account < SIZE; account++) {
     balances[account] = 0;
     }

   /* Prompt user for whether they want to make a deposit or print 
      all accounts */

   cout << "D)eposit, P)rint, or Q)uit: ";
   cin >> option;

   /* Loop until they quit */

   while (option != 'Q' && option != 'q') {

     switch(option) {
       case 'd': case 'D':
         Deposit(IDs, balances);
         break;
       case 'p': case 'P':
         Print(IDs, balances);
         break;
       default:
         cout << "No such option!\n";
       }

     /* Prompt user for whether they want to make a deposit or print 
        all accounts */

     cout << "D)eposit, P)rint, or Q)uit: ";
     cin >> option;

     }
   }

      
/* This function prints all ids and balances in table form, by printing both
   the id and balance at a particular index. */

void Print(int IDs[], float balances[]) {
   int account;
   cout << "ID:\tBALANCE:\n";  /* print header at top of table */
   for (account = 0; account < SIZE; account++) {
     cout << IDs[account] << "\t" << balances[account] << "\n";
     }
   }

/* This function promts for an id and searches for it in the array of ids.
   IF the id is found, the user is prompted for a deposit to make to the
   corresponding element of the balances array. */

void Deposit(int IDs[], float balances[]) {
   int account, id;
   float dep;

   /* Prompt for the user id */

   cout << "Enter ID of account: ";
   cin >> id;

   /* call the function to search for the corresponding index in the IDs */

   account = Search(IDs, id);

   /* If the id was found, prompt for a deposit and use it to increment
      the corresponding balance. */

   if (account != -1) {

     cout << "Enter deposit: ";
     cin >> dep;

     balances[account] = balances[account] + dep;

     }

   else {
     cout << "No account with that ID!\n";
     }

   }


/* This function searchs the array for the element with the given value, and
   returns its index (or -1 if the value was not found). */

int Search(int IDs[], int id) {
  
  int account;  /* current element of IDs we are examining */

  /* Sequentially examine all elements of IDs, comparing each with id. If they
     are equal, exit the function and return the index where id was found. */

  for (account = 0; account < SIZE; account++) {
    
    if (IDs[account] == id) {
      return account;
      }

    }

  return -1;  /* id not in array */

  }

