This challenging C++ practice assignment requires us to write a c++ lottery program. It randomly generates the numbers of a lottery ticket and then outputs the values. Similarly, the specific requirement of how this is going to be coded is the following:

  • Use arrays to generate 12 rows and 6 columns. So that the columns generate random numbers of the lottery ticket and the rows will showcase the numbers generated
  • Use a for loop to display the numbers stored in the arrays.
  • Restrict the random number generation from 1 – 45.
</pre>
<pre>//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Lottery ticket generator
// Author: Sahil Bora
// File:main.cpp
// Made with Visual Studio 2010
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Public variables
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
const int MIN = 1;
const int MAX = 45;
const int COLS = 6;
const int ROWS = 12;
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Function prototypes
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
void getLine(int line[]);
void printLine(int line[]);
bool duplicates(int num, int arr[], int size);
int main()
{
int row[COLS] = {0};

srand(time( NULL));

cout << "Your Tattslotto ticket\n\n";

for(int i = 0; i < ROWS; i++)
{

getLine(row);
printLine(row);
}

system("PAUSE");
}
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Bool duplicates function implementation
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
bool duplicates(int num, int arr[], int size)
{
for(int j = 0; j < size; j++)
{
if(arr[j] == num)
return true;
}

return false;
}

//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
// Get line function implementation
//−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
void getLine(int line[])
{

int rNum;

for(int i = 0; i < COLS; i++)
{
rNum = rand()%(MAX − MIN + 1) + MIN;

while(duplicates(rNum, line, i)== true)
rNum = rand()%(MAX−MIN+1)+ MIN;

line[i] = rNum;
}
}

void printLine(int line[])
{
for(int i = 0; i < COLS; i++)
cout << "\n" << line[i];

cout << endl << endl;
}</pre>
<pre>
Share this post