Okay. I have a program I am writing similar to the lottery, my input file provides 10 combinations and my rand() produces 5 random numbers which I want to compare them against using binary and linear search. I know how to compare them 1 number at a time, but don't know how to compare them combination against combination, anyone want to help?

Input file

11-18-20-24-25
8-10-23-32-36
1-6-12-18-34
23-29-31-32-34
1-15-17-23-32
4-6-13-25-27
8-9-26-29-34
14-17-19-24-30
1-8-25-28-29
13-17-24-29-33

Source file

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

using namespace std;

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

void selectionSortArray(int [], int); 

int main()
{
// This holds the numbers from the input file
    struct comboNums
    {
         int number1;
         int number2;
         int number3;
         int number4;
         int number5;
    };
// Naming variable for struct
    comboNums luckyNum[Comb_SIZE];
// The Random number in memory
    int theNumbers[Rand_SIZE];

   srand ( time(NULL) );
   
// Looping to create 5 "lucky" numbers
   for (int i = 0; i < Rand_SIZE; i++)
   {
// The random number from 1-50
       theNumbers[i] = rand() % 50 + 1;          
   }
   
// 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++)
   {
// Allocating numbers from input file in proper slots
              myfile >> luckyNum[a].number1;
              myfile >> luckyNum[a].number2;
              myfile >> luckyNum[a].number3;
              myfile >> luckyNum[a].number4;
              myfile >> luckyNum[a].number5;
              
   }
   
   for (int b = 0; b < Comb_SIZE; b++)
   {
              cout << "Combination " << b+1 << " of five numbers is: ";
// Displaying numbers from input file
              cout << luckyNum[b].number1;
              cout << luckyNum[b].number2;
              cout << luckyNum[b].number3;
              cout << luckyNum[b].number4;
              cout << luckyNum[b].number5;
              endl(cout);
   }
      
// Closing Input File
   myfile.close();
   }
// Just in case file doesn't open
   else cout << "Unable to open file"; 
   
// Beginning the linear search
   cout << "We will begin the linear search... " << endl; 
   
   
   
   
   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;

	}
}

Recommended Answers

All 22 Replies

I like your idea for your program. Interesting concept !-)
My queston to you:
How do you want to compare them? (I mean 'in what way'). Like: is 5-17-2 equal to 17-5-2?

An idea for the numbers is:
Sort them first. Then, add them in one big integer. Like:
5-17-03 -> 051703. You can easily compare them to another number like 170503...
You can also use binaries, so it'll be even easier to shift with bits.

Good luck,
Cheers!

They are all sorted if you noticed. selectionSortArray(theNumbers,Rand_SIZE); Function does that. I would like to know how to run the checking effectively... Less lines is better for me. Because the idea I have requires too many lines and would be epic.

Aah, So it's the sorting that nerds to shrink. OK let's see.
I think what you're doing is already quite effucient.

When I look at your code, you do the following:
Generate numbers,
Sort them,
Open the file,
Get the numers in your array,
Close the file.

Shouldnt the numbers in your file be sorted as well? Otherwise, it won't be likely you'll end up with a match.
Ill help you with the sorting tomorrow, I'm on my iPod right now, so it's impossible to code ;)

I see. The numbers inputed are already in order so no need for sorting. And I sort of got an idea how to do the linear search just need help passing the struct, since I don't know how to do it.

#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;

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

