MCC - CIS162AB - C++ Level I
P09 Array and File Processing - 20 points
(turn in source code, input file, and sample output)
   cpp.gif

The purpose of this C++ programming project is to allow the student to perform simple array and file processing. This program has the following five menu options:
  A: Display Employee Id (current order)
  B: Display Employee Id Ascending Order
  C: Display Employee Id Descending Order
  D: Search Employee Id List
  X: Exit the program

Requirements:

1. Create project P09

2. Add text file output.txt

3. Add text file P09.txt
   Enter or copy-and-paste the following data into the text file:

   1018 
   1005 
   1002 
   1004 
   1010 
   1001 
   1007 
   1006 
   1009
   1003 

   Make sure to press enter after the last record, so that EOF test will work
   correctly.

4. Add C++ source file P09.cpp

5. Copy-and-paste the source code provided below.

6. Build the project to verify all of the source was copied correctly.
   There will be some warnings regarding some unreferenced local variables.

7. Add the required code to implement options A through D.
   Code the loadArray, sortArrayAscending, sortArrayDescending, and searchArray functions.
   Consider building the program incrementally.  Code loadArray first, followed by the
   sorts and search.  

8. Generate and capture sample output from each option (A - D)
   For search option (D) include.
      a match found
      a match not found due to early exit
      a match not found because the end of the array was reached

9.  Turn in source code (P09.cpp), P09.txt, output.txt


General Processing:
  The records stored in the file P09.txt are loaded into an
  array.  All of the menu options are implemented by manipulating the array.

  A: Display Employee Id (current order) 
  This option displays the array in its current sort order.
  It is used to display the array right after it is loaded,
  so that the original order of the data can be viewed.


  B: Display Employee Id Ascending Order 
  C: Display Employee Id Descending Order 
  These two options sort the array in the specified order and then displays the arrary.
 
  D: Search Employee Id List 
  Prompt the user to enter an employee id. Create a local int variable to 
  store the search criteria. Only search for exact matches. 
  
  You must include the early exit logic. To facilitate an early exit from the search 
  loop, the array must be sorted by id before beginning the search.  An early exit is 
  when the loop is terminated because it has been determined that the search criteria is
  not in the list. This occurs when the table element is greater than the search criteria.
  
  Display an error message if a match was not found.

  X: Exit the program allows the user to exit.

  
Sample Output 

P09   Your Name Here

Enter the letter of the desired menu option.
Press the Enter key after entering the letter.

  A: Display Employee Id (current order)
  B: Display Employee Id Ascending Order
  C: Display Employee Id Descending Order
  D: Search Employee Id List
  X: Exit the program

Choice: A
Current order of Employee Ids:

position = 1  index = 0  value = 1018
position = 2  index = 1  value = 1005
position = 3  index = 2  value = 1002
position = 4  index = 3  value = 1004
position = 5  index = 4  value = 1010
position = 6  index = 5  value = 1001
position = 7  index = 6  value = 1007
position = 8  index = 7  value = 1006
position = 9  index = 8  value = 1009
position = 10  index = 9  value = 1003

--

Choice: B
Employee Ids sorted ascending:

position = 1  index = 0  value = 1001
position = 2  index = 1  value = 1002
position = 3  index = 2  value = 1003
position = 4  index = 3  value = 1004
position = 5  index = 4  value = 1005
position = 6  index = 5  value = 1006
position = 7  index = 6  value = 1007
position = 8  index = 7  value = 1009
position = 9  index = 8  value = 1010
position = 10  index = 9  value = 1018

--

Choice: C
Employee Ids sorted descending:

position = 1  index = 0  value = 1018
position = 2  index = 1  value = 1010
position = 3  index = 2  value = 1009
position = 4  index = 3  value = 1007
position = 5  index = 4  value = 1006
position = 6  index = 5  value = 1005
position = 7  index = 6  value = 1004
position = 8  index = 7  value = 1003
position = 9  index = 8  value = 1002
position = 10  index = 9  value = 1001

--

Choice: d
Enter Employee Id to search for: 1005
1005 is stored in array position 5 and is
referenced with an index value of 4.

--
Choice: d
Enter a number to search for: 1011
Early exit...1011 is not on the list.

--
Choice: d
Enter a number to search for: 1020
1020 is not on the list.

--
Choice: x
Now exiting. Please wait...


Source Code:

