I have created a histogram program that has the user enter the lower limit of the range, then generates a 20 number array with that number at array[0] and the lower limit + 20 at array[20]. It then does a random number generation where 85% of the randoms are within the range and 15% are outside, and counts how many fall outside of the range. Then it counts occurances and prints a histogram that shows the amount below range, then prints the range (number, count, #'s or *'s), a # sign is printed for every 10 occurances that a number is counted and a * for every 1 after that 10 or if there are not 10 occurances. Just wanted to explain what the program did to allow for better knowledge of the situation.

Now, my problem is that whenever the user enters a negative number whose number does not fall into a positive range, such as user enters -50, creates a range of -50 to -30, as opposed to entering -10 and range is -10 to 10, there seems to be a problem with the randoms generating any occurances of the last 2-3 slots in the array, array[18 through 20], sometimes it's just the last 2, sometimes the last 3, never more than the last 3 slots though. I have tried many many negative numbers where the entire range stays negative and this happens to every one. Now if the range falls into positive, or even ends a 0 the randoms generate for the end of the array just fine and all goes well.

My other problem is that when the histogram generates an even 10, or multiple of 10, occurances for a specific number in the range, it will print out # signs then one *. So, if there are 10 occurances the histogram is showing 11, yet if there is 11 or 12, etc.. occurances then the histrogram prints out fine.

Any help and/or advice is greatly appreciated, sorry for the long post, my code is as follows:

#include <iostream> 
#include <iomanip> 
#include <ctime> 

using namespace std;

int main() 
{ 
	const int occurSize = 21;
	int randomGenSize, lowerLimit, percentValue, belowRange = 0, aboveRange = 0;
	int countOccurances[occurSize] = {0}, rangeNums[21];
	int* randomNumbers = 0;	

    srand(time(0)); 
	
	cout << "Please enter the lower limit of the range: ";
	cin >> lowerLimit;
	cout << "How many random numbers would like generated (1-10000): ";
	cin >> randomGenSize;

	if(randomGenSize > 10000)
	{
		cout << "Number entered does not fall between 1 and 10000.\n\n";
		cout << "Please enter a number between 1 and 10000: ";
		cin >> randomGenSize;
	}	

	for(int z = 0; z < 21; z++)  //create boundary array
	{
		rangeNums[z] = lowerLimit;
		lowerLimit++;
	}

	randomNumbers = new int[randomGenSize];
	
	for(int a = 0; a < randomGenSize; a++)   //create random numbers 85% within range, 15% within or outside range
    {
		percentValue = rand() % 100 + 1;

		if(percentValue <= 85)
		{
			if(lowerLimit >= 0)
			{
				randomNumbers[a] = rand() % ((rangeNums[20] + 1) - rangeNums[0]) + rangeNums[0];
			}
				else if(lowerLimit < 0)
				{
					randomNumbers[a] = rand() % ((rangeNums[20] - 1) - rangeNums[0]) + rangeNums[0];
				}
		}
			else
			{
				randomNumbers[a] = rand() - 16000;
			}

		if(randomNumbers[a] < rangeNums[0])           //count numbers below and above range boundaries
		{
			belowRange++;
		}
			else if(randomNumbers[a] > rangeNums[20])
			{
				aboveRange++;
			}
    } 

	for (int b = 0; b < randomGenSize; b++)          //count the occurances of each number in the range
	{ 
		for(int c = 0; c < occurSize; c++)
		{
			if(randomNumbers[b] == rangeNums[c])
			{
				countOccurances[c]++; 
			}
		}
	}

	cout << "\n\nQuantity Below Range: " << belowRange << "\n\n";

	for(int d = 0; d < occurSize; d++)                          //create the histogram
	{
			cout << setw(5) << rangeNums[d] << setw(3) << "(" << countOccurances[d] << ")" << setw(3);

			for(int e = 0; e < countOccurances[d]; e++)
			{
				while(countOccurances[d] >= 10)
				{
					cout << "#";
					countOccurances[d] -= 10;
				}

				if(countOccurances[d] < 10)
				{
					cout << "*";
				}
			}
			
			cout << endl;
	}
	
	cout << "\nQuantity Above Range: " << aboveRange << "\n\n\n";
	 
    	return 0; 
}

change line 48 to:
randomNumbers[a] = rand() % ((rangeNums[21] - 1) - rangeNums[0]) + rangeNums[0];
(all slots occupied)

change lne 91 to:
if(countOccurances[d] != 0)
(no extra '*' printed with multiples of 10)

then it seems to work.

I hate working with comparison with numbers in loops but it is sometimes so unavoidable!
Nice program.

change line 48 to:
randomNumbers[a] = rand() % ((rangeNums[21] - 1) - rangeNums[0]) + rangeNums[0];
(all slots occupied)

change lne 91 to:
if(countOccurances[d] != 0)
(no extra '*' printed with multiples of 10)

then it seems to work.

I hate working with comparison with numbers in loops but it is sometimes so unavoidable!
Nice program.

Thanks for the help. The problem with the *'s and #'s has been taken care of, I can't believe that I didn't think of that, just had a lot going on and it didn't cross my mind. As for the first problem, changing rangeNums to 21 from 20 did not solve that problem at all. When I changed it and put in to start at -50 with 50 randoms generated, all of the randoms that it generated were positive and in the thousands, very random too, like one would be 2983 and another would be 5469, etc.. So, I am not sure why changing to 21 would have made that kind of drastic change but it did. Anyone have any idea as to how to fix this, I am exhausted on ideas and would like to get this program done and over with, this one has taken me way to long to complete and this is the only problem that I am experiencing then I'm done with it. Thanks for any help anyone can offer on this, and thanks for your help Dingbats.

I've read of this sort of problem before.
Just tried it with -50 and 50 and the results are "normal": as for others, for example -20 (and less) till 200 with various randoms - all "normal".
Sugestion:
copy everything from this project into a completely new project (different name, folder, etc) and then compile - don't ask me why: somebody else will have to answer that.

When it doesn't work >= sorry. I'm only a hobby writer and with my "corrections" it worked (works) fine.

Addendum: check your ranges in loops - they're nearly always a bugger to sort out!

....that's why I wrote:
I hate working with comparison with numbers in loops but it is sometimes so unavoidable!

Good luck,
David

I've read of this sort of problem before.
Just tried it with -50 and 50 and the results are "normal": as for others, for example -20 (and less) till 200 with various randoms - all "normal".
Sugestion:
copy everything from this project into a completely new project (different name, folder, etc) and then compile - don't ask me why: somebody else will have to answer that.

When it doesn't work >= sorry. I'm only a hobby writer and with my "corrections" it worked (works) fine.

Thank you very much for your help, even though your solution didn't work for me it got me to messing around with the other numbers some more and I managed to get it to work with:

randomNumbers[a] = rand() % ((rangeNums[20] + 1) - rangeNums[0]) + rangeNums[0];

I am pretty sure that I had tried this before with failure since it is the same as the statement above it, but I guess I hadn't. And since I was working with negatives I guess I assumed that it would have to be a - 1 instead of + 1. But thanks for your help and have a great night, marking thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.