int main()
{
// This holds the numbers from the input file
    struct comboNums
    {
         int number1;
         int number2;
         int number3;
         int number4;
         int number5;
    };
// Naming variable for struct
    comboNums luckyNum[Comb_SIZE];
// The Random number in memory
    int theNumbers[Rand_SIZE],
        found1[Rand_SIZE],
        found2[Rand_SIZE],
        found3[Rand_SIZE],
        found4[Rand_SIZE],
        found5[Rand_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++)
   {
// Allocating numbers from input file in proper slots
              myfile >> luckyNum[a].number1;
              myfile >> luckyNum[a].number2;
              myfile >> luckyNum[a].number3;
              myfile >> luckyNum[a].number4;
              myfile >> luckyNum[a].number5;
              
   }

// Looping for display
   for (int b = 0; b < Comb_SIZE; b++)
   {
              cout << "Combination " << b+1 << " of five numbers is: ";
// Displaying numbers from input file
              cout << luckyNum[b].number1;
              cout << luckyNum[b].number2;
              cout << luckyNum[b].number3;
              cout << luckyNum[b].number4;
              cout << luckyNum[b].number5;
              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 search... " << endl; 
   endl(cout);
   
   for (int i = 0; i < Rand_SIZE; i++)
   {
       found1= liSearchList(theNumbers, Rand_SIZE, luckyNum[0]);
       found2= liSearchList(theNumbers, Rand_SIZE, luckyNum[1]);
       found3= liSearchList(theNumbers, Rand_SIZE, luckyNum[2]);
       found4= liSearchList(theNumbers, Rand_SIZE, luckyNum[3]);
       found5= liSearchList(theNumbers, Rand_SIZE, luckyNum[4]);
   
   }
   
   if (found1 == -1)
      cout << "Combination 1 is not a winner" << endl;
   else 
      cout << "Combination 1 is a winner" << endl;
   
   if (found2 == -1)
      cout << "Combination 2 is not a winner" << endl;
   else 
      cout << "Combination 1 is a winner" << endl;
      
   if (found3 == -1)
      cout << "Combination 3 is not a winner" << endl;
   else 
      cout << "Combination 1 is a winner" << endl;
      
   if (found4 == -1)
      cout << "Combination 4 is not a winner" << endl;
   else 
      cout << "Combination 1 is a winner" << endl;
   
   if (found5 == -1)
      cout << "Combination 5 is not a winner" << endl;
   else 
      cout << "Combination 1 is a winner" << endl;
   
   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, struct comboNums 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 value.
		 continue;
                     // if the desired value is found, the array subscript
			         // count is returned to indicate the location in the array
	}
	return -1;	     // if the value is not found, -1 is returned
}

I know the prototype is wrong...

You need to take the struct definition on lines 1 to 28 out of main and stick it above the first function declaration that uses it on line 16.

If you change the struct to an array, it might also be easier:

struct comboNums
{
    int numbers[5];
};

That way you can very easily use your existing function to sort it with no changes at all:

comboNums combo;
// fill in combo with whatever
selectionSortArray(combo.numbers, 5); // array is now sorted

You need to decide what makes one comboNums object greater than, equal to, or less than another. Something like this:

// true if comb1 == comb2, false otherwise
bool equalTo(comboNums comb1, comboNums comb2);
// true if comb1 < comb2, false otherwise
bool lessThan(comboNums comb1, comboNums comb2);
// true if comb1 > comb2, false otherwise
bool greaterThan(comboNums comb1, comboNums comb2);

Or write them as operators.

Call them like this:

comboNums comb1, comb2;
// fill them in and sort them.
if(greaterThan(comb1, comb2))
{
    // do whatever
}

But struct declarations should go at the top, not within main. And this is C++, not C, so you don't have to keep that keyword "struct" in there:

int liSearchList( int List[], int numElems, struct comboNums value[]);

You can delete the word "struct" and change it to this:

int liSearchList( int List[], int numElems, comboNums value[])

You need to decide what makes one comboNums object greater than, equal to, or less than another. Something like this:

C++ Syntax (Toggle Plain Text)
// true if comb1 == comb2, false otherwise
bool equalTo(comboNums comb1, comboNums comb2);
// true if comb1 < comb2, false otherwise
bool lessThan(comboNums comb1, comboNums comb2);
// true if comb1 > comb2, false otherwise
bool greaterThan(comboNums comb1, comboNums comb2);

Or write them as operators.

Call them like this:
C++ Syntax (Toggle Plain Text)
comboNums comb1, comb2;
// fill them in and sort them.
if(greaterThan(comb1, comb2))
{
// do whatever
}

Don't understand the logic behind this. What is that supposed to do and I don't have to sort the combo numbers, they are already inputed in ascending order.

Don't know how to edit previous post... v.v Ok I got my search methods in my code, just don't know how to pass the information to them. Help please.

#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][Rand_SIZE],
        found2[Comb_SIZE][Rand_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 < Comb_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 < Comb_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 search... " << endl; 
   endl(cout);
// Sending to linear search
//
// 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 << "Combination " << j+1 << " is not a winner" << endl;
                      break;
                  }
                  else 
                      cout << "Combination " << j+1 << " is a winner" << endl;
                      
              
                  if (found2[j] == -1)
                  {
                      cout << "Combination " << j+1 << " is not a winner" << endl;
                      break;
                  }
                  else 
                      cout << "Combination " << j+1 << " is a winner" << endl;
                      
              
   }
   
   
   
   
   
   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
         return 1;
                     // 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
	
	while (first <= last)
	{
		middle = first + (last - first) / 2; 
		   
	if (array[middle] == value)
		return middle;		       // if value is in the middle, we are done
	                                 
	else if (array[middle]<value)
		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
}

>> Don't understand the logic behind this. What is that supposed to do and I don't have to sort the combo numbers, they are already inputed in ascending order.

>> Don't know how to edit previous post... v.v Ok I got my search methods in my code, just don't know how to pass the information to them. Help please.

You have this thread marked as solved. Is it?

Regarding the first question, you said you were trying to edit it, so I don't know if you still have that question. I possibly misunderstood what you were searching for in the searches and the sorts. If we're on two completely different pages, my comments in the last post may be irrelevant.

int liSearchList( int List[], int numElems, int value[])

This function only returns -1 or 1, so it seems wrong.

Anyway, if the thread truly isn't solved, mark it unsolved please. Your functions only take integer arrays. Not sure if you want them to take struct arrays or if you like them as they are. If the question is how to pass an array of integers to a function, it looks like you already do that correctly in line 48 and elsewhere.

Okay, I have learned to transfer my numbers from function to function how I want them. But haven't been able to get my linear and binary searches to work. *I blocked out the random generators to hard input a working combo* And it still doesn't work. Any ideas?

#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] = {1,15,17,23,32},
        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] < 5)
                  {
                      cout << "Linear Search: " << endl;
                      cout << "Combination " << j+1 << " is not a winner" << endl;
                      endl(cout);
                  }
                  else if (found2[j] == 5)
                  {
                      cout << "Linear Search: " << endl;
                      cout << "Combination " << j+1 << " is a winner" << endl;
                      endl(cout);
                  }
              
                  if (found2[j] > 10)
                  {
                      cout << "Binary Search: " << endl;
                      cout << "Combination " << j+1 << " is not a winner" << endl;
                      endl(cout);
                  }
                  else if (found2[j] == 10)
                  {
                      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[])
{   
    int found[Rand_SIZE];
    
	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
         
         found[count] = 1;
                     // if the desired value is found, the linear search
			         // will continue until the end
			         if (count == 4)
			         return found[0]+found[1]+found[2]+found[3]+found[4];
        }   
	}

return -1;
}

//*******************************************************************
//                     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])
		 return middle;		       // 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
}

Lines 260 to 271. I think this is what confused me originally when I posted a while back.

Here's the spec:

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

Here's the definition:

int binarySearch(int array[],int numElems,intvalue[]) //function heading

Note bold red added emphasis. The spec says that you are searching ONE array for ONE value. But you are passing two arrays to the function, plus an array size?

I think the spec needs to be clearer and the variable names need to be clearer. I don't know what you are attempting, so I can't comment on where the problem is.

Also perhaps provide a small example of input and the corresponding correct output for the function.

Okay. I will tell you in pseudo code...

1. Input Array of generated numbers, Array size and one of the combinations.
2. Use the search to run the combination numbers through the generated ones.
3. *Since linear and binary search return location of number using the subscript*
I want it to "return -1" if not found,  or the location if it is there, for each number.
4. So I would need it to continue and add those numbers before it sends back.
4. When sent back it would activate the winner or not winner statements.
int binarySearch(int array[],int numElems,int value[])

Let array[]={1,2,3,4,5} Let value[]={1,2,3,4,6} Let numElems = 5 What should this function return?

After the search it should return 5. Since slot 0,1,2,3 are the same, which adds up to six. And the missing 6 in the 4th slot, would return -1.

After the search it should return 5. Since slot 0,1,2,3 are the same, which adds up to six. And the missing 6 in the 4th slot, would return -1.

Why bother to search at all then? If "winning" means all 5 numbers match and you're checking for winners, just go front to back and if anything doesn't match, return false. Otherwise return true.

If you don't want to do the idea above, make a helper search function which is the normal binary search funciton:

// returns -1 for "not found".  Othewise returns corresponding index of array element that matches elementToSearchFor
int binarySearch(int array[], int arraySize, int elementToSearchfor)

Stick all your mid, low, high, and while code in here and call it from your other binarySearch function. In the larger binarySearch function, go through your combination element by element and call the helper function to do the search. Add up the return values and return the sum.

