Strange bug in "histogram" function...

I'm in the middle of making a program that simulates a game of lotto. So far I've implemented the following features...

generating the lotto numbers
Printing the lotto numbers
Display a histogram of the distribution of numbers.

Each lotto ticket has 10 games on it, and each game has 6 numbers. The output of the function that prints each ticket looks something like this..
1
2
3
4
5
6
Ticket 4
Board  Number
A      23  34  22  2   15  28
B      23  22  11  28  16  13
...
J



The random numbers generated for the lotto tickets are between 1 and 40.

The histogram function output looks something like...
1
2
3
4
5
6
Number   Frequency   Histogram
1           10       **********
2           4        ****
3           8        ********
...
40          5        *****



The tickets are stored in a 3D array
 
int tickets[ MAX_TICKETS ][ MAX_GAMES ][ MAX_NUMBERS ];


The problem is, when I call the function to display the tickets followed by the histogram function, the histogram displays incorrect results, while if I just output the histogram (and not the tickets) the output is fine.

Before I show you the code for the histogram and displaying of the tickets, it might be helpful to know how the array of lotto tickets is generated.

Function for generating lotto numbers...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void getTickets( int tickets[][ MAX_GAMES ][ MAX_NUMBERS ], int ticketNumber )
{
	int random;
	bool isRepeat;

	srand( time( 0 ) );

	for( int i = 0; i < ticketNumber; i++ )
	{
		for( int j = 0; j < MAX_GAMES; j++ )
		{
			for( int k = 0; k < MAX_NUMBERS; k++ )
			{
				do
				{
					isRepeat = false;
					random = 1 + rand() % NUMBER_RANGE;

					for( int m = 0; m < k; m++ )
					{
						if( tickets[ i ][ j ][ m ] == random )
							isRepeat = true;
					}

				}while( isRepeat );

				tickets[ i ][ j ][ k ] = random;
			}
		}
	}
}


I hope this code is easy enough to understand, the inner for loop is to make sure there're no duplicate numbers in a single game.


Here's the code for displaying each ticket...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void displayTickets( int tickets[][ MAX_GAMES ][ MAX_NUMBERS ], int ticketNumber )
{
	for( int i = 0; i < ticketNumber; i++ )
	{
		cout << "Ticket " << i + 1 << endl
			  << setw( 7 ) << left << "Board" << "Numbers" << endl;

		for( int j = 0; j < MAX_GAMES; j++ )
		{
			cout << setw( 7 ) << left << char( 'A' + j );

			for( int k = 0; k < MAX_NUMBERS; k++ )
			{
				cout << setw( 7 ) << left << tickets[ i ][ j ][ k ];
			}

			cout << endl;
		}

		cout << endl;
	}
}



Finally here's the code for displaying the histogram...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void histogram( int tickets[][ MAX_GAMES ][ MAX_NUMBERS ], int ticketNumber )
{
	int histogram[ NUMBER_RANGE + 1 ] = { 0 };

	for( int i = 0; i < ticketNumber; i++ )
	{
		for( int j = 0; j < MAX_GAMES; j++ )
		{
			for( int k = 0; k < MAX_NUMBERS; k++ )
			{
				histogram[ tickets[ i ][ j ][ k ] ]++;
			}
		}
	}

	cout << setw( 8 ) << left << "Number" << setw( 11 ) << "Frequency"
		  << "Histogram" << endl;

	for( int i = 1; i <= NUMBER_RANGE; i++ )
	{
		cout << setw( 11 ) << i << setw( 8 ) << histogram[ i ];

		for( int j = 0; j < histogram[ i ]; j++ )
		{
			cout << j << " ";
		}

		cout << endl;
	}
}


Here's the code that calls these functions in a loop...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int tickets[ MAX_TICKETS ][ MAX_GAMES ][ MAX_NUMBERS ];
	int ticketNumber;

	ticketNumber = numberOfTickets( MAX_TICKETS );


	do
	{
		getTickets( tickets, ticketNumber );
		clrscr();
                displayTickets( tickets, ticketNumber );
		histogram( tickets, ticketNumber );
	}while ( getch() != 27 );

	return 0;


So every time I press a key on the keyboard that isn't ESC the getTickets function fills the "tickets" array with a new set of numbers, the displayTickets function then prints all these tickets and then the histogram is printed.

The problem is, the bars of the histogram (represented by '*') never shrinks in size, but the strangest part is this only seems to happen when printing 4 or more tickets. What I mean by "never shrinks in size" is, for example, on one iteration through the loop, there's a total of 5 1s, so 5 '*' are printed in the histogram. But on the next loop through only 2 1s are generated, the histogram will say the frequency is 2, but the bar representing the number of 1s will still remain at 5. Say 10 1s were then generated after that, the bar chart will then correctly increase to show 10 '*', but it'll never get smaller, even when the number of 1s is less than 10...

Like I said above, this only seems to happen when I'm printing 4 or more tickets, which just makes this bug even more weird to me.

I'm hoping there's some obvious mistake I've made that someone will find, if anyone needs any more info I'll be watching this thread and will reply with whatever info you need.
Last edited on
Here's a picture of the console screen with bugged output..
http://img36.imageshack.us/i/bugle.png/

The '*' in the histogram bars have been replaces with numbers to make it easy to see it's incorrect.
After playing around with it a bit I've discovered that the clrscr() function seems to be causing the issue, when I remove the clrscr() from the block of code I last posted and just let the output continue down the page, the histogram displays correctly. I still don't know why it's doing that though.
Topic archived. No new replies allowed.