//P09 Array and File Processing - Your Name Here
/*
   This program uses a simple array and a data file.  

   Loads, sorts and searches a partially filled array of Employee ID (integers)

*/
#include <fstream>  // file processing
#include <iostream> // cin and cout

using namespace std;


//Global Constants
//When using to declare arrays, must be defined with const modifier
const int  ARRAY_SIZE = 20; 

//Declare arrays as global so we don't have to pass the arrays to each function.
//Normally we wouldn't declare variables that change values a global.
int  employeeId[ARRAY_SIZE];
int  numberOfEmps;    //count of how many employees were loaded into arrays

void loadArray( );

void sortArrayAscending( );

void sortArrayDescending( );

void displayArray( );

void searchArray( );

void displayContinuePrompt( );


//Program starts here   
void main( )
{
//Declare and initialize local main variables
    char   choice;            //menu option

//Load the arrays with data
    loadArray( );

//check to see what the user wants to do
    do  // while (choice != 'X')
    {
        cout << "P09   Your Name Here   \n\n";

        cout << "Enter the letter of the desired menu option. \n"
             << "Press the Enter key after entering the letter. \n \n"

             << "  A: Display Employee Id (current order) \n"
             << "  B: Display Employee Id Ascending Order \n"
             << "  C: Display Employee Id Descending Order \n"
             << "  D: Search Employee Id List \n"

             << "  X: Exit the program \n \n"

             << "Choice: ";

        cin >> choice;

        choice = toupper(choice);

        switch (choice)
        {
            case 'A':
                cout << "Current order of Employee Ids:\n";
                displayArray( );
                displayContinuePrompt( );
                break;
            case 'B':
                cout << "Employee Ids sorted ascending:\n";
                sortArrayAscending( );
                displayArray( );
                displayContinuePrompt( );
                break;
            case 'C':
                cout << "Employee Ids sorted descending:\n";
                sortArrayDescending( );
                displayArray( );
                displayContinuePrompt( );
                break;
            case 'D':
                searchArray( );
                displayContinuePrompt( );
                break;
            case 'X':
                cout << "Now exiting. Please wait...\n\n";
                break;
            default:
                cout << "\a \n\n Invalid Option Entered - Please try again. \n\n";

        } // end of switch

    } while (choice != 'X');
    
    return;
} // end of main


//Function Definitions
void loadArray( )
{

//Students must remove next line of code and code the rest of the function. 
    cout << "\n   loadArray has not been implemented yet. \n\n\n";


//Open the file for input  
//If there are any errors, 
//  display an error message and return.

//Declare index and initialize to zero for first array element
    int  i = 0;

//Read the first record into array 

//Use while loop to process file, because 
//  while loops handle empty files.

//  increment counter, i++;
//  Check if array size exceeded
//  Read next Id into array
//end while loop
    
//Close the file
    
//Save the number of records loaded
    numberOfEmps = i;

    return;
}


void sortArrayAscending( )
{
    int minIndex, minValue, holdValue;

//Students need to code for-loop to sort employeeId in ascending order 

    return;
}


void sortArrayDescending( )
{
//For a descending sort we still walk through the array, but this
//time we are searching for the highest values.
    int maxIndex, maxValue, holdValue;

//Students need to code for-loop to sort employeeId in descending order 

    return;
}


void displayArray( )
{
    cout << endl;

    for (int i = 0; i < numberOfEmps; i++)
    {
        cout << "position = "; 
        cout <<  (i + 1);

        cout << "  index = "; 
        cout <<  i;

        cout << "  value = ";
        cout <<  employeeId[i] 
             << endl;
    }
    return;
}

//Early exit - an early exit from a search can occur when we know that
//the value we are looking for will not be found in the array.  To
//implement an early exit, the array most be sorted ascending.
//If  we can determine that a value will not be found, we should
//not search the remainder of the array.

void searchArray( )
{ 
//array must be sorted ascending for early exit logic to work  
    sortArrayAscending( ); 

    int  searchId;
    bool numberFound = false;

    cout << "Enter Employee Id to search for: ";
    cin >> searchId;

//Students need to code for-loop to search employeeId 
//  include early exit logic

    return;
}


void displayContinuePrompt()
{
    char prompt;

    cout << "\n\nProcedure completed. Press Enter to continue: ";
    cin.ignore();
    prompt = cin.get();

    system("cls");   

    return;
}
//End of program

Revised: 05/15/2010 - www.mesacc.edu/~marquez/cis162ab/p09_arrays.html