Parallel Arrays

Discussion in 'C' started by Rdrnnr62, Nov 4, 2006.

  1. Rdrnnr62

    Rdrnnr62 New Member

    Joined:
    Nov 4, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Need help loading parallel arrays from a data file. My display is skipping the first 27 lines and starting from element [28]. The search methods are working fine until I search for an element that has not been loaded. Please help.
    Code:
    #include <fstream>
    using namespace std;
    class EmployeeData
    {
    	private:
    		int empID [200];
    		double empPayRate [200];
    		int empCt;
    		int index;
    		double payRate;
    		int found;
    		int mid;
    		int seqCt;
    		int binCt;
    
    
    	public:
    		EmployeeData();
    		void loadArrays();
    		void printArray();
    		int seqSearch(int);
    		int binSearch(int);
    		void displaySeqSearchResults();
    		void displayBinSearchResults();
    };
    
    
    
    EmployeeData::EmployeeData()
    {
    	empCt = 0;
    	seqCt = 0;
    	binCt = 0;
    	payRate = 0.0;
    	for(int empCt = 0; empCt < 200; ++empCt) 
    	{
    		empID [index] = 0;
    	
    	}
    }
    
    void EmployeeData::loadArrays()
    {
    	ifstream employee;
    
    	employee.open("employee.dat");
    	if(employee.is_open())
    	{
    		employee       >>	empID[empCt]
    			     >>	empPayRate[empCt];
    					
    
    		while(!employee.eof())
    		{
    			index = empCt;
    			empCt = empCt + 1;
    			employee	    >>	empID[empCt]
    				    >>	empPayRate[empCt];
    						
    		}
    		employee.close();
    	}
    
    	else
    	{
    		empCt = -1;
    		Console::WriteLine("\nFile Failed To Open");
    	}
    }
    int EmployeeData::seqSearch(int reqEmpID)
    {
    	found = -1;
    	index = 0;
    	seqCt = 0;
    
    	for(index = 0; index < empCt; ++index)
    	{
    		++seqCt;
    
    		if(reqEmpID == empID[index])
    		{
    			found = empID[index];
    			payRate = empPayRate[index];
    			return found;
    		}
    	}
    	return found;
    }
    int EmployeeData::binSearch(int reqEmpID)
    {
    	int first = 0;
    	int last = empCt - 1;
    	int found = 0;
    
    	while(first <= last && found == 0)
    	{
    		mid = (first + last) / 2;
    		++binCt;
    
    		if(empID[mid] == reqEmpID)
    		{
    			found = 1;
    			payRate = empPayRate[mid];
    			mid = empID[mid];
    		}
    
    			if(empID[mid] < reqEmpID)
    			{
    				first = mid + 1;
    			}
    			else
    			{
    				last = mid - 1;
    			}
    		if(found == 0)
    		{
    			mid = -1;
    		}
    		else
    		{
    			return mid;
    		}
    		
    	}
    }
    void EmployeeData::displaySeqSearchResults()
    {
    	if(found == -1)
    	{
    		Console::Write("\nSequential could not find employee.");
    	}
    	else
    	{
    		Console::WriteLine("\nSequential found employee {0} and the pay Rate is {1}",
    			found.ToString("D0"), payRate.ToString("C2"));
    		Console::WriteLine("\nSequential took {0} searches to find employee.",
    			seqCt.ToString("D0"));
    	}
    	
    }
    void EmployeeData::displayBinSearchResults()
    {
    	if(mid == -1)
    	{
    		Console::Write("\nBinary could not find employee.");
    	}
    	else
    	{
    		Console::WriteLine("\nBinary found employee {0} and the pay rate is {1}",
    			mid.ToString("D0"), payRate.ToString("C2"));
    		Console::WriteLine("\nBinary took {0} searches to find employee.",
    			binCt.ToString("D0"));
    	}
    }
    void EmployeeData::printArray()
    {
    	while(index < empCt)
    	{
    		Console::WriteLine("\nEmployee #{0} Pay Rate {1} ",
    			empID[index].ToString("D0"), empPayRate[index].ToString("C2"));
    		++index;
    		
    	}
    }
     
    Last edited by a moderator: Nov 6, 2006

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice