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