In: Computer Science
In the following questions you will be designing a class called SuperDraw to
generate lotto numbers and manage the process of verifying whether a ticket
is a winner. Every lotto ticket has 6 numbers generated randomly between 1
and 49. A number cannot be repeated in the same ticket.
The main SuperDraw class structure should look like the following:
struct ticket
{
unsigned int numbers[6];
ticket* next;
};
class SuperDraw
{
private:
ticket* ticketListHead;
ticket* ticketListTail;
public:
SuperDraw(/* args */);
~SuperDraw();
};
ticket
is a linked list structure that holds the 6 lotto numbers in an array and a pointer to
the next element in the list. Notice that the class SuperDraw has 2 private data members,
ticketListHead and ticketListTail. They are 2 pointers pointing to the head (first element)
and the tail (last element) of the linked list.
Question 1 (5 pts)
Complete the implementation for the class SuperDraw by implementing the constructor
and the destructor bodies if needed. Constructor should be initializing the object to
whatever initial suitable state.
Question 2 (15 pts)
Add a public method called newTicket(int verbose = 0) that generates random 6 numbers.
The newly created ticket should be added to the linked list and the randomly generated
numbers should be printed out to the screen if verbose argument is set to 1. By default the
verbose argument is set to 0 which means that no messages will be printed out to the
screen.
The numbers should be sorted in ascending order.
Remember that the pointers ticketListHead and ticketListTail should be updated
accordingly after the generation of each new ticket
The test main() function should look like the following:
int main()
{
SuperDraw sd;
sd.newTicket(1);
}
The output should be something like:
A new ticket was successfully generated. The numbers are: 12, 14, 23, 39, 40, 44
Add a constructor that takes an int argument which corresponds to the number of tickets
to be generated.
Question 3 (10 pts)
The test main() function should look like the following:
int main()
{
SuperDraw sd(2);
}
The output should be something like:
2 new ticket were successfully generated.
The numbers are: 12, 14, 23, 39, 40, 44 and 1, 2, 9, 12, 28, 41
Screenshot
-------------------------------------------------------------------
Program
//Header file
#include<iostream>
#include<string>
using namespace std;
//Ticket node
struct ticket
{
unsigned int numbers[6];
ticket* next;
};
//sorting method
void sort(ticket* val) {
for (int i = 0; i < 6; i++) {
for (int j = i+1; j < 6; j++)
{
if
(val->numbers[i] > val->numbers[j]) {
int temp=val->numbers[i];
val->numbers[i] = val->numbers[j];
val->numbers[j] = temp;
}
}
}
}
//Create a class draw
class SuperDraw
{
//Instance variables
private:
ticket* ticketListHead;
ticket* ticketListTail;
public:
//Default constructor
SuperDraw() {
ticketListHead = NULL;
ticketListTail = NULL;
}
//Parameterized constructor
SuperDraw(int val) {
//Loop unti number of parameter
ticket generate
for (int j = 0; j < val; j++)
{
//Create a
ticket
ticket *temp =
new ticket;
//Add
values
for (int i = 0;
i < 6; i++) {
temp->numbers[i] = rand()
% 49 + 1;
}
temp->next =
NULL;
//Sort
sort(temp);
//Then add into
list
if
(ticketListHead== NULL)
{
ticketListHead= temp;
ticketListTail = temp;
temp = NULL;
}
else
{
ticketListTail->next = temp;
ticketListTail = temp;
}
}
ticket *temp = new ticket;
temp = ticketListHead;
while (temp != NULL)
{
for (int i = 0;
i < 6; i++) {
if (i < 5) {
cout <<
temp->numbers[i] << ",";
}
else {
cout <<
temp->numbers[i];
}
}
if (temp !=
ticketListTail) {
cout << " and ";
}
temp =
temp->next;
}
cout << endl;
}
//Method to add new ticket
void newTicket(int verbose=0) {
ticket *temp =
new ticket;
for (int i = 0;
i < 6; i++) {
temp->numbers[i] = rand() % 49 + 1;
}
temp->next =
NULL;
sort(temp);
if
(ticketListHead == NULL)
{
ticketListHead = temp;
ticketListTail = temp;
temp = NULL;
}
else
{
ticketListTail->next = temp;
ticketListTail = temp;
}
//If passed
verbose is 1 then print
if (verbose == 1) {
for (int i = 0;
i < 6; i++) {
if (i < 5) {
cout <<
temp->numbers[i] << ",";
}
else {
cout <<
temp->numbers[i];
}
}
cout <<
endl;
}
}
//Destructor
~SuperDraw() {
}
};
//Main method
int main()
{
srand(0);
//Create an object
SuperDraw sd(2);
//Add new ticket
sd.newTicket(1);
return 0;
}
----------------------------------------------------------
Output
22,27,36,37,38,39 and 14,23,24,36,37,44
7,16,22,28,37,46