A
August1
A handful of articles have been posted requesting information on how
to use these functions in addition to the time() function as the seed
to generate unique groups (sets) of numbers - each group consisting of
6 numbers - with a total of 50 groups of numbers.
A well-known girl that some publishing companies use to provide
introductory level textbooks to various Junior Colleges in the U.S.,
not surprisngly, asks for this same exact information in one of the
exercises that she presents in various editions of her Introductory
Courses for C++.
Unfortunately, the little girl forgets to tell you that these
functions cannot promise or guarantee such unique numbers to be
presented to you, now or ever!
Therefore, you as the programmer, must manipulate the output to
provide what you want. (It's nice to know that your instructors also
have the textbook with all of the answers readily available to them.
But if you wish to think they are that enlightened, go ahead.)
Below is one way, among many, to approach this exercise presented by
her highness in all of her glorious Zuk.
Just copy it and paste it and you'll get one way for executing this
exercise.
#include <iostream>
#include <fstream>//for declaring objects of the ofstream and ifstream
classes for a data file
#include <time.h>//for use of time() & symbolic constant NULL (also
defined in stdlib.h file)
#include <stdlib.h>//for use of rand() & srand() functions and
constants RAND_MAX & NULL
using namespace std;//precludes need of .h extension when importing
most header files
void main()
{
//declare and initialize variables
short count = 0;//used for counter to generate 50 groups of lottery
numbers
short lowNum = 1;//used for lowerbound number of rand() function
short highNum = 9;//used for upperbound number of rand() function
short numero = 1;//counter used for formatting output of lottery
numbers to the screen
short b = 0;//used for counter in while() loop that reads records
from data file
short siArray[6] = {0};/*declare and initialize short integer type
array containing 6
elements with 0 values for each element*/
srand(time(NULL));//initialize random number generator
ofstream send2file;//declare object of ofstream class for use with
data file
ifstream readfile;//declare object of ifstream class for use with
data file
send2file.open("T8Be08.dat", ios:ut);/*use open function associated
with both ifstream and ofstream
classes to open data file*/
//heading and information
cout << "\t\t\tLottery Numbers" << endl << endl
<< "This program opens the data file \"T8Be08.dat\" and then stores
groups of" << endl
<< "lotto numbers that have 6 unique numbers to a group in the data
file." << endl
<< "There are 50 groups of numbers. The groups of numbers are
displayed to the" << endl
<< "screen - 3 groups per line." << endl << endl;
if(!send2file.fail())//if opening data file was successful
{
while(count != 50)
{
for(short x = 0; x <= 5; x++)
{
//generate numbers
siArray[x] = lowNum + rand() % (highNum - lowNum + 1);
//adjust numeric range from 1 - 54, i.e., 1 - 9, 10 - 18, 19 - 27,
etc.
lowNum = highNum + 1;
highNum = highNum + 9;
if(x == 5)
{
for(short y = 0; y <= 5; y++)
{
if(y == 5)
{
send2file << siArray[y] << endl;//keep each range of lotto
numbers on 1 line in data file
}
else
{
send2file << siArray[y] << '#';
}//end nested if-else
}//end nested for() loop
}//end nested if
}//end nested for() loop
//reintialize lowNum and highNum for range so that range does not
exceed 54
lowNum = 1;
highNum = 9;
/*increment counter so that group of 6 numbers are generated and
sent to data file
50 times*/
count++;
}//end while() loop
send2file.close();//use close() function associated with both
ifstream and ofstream classes to close file
}
else
{
cout << "Error opening data file." << endl;
}//end if-else
readfile.open("T8Be08.dat", ios::in);//open data file for input
if(readfile.fail())
{
cout << "Error opening data file." << endl;
}
else
{
for(short z = 0; z <= 5; z++)//read 1st record from the data file
{
readfile >> siArray[z];
readfile.ignore(1);/*consume # character betwwen numbers of each
record in data file
or the invisible newline ('\n') character at the end of
each record
int the file*/
cout << siArray[z] << " ";//display each number in a group (6 per
group) to the screen
}//end nested for() loop that reads 1st group of numbers from data
file
cout << " ";//put spacing between each group of numbers displayed
to the screen
while(b != 49)//read remaining records from the data file (50 in
all)
{
for(short z = 0; z <= 5; z++)
{
readfile >> siArray[z];
readfile.ignore(1);
cout << siArray[z] << " ";
if(z == 5)
{
cout << " ";
}//end nested if
}//end nested for() loop
numero++;//increment counter for screen formatting
//if 3 groups of lotto numbers are on the output screen, go to next
line
if(numero == 3)
{
cout << endl;
numero = 0;//reset counter that allows 3 groups of numbers to
appear per line
}
b++;/*increment counter up to 50 for the 50 groups of lotto numbers
to be read from the data file*/
}//end nested while() loop
}//end if-else
cout << endl;//spacing
}//end main function
to use these functions in addition to the time() function as the seed
to generate unique groups (sets) of numbers - each group consisting of
6 numbers - with a total of 50 groups of numbers.
A well-known girl that some publishing companies use to provide
introductory level textbooks to various Junior Colleges in the U.S.,
not surprisngly, asks for this same exact information in one of the
exercises that she presents in various editions of her Introductory
Courses for C++.
Unfortunately, the little girl forgets to tell you that these
functions cannot promise or guarantee such unique numbers to be
presented to you, now or ever!
Therefore, you as the programmer, must manipulate the output to
provide what you want. (It's nice to know that your instructors also
have the textbook with all of the answers readily available to them.
But if you wish to think they are that enlightened, go ahead.)
Below is one way, among many, to approach this exercise presented by
her highness in all of her glorious Zuk.
Just copy it and paste it and you'll get one way for executing this
exercise.
#include <iostream>
#include <fstream>//for declaring objects of the ofstream and ifstream
classes for a data file
#include <time.h>//for use of time() & symbolic constant NULL (also
defined in stdlib.h file)
#include <stdlib.h>//for use of rand() & srand() functions and
constants RAND_MAX & NULL
using namespace std;//precludes need of .h extension when importing
most header files
void main()
{
//declare and initialize variables
short count = 0;//used for counter to generate 50 groups of lottery
numbers
short lowNum = 1;//used for lowerbound number of rand() function
short highNum = 9;//used for upperbound number of rand() function
short numero = 1;//counter used for formatting output of lottery
numbers to the screen
short b = 0;//used for counter in while() loop that reads records
from data file
short siArray[6] = {0};/*declare and initialize short integer type
array containing 6
elements with 0 values for each element*/
srand(time(NULL));//initialize random number generator
ofstream send2file;//declare object of ofstream class for use with
data file
ifstream readfile;//declare object of ifstream class for use with
data file
send2file.open("T8Be08.dat", ios:ut);/*use open function associated
with both ifstream and ofstream
classes to open data file*/
//heading and information
cout << "\t\t\tLottery Numbers" << endl << endl
<< "This program opens the data file \"T8Be08.dat\" and then stores
groups of" << endl
<< "lotto numbers that have 6 unique numbers to a group in the data
file." << endl
<< "There are 50 groups of numbers. The groups of numbers are
displayed to the" << endl
<< "screen - 3 groups per line." << endl << endl;
if(!send2file.fail())//if opening data file was successful
{
while(count != 50)
{
for(short x = 0; x <= 5; x++)
{
//generate numbers
siArray[x] = lowNum + rand() % (highNum - lowNum + 1);
//adjust numeric range from 1 - 54, i.e., 1 - 9, 10 - 18, 19 - 27,
etc.
lowNum = highNum + 1;
highNum = highNum + 9;
if(x == 5)
{
for(short y = 0; y <= 5; y++)
{
if(y == 5)
{
send2file << siArray[y] << endl;//keep each range of lotto
numbers on 1 line in data file
}
else
{
send2file << siArray[y] << '#';
}//end nested if-else
}//end nested for() loop
}//end nested if
}//end nested for() loop
//reintialize lowNum and highNum for range so that range does not
exceed 54
lowNum = 1;
highNum = 9;
/*increment counter so that group of 6 numbers are generated and
sent to data file
50 times*/
count++;
}//end while() loop
send2file.close();//use close() function associated with both
ifstream and ofstream classes to close file
}
else
{
cout << "Error opening data file." << endl;
}//end if-else
readfile.open("T8Be08.dat", ios::in);//open data file for input
if(readfile.fail())
{
cout << "Error opening data file." << endl;
}
else
{
for(short z = 0; z <= 5; z++)//read 1st record from the data file
{
readfile >> siArray[z];
readfile.ignore(1);/*consume # character betwwen numbers of each
record in data file
or the invisible newline ('\n') character at the end of
each record
int the file*/
cout << siArray[z] << " ";//display each number in a group (6 per
group) to the screen
}//end nested for() loop that reads 1st group of numbers from data
file
cout << " ";//put spacing between each group of numbers displayed
to the screen
while(b != 49)//read remaining records from the data file (50 in
all)
{
for(short z = 0; z <= 5; z++)
{
readfile >> siArray[z];
readfile.ignore(1);
cout << siArray[z] << " ";
if(z == 5)
{
cout << " ";
}//end nested if
}//end nested for() loop
numero++;//increment counter for screen formatting
//if 3 groups of lotto numbers are on the output screen, go to next
line
if(numero == 3)
{
cout << endl;
numero = 0;//reset counter that allows 3 groups of numbers to
appear per line
}
b++;/*increment counter up to 50 for the 50 groups of lotto numbers
to be read from the data file*/
}//end nested while() loop
}//end if-else
cout << endl;//spacing
}//end main function