Nandomo -3 Junior Poster

Ok I got my program running, finally ^.^ Compiling without errors But I want the looping for the binary search to work. How do I get it to loop without sending back a return until all 5 numbers are done searching.

#include <cstdlib>
#include <iostream>
#include <fstream>


using namespace std;

// Size of each combination including 0
const int Rand_SIZE = 5;
// Number of combinations including 0
const int Comb_SIZE = 10;

// This holds the numbers from the input file
    struct comboNums
    {
         int number[Rand_SIZE];
    };


// Prototypes
void selectionSortArray(int [], int); 
void checkForDuplicate(int [], int);
int liSearchList( int [], int, int []);
int binarySearch(int [],int ,int []);

int main()
{

// Naming variable for struct
    comboNums luckyNum[Comb_SIZE];
// The Random number in memory
    int theNumbers[Rand_SIZE],
        found1[Comb_SIZE],
        found2[Comb_SIZE];
    
    srand (time(NULL) );  
// Creating the Random numbers in memory
    for (int i=0; i < Rand_SIZE; i++) 
    {
        theNumbers[i] = rand() % 50 + 1;
            
    }

// Sending to check for duplicates
   checkForDuplicate(theNumbers, Rand_SIZE);

// Sorting numbers using selection sort
   selectionSortArray(theNumbers,Rand_SIZE);
   
// This displays the winning numbers.
   cout << "These are the winning numbers: ";
   for (int i = 0; i < Rand_SIZE; i++)
   {

       cout << theNumbers[i];
       if ( i < Rand_SIZE - 1)
          cout << "-";
   }
   endl(cout);
   endl(cout);
// Opening up input file
   ifstream myfile;
   myfile.open ("luckyNumbers.txt");
// Checking if file opens
   if (myfile.is_open())
   {
// Looping to capture all 10 combinations
   for (int a = 0; a < Comb_SIZE; a++)
   {
       for (int b = 0; b < Rand_SIZE; b++)
       {
// Allocating numbers from input file in proper slots
              myfile >> luckyNum[a].number[b];
              
       }    
       
   }

// Looping for display
   for (int b = 0; b < Comb_SIZE; b++)
   {
              cout << "Combination " << b+1 << " of five numbers is: ";
// Displaying numbers from input file
       for (int a = 0; a < Rand_SIZE; a++)
       {
           cout << luckyNum[b].number[a];        
       }      
       endl(cout);
   }
      
// Closing Input File
   myfile.close();
   }
// Just in case file doesn't open
   else cout << "Unable to open file"; 
   
   endl(cout);
// Beginning the linear search
   cout << "We will begin the linear and binary searches... " << endl; 
   endl(cout);
// Sending to linear and binary searches
//
// Loop Sending 10 combinations
   for (int j = 0; j < Comb_SIZE; j++)
   {
// Loop Sending 5 numbers
              
                  found1[j] = liSearchList(theNumbers, Rand_SIZE, luckyNum[j].number);
                  found2[j] = binarySearch(theNumbers, Rand_SIZE, luckyNum[j].number);
                
// Checking for winners
                  if (found1[j] == -1)
                  {
                      cout << "Linear Search: " << endl;
                      cout << "Combination " << j+1 << " is not a winner" << endl;
                      endl(cout);
                  }
                  else 
                  {
                      cout << "Linear Search: " << endl;
                      cout << "Combination " << j+1 << " is a winner" << endl;
                      endl(cout);
                  }
              
                  if (found2[j] == -1)
                  {
                      cout << "Binary Search: " << endl;
                      cout << "Combination " << j+1 << " is not a winner" << endl;
                      endl(cout);
                  }
                  else 
                  {
                      cout << "Binary Search: " << endl;
                      cout << "Combination " << j+1 << " is a winner" << endl;
                      endl(cout);
                  }   
              
   }
   
   
   
   
   
   system("pause");
   return 0;
}

//******************************************************************
//                      selectionSortArray
//
// task:		  to sort values of an array in ascending order
// data in:       the array, the array size
// data out:      the sorted array
//
//******************************************************************


void selectionSortArray(int array[], int elems)
{
	int seek;      //array position currently being put in order
	int minCount;  //location of smallest value found
	int minValue;  //holds the smallest value found
	
	for (seek = 0; seek < (elems-1);seek++)  // outer loop performs the swap
		                                   // and then increments seek
	{
		minCount = seek;
		minValue = array[seek];
		for(int index = seek + 1; index < elems; index++) 
            {
	      	  // inner loop searches through array     
			  // starting at array[seek] searching
			  // for the smallest value. When the 
			  // value is found, the subscript is
			  // stored in minCount. The value is    
			  // stored in minValue.
		      
			    if(array[index] < minValue)
			    {
                    minValue = array[index];                                                     
				    minCount = index;       
                }
            }

			// the following two statements exchange the value of the
			// element currently needing the smallest value found in the 
		    // pass(indicated by seek) with the smallest value found
		    // (located in minValue)

		array[minCount] = array[seek];  
		array[seek] = minValue;

	}
}

//******************************************************************
//                      checkForDuplicate
//
// task:		  to check for values that might be duplicates 
//                and make new ones in its place
// data in:       the array, the array size
// data out:      the array with any replacements, if needed
//
//******************************************************************
	
void checkForDuplicate(int numbers[], int size)
{
// Probably someway to make this more efficent but you get the idea 

// Naming Subscripts
  int i, j; 

// Looping to compare
  for(i = 0; i < size; i++)
  {
     for(j = i + 1; j < size; j++)
     {
// if duplicate replace with new number
       if(numbers[i] == numbers[j])
       {
         numbers[j] = rand() % 50 + 1;         
       }
     }
  }
}

//********-***********************************************************
//                      liSearchList
//
// task:	      This searches an array for a particular value
// data in:       List of values in an array, the number of 
//                elements in the array, and the value searched for
//                in the array
// data returned: Position in the array of the value or -1 if value
//                not found
//
//*******************************************************************

int liSearchList( int List[], int numElems, int value[])
{
               
	for (int count = 0;count < numElems; count++)  
	{
		if (List[count] == value[count])
        {              // each array entry is checked to see if it contains
	                  // the desired values
         if (count == numElems - 1)
         return 1;
         else 
         continue;
                     // if the desired value is found, the linear search
			         // will continue until the end
        }    
	}
	return -1;	     // if the value is not found, -1 is returned
}

//*******************************************************************
//                     binarySearch
//
// task:		  This searches an array for a particular value
// data in:       List of values in an orderd array, the number of 
//                elements in the array, and the value searched for
//                in the array
// data returned: Position in the array of the value or -1 if value
//                not found
//
//*******************************************************************
int binarySearch(int array[],int numElems,int value[]) //function heading
{
	int first = 0;				    // First element of list
	int last = numElems - 1;	    // last element of the list
	int middle;					    // variable containing the current 
	                                // middle value of the list
	for (int i = 0; i < numElems; i++)
	{
	    while (first <= last)
	    {
		      middle = first + (last - first) / 2; 
		   
	    if (array[middle] == value[i])
		   continue;		           // if value is in the middle, we are done
	                                 
        else if (array[middle] < value[i])
		     last = middle - 1;		   // toss out the second remaining half of
								       // the array and search the first 
	    else
	    	first = middle + 1;		   // toss out the first remaining half of
             
        					   // the array and search the second
      	}
    }
	return -1;					   // indicates that value is not in the array
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.