The winner and no winner function is for my assignment, but in the future I might add an option to tell you how many numbers you matched. So want to learn how to do it now. Will be fun :D

The winner and no winner function is for my assignment, but in the future I might add an option to tell you how many numbers you matched. So want to learn how to do it now. Will be fun :D

OK, use the helper function I suggested and call it for every element of the combination. Then add them up and return the sum.

So basically I am using the larger function to add together and send back.

Ok I put them through helper functions, am getting -5 in return for all of them... -.- As if it were skipping over the search function and only reading "return -1".

int liSendSearchList( int List[], int Rand_SIZE, int value[])
{   
    int num[5];

	    num[0] = binarySearch(List, Rand_SIZE, value[0]);
        num[1] = binarySearch(List, Rand_SIZE, value[1]);
        num[2] = binarySearch(List, Rand_SIZE, value[2]);
        num[3] = binarySearch(List, Rand_SIZE, value[3]);
        num[4] = binarySearch(List, Rand_SIZE, value[4]);   
	
    return num[0] + num[1] + num[2] + num[3] + num[4];
}

int searchList( char List[], int Rand_SIZE, char elemToSearchFor)
{
	for (int count = 0;count < Rand_SIZE; count++)  
	{
		if (List[count] == elemToSearchFor)
        {              // each array entry is checked to see if it contains
	                  // the desired value.
		 return count; 
                     // if the desired value is found, the array subscript
		 }	         // count is returned to indicate the location in the array
	}
	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 binarySendSearch(int array[],int Rand_SIZE,int value[]) //function heading
{
    int num[5];

	
	    num[0] = binarySearch(array, Rand_SIZE, value[0]);
        num[1] = binarySearch(array, Rand_SIZE, value[1]);
        num[2] = binarySearch(array, Rand_SIZE, value[2]);
        num[3] = binarySearch(array, Rand_SIZE, value[3]);
        num[4] = binarySearch(array, Rand_SIZE, value[4]);   
    
	
    return num[0] + num[1] + num[2] + num[3] + num[4];
}


int binarySearch(int array[],int Rand_SIZE,int elemToSearchFor) //function heading
{
	int first = 0;				    // First element of list
	int last = Rand_SIZE - 1;	    // last element of the list
	int middle = 2;					  // variable containing the current 
	                                // middle value of the list
	while (first <= last)
	{
		middle = first + (last - first)/2; 
		   
	if (array[middle] == elemToSearchFor)
		 return middle;		       // if value is in the middle, we are done
	                                 
	else if (array[middle]<elemToSearchFor)
		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
}
int binarySearch(int array[],int Rand_SIZE,int elemToSearchFor) //function heading
{
    int first = 0;				    // First element of list
    int last = Rand_SIZE - 1;	    // last element of the list
    int middle = 2;					  // variable containing the current 
	                                // middle value of the list
    while (first <= last)
    {
        middle = first + (last - first)/2; 
		   
        if (array[middle] == elemToSearchFor)
            return middle;		       // if value is in the middle, we are done
	                                 
        else if (array[middle]<elemToSearchFor)
            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
}

array = {1,2,3,4,5}
elemToSearchFor = 5

Let's walk through the code.

Before entering the while loop, first = 0, middle = 2, last = 4.
Enter while loop.
Line 9 : middle assigned to equal 2.

Line 11: array[2] = 3, elemToSearchFor = 5. These are not equal. Line 12 doesn't execute.

Line 14: array[2] = 3, elemToSearchFor = 5. 3 < 5, so line 15 executes.

Line 15: last = middle - 1 = 2 - 1 = 1.

Since Line 14 evaluated to true, lines 17 and 18 don't execute.

Back to line 9: first = 0, last = 1. Hence the searchable array segment consists of 1 and 2. 5 cannot be found.

Hence the searchable array segment consists of 1 and 2.

Sorry, don't understand.

Through my search I passed values 1,15,17,23,32 while hard coding as the random number the same elements and it still gave me -2 through linear search and -5 on the binary.

Your binary search helper function is wrong. Ignore everything but that. Step through it with paper and pencil. You aren't adjusting high, mid, and low correctly after your comparisons.

I saw and added a fix to it confusing the "-" for negatives, now its inputing the numbers correctly. Still giving me problems. Going to show it to my professor in 20 mins. To see if he can help me.

Be a part of the DaniWeb community

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